Dex file exceeded 64k after adding Google Play Services 9.0.0

最后都变了- 提交于 2019-12-23 07:59:07

问题


I was following the Firebase guide to adding FCM, and so I added the following dependencies to my app gradle:

compile 'com.google.android.gms:play-services:9.0.0'
apply plugin: 'com.google.gms.google-services'

And this one to my project gradle:

classpath 'com.google.gms:google-services:3.0.0'

After this, I added the google-services.json from the Firebase console settings by downloading it and adding it to my app directory.

Now I'm getting this error:

Error:The number of method references in a .dex file cannot exceed 64K. Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

If I follow the instructions and have my application to support multidex, my application crashes as soon as it launches.

Here is the error after adding multidex to my app:

    05-20 01:25:32.253 19812-19812/com.cryogenos.pearsonvisionlimousine W/dalvikvm: VFY: unable to resolve static field 8723 (common_google_play_services_unknown_issue) in Lcom/google/android/gms/R$string;05-20 01:25:32.253 19812-19812/com.cryogenos.pearsonvisionlimousine W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x416b5e30)05-20 01:25:32.253 19812-19812/com.cryogenos.pearsonvisionlimousine E/AndroidRuntime: FATAL EXCEPTION: mainProcess: com.cryogenos.pearsonvisionlimousine, PID: 19812java.lang.NoClassDefFoundError: com.google.android.gms.R$stringat com.google.android.gms.common.internal.zzah.<init>
(Unknown Source)
at com.google.firebase.FirebaseOptions.fromResource(Unknown Source)
at com.google.firebase.FirebaseApp.zzbu(Unknown Source)
at com.google.firebase.provider.FirebaseInitProvider.onCreate(Unknown Source)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1609)
at android.content.ContentProvider.attachInfo(ContentProvider.java:1574)
at com.google.firebase.provider.FirebaseInitProvider.attachInfo(Unknown Source)
at android.app.ActivityThread.installProvider(ActivityThread.java:5643)
at android.app.ActivityThread.installContentProviders(ActivityThread.java:5206)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5143)
at android.app.ActivityThread.access$1500(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5883)
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:872)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:688)
at dalvik.system.NativeStart.main(Native Method)

My phone's google play services is 9.0.0+. I have updated to the latest play services and the repository in SDK manager.

EDIT:

My app build.gradle:

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "22.0.1"

defaultConfig {
    applicationId "com.cryogenos.pearsonvisionlimousine"
    minSdkVersion 19
    targetSdkVersion 23
    versionCode 3
    versionName "2.1"
    multiDexEnabled true
}
buildTypes {
    release {
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.google.android.gms:play-services:9.0.0'
compile 'com.mcxiaoke.volley:library:1.0.19'
compile 'com.android.support:design:23.1.1'
compile 'com.android.support:multidex:1.0.0'
compile 'com.google.firebase:firebase-messaging:9.0.0'
}

apply plugin: 'com.google.gms.google-services'


回答1:


One possible way to avoid 64k Dex error is by including only those APIs which your app needs from Google Play Services.

Selectively compiling APIs into your executable

From version 6.5, you can instead selectively compile Google Play service APIs into your app. For example, to include only the Google Fit and Android Wear APIs, replace the following line in your build.gradle file:

compile 'com.google.android.gms:play-services:9.0.0'

with these lines:

compile 'com.google.android.gms:play-services-fitness:9.0.0'
compile 'com.google.android.gms:play-services-wearable:9.0.0'

UPDATED Well I read the doc, which says

Apps that rely on the Play Services SDK should always check the device for a compatible Google Play services APK before accessing Google Play services features

So you only need to check Google Play Service is available or not. And to do this you only need Google Actions, Base Client Library API.

compile 'com.google.android.gms:play-services-base:9.0.0'




回答2:


as explained in documentation use Selectively compiling APIs into your executable which is best approach for you(take it in first priority insted of Multi Dex), because Multi-dex have some limitations check before proceed.

Avoiding the 64K Limit - Proguard will help you

Before configuring your app to enable use of 64K or more method references, you should take steps to reduce the total number of references called by your app code, including methods defined by your app code or included libraries. The following strategies can help you avoid hitting the dex reference limit:

Review your app's direct and transitive dependencies - Ensure any large library dependency you include in your app is used in a manner that outweighs the amount of code being added to the application. A common anti-pattern is to include a very large library because a few utility methods were useful. Reducing your app code dependencies can often help you avoid the dex reference limit.

Remove unused code with ProGuard - Configure the ProGuard settings for your app to run ProGuard and ensure you have shrinking enabled for release builds. Enabling shrinking ensures you are not shipping unused code with your APKs.

Using these techniques can help you avoid the build configuration changes required to enable more method references in your app. These steps can also decrease the size of your APKs, which is particularly important for markets where bandwidth costs are high.

So, try to avoid Multi-Dex


one more thing when you use compile 'com.android.support:design:23.1.1' then you not need to use compile 'com.android.support:appcompat-v7:23.1.1' and compile 'com.android.support:support-v4:23.1.1'. so remove v7 & v4 from build.gradle file




回答3:


Use MultiDex

MultiDex allows you to use multiple DEX files contained within one APK. With a few steps

Multidex support steps




回答4:


First check if the multidex has really worked, you can do that by renaming the apk file to zip and extracting it. There should 2 multiple classes.dex files.

Secondly, it can also happen if earlier to reduce the method you must have used proguard, so just comment the use of proguard and it should work.

Otherwise paste your exception here.




回答5:


Something wrong.

Checking the doc you only need this dependency

dependencies {
     compile 'com.google.firebase:firebase-messaging:9.0.0'
}

So you can remove this dependency

//compile 'com.google.android.gms:play-services:9.0.0'

If it is not enough and you have more than 65536 methods you can use the multidex support.

Just add these lines in the build.gradle:

android {

    defaultConfig {
        ...

        // Enabling multidex support.
        multiDexEnabled true
    }
    ...
}

dependencies {
  compile 'com.android.support:multidex:1.0.0'
}

Also in your Manifest add the MultiDexApplication class from the multidex support library to the application element

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.multidex.myapplication">
    <application
        ...
        android:name="android.support.multidex.MultiDexApplication">
        ...
    </application>
</manifest>

and override attachBaseContext method:

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

If you are using a own Application class, change the parent class from Application to MultiDexApplication.



来源:https://stackoverflow.com/questions/37338398/dex-file-exceeded-64k-after-adding-google-play-services-9-0-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!