Android The number of method references in a .dex file cannot exceed 64K

方法数64k的限制
The number of method references in a .dex file cannot exceed 64K

exceed 64K

或者
java.util.concurrent.ExecutionException: com.android.dex.DexIndexOverflowException: method ID not in [0, 0xffff]
这两种报错是同一个问题,都是方法数超限了。

那项目到底有多少个方法数呢,可参阅 android 统计项目方法数

然而,知道了项目方法数并无济于事。

方法数超限怎么解决呢?
  1. app的 build.gradle 的 defaultConfig 中添加 multiDexEnabled
android {
    //***
    defaultConfig {
        //***
        multiDexEnabled true
        //***
    }
}
  1. app的 build.gradle 的 dependencies 中添加 multidex 依赖,如下:
dependencies {
    compile 'com.android.support:multidex:1.0.3'
    //***
}
  1. application 配置(两个配置方式):
    a. 使项目AndroidManifest.xml中配置的 application继承于 android.support.multidex.MultiDexApplication
import android.support.multidex.MultiDexApplication;

public class MyApplication extends MultiDexApplication {
    //***
}

b. 若application未继承MultiDexApplication,则覆盖 attachBaseContext 方法并添加 MultiDex.install(context)。如下:

import android.app.Application;
import android.content.Context;
import android.support.multidex.MultiDex;

public class MyApplication extends Application {

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

   //***
}

至此,就可超越64k了

over~

你可能感兴趣的:(Android The number of method references in a .dex file cannot exceed 64K)