Adding Firestore and MultiDex Causes my Android App to Crash

烂漫一生 提交于 2021-02-08 10:43:26

问题


I have been trying to build a simple app with Android Studio which has a Firestore implementation. When I add the following line to my build.gradle file

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

the app does not even build and throws the following error:

cannot fit request class in a single dex file

I have looked this issue up and found this Stackoverflow page which reccomends adding MultiDex to app. This allows the app to build, but it crashes when I try to run it in the emulator. I've checked the log files but they don't contain any information on the crash.

Of course, when I remove the Firestore and MultiDex SDKs, the app builds and runs perfectly.

Edit: Here is the app\build.gradle file with Firestore and MultiDex added in

apply plugin: 'com.android.application'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"
    defaultConfig {
        applicationId "com.main.myapp"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1

        // MultiDex is enabled
        multiDexEnabled true

        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.android.material:material:1.0.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'com.google.firebase:firebase-auth:16.0.5'

    // These two cause the app to crash
    implementation 'com.android.support:multidex:2.0.1'
    implementation 'com.google.firebase:firebase-firestore:21.3.0'

    implementation 'com.google.firebase:firebase-functions:19.0.1'
    implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.60'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation 'com.google.android.gms:play-services-ads:18.2.0'
}

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

Does anyone have any idea on what the problem is? Any solutions would be very much appreciated.


回答1:


I have finally figured it out! The issue was that I was following the Google documentation for adding MultiDex to my app, however there is one key line which is out of date which was causing the app to crash. The documentation in question is here.

The incorrect instruction in the documentation is to add the line android:name="android.support.multidex.MultiDexApplication" > to the Manifest file like so:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapp">
    <application
            <!--Incorrect-->
            android:name="android.support.multidex.MultiDexApplication" >
        ...
    </application>
</manifest>

However, thanks to a comment from Ivan Vovk on the second answer to this question, the correct line that should be added to the Manifest file is:

android:name="androidx.multidex.MultiDexApplication"

After I made this correction, I was able to run the app without any crashes.



来源:https://stackoverflow.com/questions/58963892/adding-firestore-and-multidex-causes-my-android-app-to-crash

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!