在android開發(fā)的過程中浮定,隨著app的功能和代碼的增加揽涮,總會在一次編譯后遇到這個錯誤:
Conversion to Dalvik format failed:
Unable to execute dex: method ID not in [0, 0xffff]: 65536
這就是android中方法數超過64k狸窘,即64 * 1024位數的限制。在android官方api中給出了這個問題的解決方案《配置方法數超過 64K 的應用》烟号,讓你完美的規(guī)避64k的限制绊谭。
1.如果你的minSdkVersion的設置>=21,只需要在build.gradle中設置multiDexEnabled為true
android {
defaultConfig {
...
minSdkVersion 21
targetSdkVersion 26
multiDexEnabled true
}
...
}
2.如果你的minSdkVersion的設置<21汪拥,就需要如下操作了
1.在AndroidMainfest.xml中添加application达传。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<application
android:name="MyApplication" >
...
</application>
</manifest>
2.調用attachBaseContent()方法調用Multidex.install(this)。
public class MyApplication extends SomeOtherApplication {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(context);
Multidex.install(this);
}
}
3.build.gradle中添加
android {
defaultConfig {
...
minSdkVersion 15
targetSdkVersion 26
multiDexEnabled true
}
dexOptions {
incremental true
javaMaxHeapSize "4g"
}
...
}
dependencies {
compile 'com.android.support:multidex:1.0.1'
}
就搞定了迫筑!