Multidex、Proguard 和 Firebase

Multidex, Proguard and Firebase

当我将 Firebase 身份验证和数据库添加到我的项目时,出现以下问题:

 FATAL EXCEPTION: main Process: com.myapp, PID: 4024          java.lang.RuntimeException: Unable to get provider    com.google.firebase.provider.FirebaseInitProvider:    java.lang.ClassNotFoundException: Didn't find class   "com.google.firebase.provider.FirebaseInitProvider" on path:   DexPathList[[zip file "/data/app/com.myapp-NTr4rdWkzRUXkEy5dItLzQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp-NTr4rdWkzRUXkEy5dItLzQ==/lib/arm64, /system/lib64, /system/vendor/lib64]]

于是app就关闭了,我上网查了下可能是Multidex,但是我尝试添加Multidex,出现如下错误:

  FATAL EXCEPTION: main Process: com.myapp, PID: java.lang.ClassNotFoundException: Didn't find class ".MyApplication" on path: DexPathList[[zip file "/data/app/com.myapp-NTr4rdWkzRUXkEy5dItLzQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.myapp-NTr4rdWkzRUXkEy5dItLzQ==/lib/arm64, /system/lib64, /system/vendor/lib64]]

这是我的 gradle 文件:

apply plugin: 'com.android.application'

android {
compileSdkVersion 29

 defaultConfig {
    applicationId "com.company.myapp"
    minSdkVersion 16
    targetSdkVersion 29
    versionCode 1
    versionName "1.0.0"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'            
    }
 }
}

dependencies {
compile 'com.google.firebase:firebase-auth:+'
compile 'com.google.firebase:firebase-database:+'
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'androidx.multidex:multidex:2.0.0+'
compile 'androidx.appcompat:appcompat:1.0.0'
compile 'androidx.legacy:legacy-support-v4:1.0.0'
compile 'com.google.android.material:material:1.0.0-rc01'
compile 'androidx.cardview:cardview:1.0.0'
}

如某些论坛所述,应用程序必须包含 MyApplication class

public class MyApplication extends Application { 
protected void attachBaseContext(Context base) { 
super.attachBaseContext(base); 
MultiDex.install(this); } }

并且在应用程序标签中

android:name="MyApplication"

我试图用 proguard 维护 FirebaseInitProvider class 但它没有用。该应用程序也停止工作了!

我还检查了 dex 文件,FirebaseInitProvider 在 APK 中。

有什么解决办法吗?

将此添加到您的混淆器文件

-keep class com.google.firebase.** { *; }