相信你肯定在项目中遇到过下面的问题👇
- Conversion to Dalvik format failed:Unable to execute dex: method
ID not in [0, 0xffff]: 65536
或者这样👇 - trouble writing output:Too many field references: 131000; max is
65536.You may try using --multi-dex option.
这两种问题出现的原因都是因为你的项目中的方法数超过65536。前者出现在低版本的手机中,后者出现在较高的版本之中。这里的版本以5.0来区分。
如何解决
如果你的minSdkVersion设置成21及更高,你只需要在build.gradle设置multiDexEnabled为true就可以了:
android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 25
multiDexEnabled true
}
...
}
如果你的minSdkVersion设置成20及以下,除了上述步骤外你还需要添加依赖库并进行相关配置:
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 25
multiDexEnabled true
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
在MyApplication中加入下面的代码进行配置:
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base); MultiDex.install(this);
}