引言
随着support包的不断庞大以及各种三方库的引用,现在开发中已经很用以遇到65536问题了
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: method ID not in [0, 0xffff]: 65536
产生原因
该问题即:一个DEX的method,field和class中任意一个的数量不能超过65536,一般以碰到method比较常见
对此有很多国内外大牛对它的具体产生原因进行探索,对此本人没有深入研究,也就不在这里介绍了.想知道具体原因的可以看看下面几篇博客:寒江不钓的当Field邂逅65535,杰风居的由Android 65K方法数限制引发的思考
解决办法
常见的解决方案有:Google推出的MultiDex以及美团多Dex分包.
其中MultiDex作为Gooogle推出的解决65K问题的方案比较简单易用,但是存在一些分包后启动会变慢可能会产生ANR,安装过程比较耗时等问题;另一种是美团团队的Dex自动拆包及动态加载方案,美团的解决方案可以参考:美团Android DEX自动拆包及动态加载简介下面我来详细介绍MultiDex的实现步骤
MulitiDex
1:在build.gradle中配置multiDexEnabled为true
defaultConfig {
applicationId "xxx"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
引入com.android.support:multidex:1.0.1
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.0'
testCompile 'junit:junit:4.12'
//MulitiDex Google的分包方案用于解决65K问题
compile 'com.android.support:multidex:1.0.1'
}
2如果已经有自己的Application,则让其继承MultiDexApplication即可;
如果自己的Application已经继承了其他的Application,则可以重新attachBaseContext方法
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
MultiDex.install(this);
}
ok就这么简单
申请大的堆内存
在中javaMaxHeapSize “Xg” X即要申请的内存大小
android {
compileSdkVersion 24
buildToolsVersion "24.0.3"
defaultConfig {
applicationId "com.xindz.entertainoneself"
minSdkVersion 19
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dexOptions {
preDexLibraries = false
javaMaxHeapSize "4g" // 调大 HeapSize
}
}