【问题标题】:Adding Firestore and MultiDex Causes my Android App to Crash添加 Firestore 和 MultiDex 导致我的 Android 应用程序崩溃
【发布时间】:2019-11-22 17:31:32
【问题描述】:

我一直在尝试使用具有 Firestore 实现的 Android Studio 构建一个简单的应用程序。当我将以下行添加到我的 build.gradle 文件中时

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

应用程序甚至没有构建并引发以下错误:

cannot fit request class in a single dex file

我查看了这个问题,发现 this Stackoverflow page 建议将 MultiDex 添加到应用程序。这允许应用程序构建,但是当我尝试在模拟器中运行它时它会崩溃。我检查了日志文件,但它们不包含有关崩溃的任何信息。

当然,当我删除 Firestore 和 MultiDex SDK 时,应用程序会完美构建和运行。

编辑:这是添加了 Firestore 和 MultiDex 的 app\build.gradle 文件

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'

有人知道问题是什么吗?任何解决方案将不胜感激。

【问题讨论】:

  • 向我们展示您添加 MultiDex 的文件。
  • @AlexMamo 我添加了 build.gradle 文件
  • 您还需要至少编辑manifest 中的应用程序名称字段,并可选择创建自己的MultiDexApplication 类。
  • 我怀疑您确实记录了错误,但应用程序正在重新启动并清除您的 logcat。尝试按包而不是“我的应用程序”自定义过滤器(快速破解以禁用 应用重启时擦除行为)

标签: android firebase google-cloud-firestore android-multidex


【解决方案1】:

我终于明白了!问题是我正在按照 Google 文档将 MultiDex 添加到我的应用程序中,但是有一个关键行已过时,导致应用程序崩溃。有问题的文档是here

文档中的错误说明是将行 android:name="android.support.multidex.MultiDexApplication" > 添加到 Manifest 文件中,如下所示:

<?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 对this question 的第二个回答的评论,应该添加到 Manifest 文件的正确行是:

android:name="androidx.multidex.MultiDexApplication"

在我进行此更正后,我能够运行该应用而没有任何崩溃。

【讨论】:

  • 如果应用程序使用 androidX 那么这就是我们应该添加的内容 android:name="androidx.multidex.MultiDexApplication" 否则对于非 androidx 这仍然是有效的一个 "android:name="android .support.multidex.MultiDexApplication""
猜你喜欢
  • 1970-01-01
  • 2017-05-28
  • 2018-09-18
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2013-12-02
  • 2014-09-09
相关资源
最近更新 更多