前段时间项目在要打包的时候突然报错了,报错如下:
Error:Execution failed for task ':app:transformClassesWithDexForDebug'.
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: Error while executing java process with main class com.android.dx.command.Main with arguments {--dex --num-threads=4 --multi-dex --main-dex-list E:******-master (3)******-master\app\build\intermediates\multi-dex\debug\maindexlist.txt --output E:******-master (3)******-master\app\build\intermediates\transforms\dex\debug\folders\1000\1f\main E:******-master (3)******-master\app\build\intermediates\transforms\jarMerging\debug\jars\1\1f\combined.jar}
最初一看到这个error信息里面的--multi-dex,想着是不是因为因为项目里面有重复的jar包,因为以前项目有类似问题,通过删除重复jar包可以解决问题,但是这一次却不行。
接着思考是否文件的方法引用超过了65535个方法限制, 然后我给项目进行分包,对其虚拟机堆分配内存大小 ,在项目app的gradler配置文件中,添加如下
defaultConfig {
multiDexEnabled true
}
dexOptions {
incremental true
jumboMode true
javaMaxHeapSize "4g"
preDexLibraries = false
}
依然无法解决问题,最后终于在https://stackoverflow.com/questions/19955297/how-can-i-use-the-multi-dex-option这里看到了类似的解决方案:
其中在问题回答部分,我看到了这样一段话“Then you need to add the multidex support library jar, located in sdk/extras/android/support/multidex/library/libs. And install it either by extending your application from MultiDexApplication, or calling Multidex.install() from your application's attachBaseContext method.”。重点就是“ application from MultiDexApplication”,让你的Application extends MultiDexApplication就ok了