首页 > 解决方案 > 添加 Firestore 依赖项时,简单程序会出现“无法将请求的类放入单个 dex 文件中”

问题描述

我有一个运行良好的非常简单的程序——直到我添加库依赖项以连接 Firestore。具体来说,当我将实现 'com.google.firebase:firebase-firestore:21.2.0' 添加到我的应用程序 gradle 时。(更改为较早的 17.xx 版本没有区别)。错误是“无法在单个 dex 文件中容纳请求的类”,这对我来说毫无意义,因为程序是如此简单:我运行的方法不可能超过 64K。

这是主要活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

public void addUnitType(View view) {
    return;
}

}

...这里是 activity_main.XML

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edit_unit_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:layout_marginStart="7dp"
        android:hint="Unit Type (MedSurg, ICU, etc)"
        android:inputType="text"
        android:textSize="10sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

...这是项目等级

    // Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.5.1'
        classpath 'com.google.gms:google-services:4.3.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

...这是应用程序 gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.0"
    defaultConfig {
        applicationId "com.example.nofirestore"
        minSdkVersion 19
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    **implementation 'com.google.firebase:firebase-firestore:21.2.0'**
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'androidx.cardview:cardview:1.0.0'
}

再一次,这是应用程序 gradle 中的违规代码——删除它,我很好,把它放在依赖项中,编译崩溃:

implementation 'com.google.firebase:firebase-firestore:21.2.0'

这是错误

"Cannot fit requested classes in a single dex file"

有什么想法吗?我已经运行了更复杂的应用程序,而不需要 multidex。我愿意知道。谢谢你。

标签: androidfirebasegoogle-cloud-firestoreandroid-multidex

解决方案


好吧,这不是失踪的

apply plugin: 'com.google.gms.google-services'

相反,它就像minSdkVersion19一样简单:将其更改为 22 消除了 multidex 编译错误,并且运行良好!


推荐阅读