android Firebase 消息传递的 multidex 支持 api

android multidex support with firebase messaging api

所以我有 Android 项目,我需要在其中包含 Firebase 消息传递 api,这是我的重要配置

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    signingConfigs {
        config {
            keyAlias 'XXXXXXX'
            keyPassword 'XXXXXXXX'
            storeFile file('/path/path/etc')
            storePassword 'noiGGW4P6509'
        }
    }

    defaultConfig {
        applicationId "xxx.xxxxxx.xxxxxx"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 37
        versionName "2.0"
        renderscriptTargetApi 20
        renderscriptSupportModeEnabled true
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.config
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:multidex:1.0.1'
    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.android.support:support-v4:24.0.0'
    compile 'com.android.support:design:24.0.0'
    compile 'com.google.android.gms:play-services-maps:9.2.0'
    compile 'com.google.android.gms:play-services-location:9.2.0'
    compile 'com.google.android.gms:play-services-analytics:9.2.0'
    compile 'com.google.android.gms:play-services-vision:9.2.0'
    compile 'com.android.support:recyclerview-v7:24.0.0'
    compile 'com.parse:parse-android:1.13.1'
    compile 'com.parse:parseinterceptors:0.0.2'
    compile 'com.parse:parsefacebookutils-v4-android:1.10.4@aar'
    compile('com.facebook.android:facebook-android-sdk:4.8.0') {
        exclude module: 'bolts-android'
    }
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.android.support:palette-v7:24.0.0'
    compile 'org.apmem.tools:layouts:1.10@aar'
    compile 'de.hdodenhof:circleimageview:2.0.0'
    compile 'jp.wasabeef:picasso-transformations:1.3.1'
    compile 'com.github.glomadrian:loadingballs:1.1@aar'
    compile 'com.vlonjatg.android:app-tour:1.0'
    compile 'com.pixplicity.sharp:library:1.0.2@aar'
    compile 'com.github.hotchemi:android-rate:0.5.6'
    compile 'info.hoang8f:android-segmented:1.0.6'
    compile 'com.android.support:cardview-v7:24.0.0'
    compile 'com.google.firebase:firebase-messaging:9.0.2'
    compile files('libs/GoogleConversionTrackingSdk-2.2.4.jar')
}

我按照此处的指南在应用程序中启用 multidex 支持 https://developer.android.com/studio/build/multidex.html,我扩展了应用程序 class

public class EnsivarApplication extends Application {

    private Tracker mTracker;

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

一切看起来都不错,应用程序可以编译,但是当它在设备 (5.0) 或模拟器 (4.3) 中 运行 时出现此错误

java.lang.NoSuchMethodError: No static method zzb(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object; in class Lcom/google/android/gms/common/internal/zzaa; or its super classes (declaration of 'com.google.android.gms.common.internal.zzaa' appears in /data/data/net.clevermobileapps.ensivar/files/instant-run/dex/slice-com.google.android.gms-play-services-basement-9.2.0_18a98179390602e5eecdbb442b4bc7d2fd2324b7-classes.dex)
at com.google.firebase.provider.FirebaseInitProvider.zza(Unknown Source)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:5146)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:4741)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4681)
at android.app.ActivityThread.access00(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1409)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5422)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)

如果我删除 firebase 库,一切正常,我不知道为什么不工作,非常感谢任何帮助,谢谢!

这是一个依赖关系问题

异常清楚地说:

com.google.android.gms-play-services

你有太多的依赖关系,我猜有些可能会发生冲突

启用 multi-dex 后,同一个库可以存在多个版本。这可能是个问题

编辑: 请也做一些研究:

同时使用 firebase 依赖版本 9.2.0,以匹配 google-play-services 依赖。

[已编辑]

从版本 15 开始

Each dependency may be updated individually, removing the need to upgrade them all simultaneously in your app.

https://android-developers.googleblog.com/2018/05/announcing-new-sdk-versioning.html