无法为API 19提供com.google.firebase.provider.FirebaseInitProvider提供程序

时间:2017-12-17 11:04:58

标签: android multidex

当我使用带有API 27的andorid版本时,它工作正常,但当我尝试使用API​​ 19运行它时,我收到以下错误:

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/se.dxapps.skidspar.beta-1.apk"],nativeLibraryDirectories=[/data/app-lib/se.dxapps.skidspar.beta-1, /vendor/lib, /system/lib]]
                                                                           at android.app.ActivityThread.installProvider(ActivityThread.java:4793)
                                                                           at android.app.ActivityThread.installContentProviders(ActivityThread.java:4385)
                                                                           at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4325)
                                                                           at android.app.ActivityThread.access$1500(ActivityThread.java:135)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:136)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                           at java.lang.reflect.Method.invokeNative(Native Method)
                                                                           at java.lang.reflect.Method.invoke(Method.java:515)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                           at dalvik.system.NativeStart.main(Native Method)
                                                                        Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.firebase.provider.FirebaseInitProvider" on path: DexPathList[[zip file "/data/app/se.dxapps.skidspar.beta-1.apk"],nativeLibraryDirectories=[/data/app-lib/se.dxapps.skidspar.beta-1, /vendor/lib, /system/lib]]
                                                                           at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
                                                                           at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
                                                                           at android.app.ActivityThread.installProvider(ActivityThread.java:4778)

所以我搜索并尝试了很多建议来解决这个问题,但它们都没有真正起作用。

我的gradle文件:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 26
    buildToolsVersion '26.0.2'

    dexOptions {
        preDexLibraries = false
    }


    defaultConfig {
        applicationId "se.dxapps.skidspar.beta"
        minSdkVersion 15
        targetSdkVersion 26
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
        multiDexEnabled true

    }

    buildTypes {
        release {
            minifyEnabled false

        }
    }
    lintOptions {
        abortOnError false
    }
    useLibrary 'org.apache.http.legacy'
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:26+'
    compile 'com.android.support:mediarouter-v7:26.1.0'
    compile 'com.android.support:support-v4:26.1.0'
    compile 'com.android.support:design:26.1.0'

    compile 'com.google.android.gms:play-services:11.0.0'
    compile 'com.google.firebase:firebase-messaging:11.0.0'

    compile 'com.crittercism:crittercism-android-agent:+'
    compile 'com.felipecsl:gifimageview:2.0.0'
    compile 'pl.droidsonroids.gif:android-gif-drawable:1.1.+'

    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.android.support:multidex:1.0.1'
}

我还更改了最新版本的google gms服务。这就是build.gradle文件的外观:

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

buildscript {
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath 'com.google.gms:google-services:3.1.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}


afterEvaluate {
    tasks.matching {
        it.name.startsWith('dex')
    }.each { dx ->
        if (dx.additionalParameters == null) {
            dx.additionalParameters = ['--multi-dex']
        } else {
            dx.additionalParameters += '--multi-dex'
        }
    }
}

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

我还在MainActivity中添加了这行代码,以反映不支持DEX的API版本。

  @Override
    protected void attachBaseContext(Context context) {
        super.attachBaseContext(context);
        MultiDex.install(this);
    }

现在我没有想法,有人有其他建议,或者可以看到问题可能出在哪里?

修改 我试过并添加了一个包含这些行的multiDexKeepFile:

se/dxapps/skidspar/MainActivity.class
com/google/firebase/provider/FirebaseInitProvider.class

并将其置于build.gradle

 buildTypes {
        release {
            multiDexKeepFile file('multidex_keep_file.txt')
            minifyEnabled false
        }

        debug{
            multiDexKeepFile file('multidex_keep_file.txt')
        }
    }

执行并运行应用程序后,我收到此错误:

Could not find method com.google.firebase.FirebaseApp.initializeApp, referenced from method com.google.firebase.provider.FirebaseInitProvider.onCreate
12-17 12:16:40.270 4325-4325/se.dxapps.skidspar.beta W/dalvikvm: VFY: unable to resolve static method 19752: Lcom/google/firebase/FirebaseApp;.initializeApp (Landroid/content/Context;)Lcom/google/firebase/FirebaseApp;
12-17 12:16:40.270 4325-4325/se.dxapps.skidspar.beta D/dalvikvm: VFY: replacing opcode 0x71 at 0x0004
12-17 12:16:40.270 4325-4325/se.dxapps.skidspar.beta D/AndroidRuntime: Shutting down VM
12-17 12:16:40.270 4325-4325/se.dxapps.skidspar.beta W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x9cca1b20)
12-17 12:16:40.270 4325-4325/se.dxapps.skidspar.beta E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: se.dxapps.skidspar.beta, PID: 4325
                                                                       java.lang.NoClassDefFoundError: com.google.firebase.FirebaseApp
                                                                           at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)

1 个答案:

答案 0 :(得分:0)

Multidex需要更多的构建时间和决策,可能会排除某些文件。尝试使用multiDexKeepFilemultiDexKeepProguard属性来防止它们被排除在外。 阅读this