添加Firestore和MultiDex导致我的Android应用崩溃 [英] Adding Firestore and MultiDex Causes my Android App to Crash
问题描述
我一直在尝试使用具有Firestore实现的Android Studio构建一个简单的应用程序.当我将以下行添加到我的 build.gradle
文件
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
实现'com.google.firebase:firebase-firestore:21.3.0'
该应用程序甚至无法构建,并引发以下错误:
the app does not even build and throws the following error:
无法在单个dex文件中容纳请求类
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.
当然,当我删除Firestore和MultiDex SDK时,该应用程序将构建并完美运行.
Of course, when I remove the Firestore and MultiDex SDKs, the app builds and runs perfectly.
这是 app \ build.gradle
文件,其中添加了Firestore和MultiDex
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.
推荐答案
我终于知道了!问题是我正在遵循Google文档将MultiDex添加到我的应用程序中,但是其中有一个关键行已过期,这导致该应用程序崩溃.相关文档位于此处.
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.
文档中的错误指示是将行 android:name ="android.support.multidex.MultiDexApplication">
添加到清单文件中,如下所示:
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>
但是,多亏了Ivan Vovk对此问题的第二个答案的评论,应该添加到清单文件中的行是:
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.
这篇关于添加Firestore和MultiDex导致我的Android应用崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!