1. 问题描述
Error:The number of method references in a .dex file cannot exceed 64K.
Error:Execution failed for task ':app:transformClassesWithDexForRelease'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command 'C:\Program Files\Java\jdk1.8.0_77\bin\java.exe'' finished with non-zero exit value 2‘’?
就是项目的总方法数量超过了64k(64*1024=65536)
2. 解决方案
方案1:使用插件化框架 比如: https://github.com/singwhatiwanna/dynamic-load-apk
方案2:分割Dex
3.分割Dex的实现方法
一、如果项目的minSdkVersion
是21或者更高,只要在项目gradle里配置multiDexEnabled 为true即可:
defaultConfig { applicationId "com.cusi.XXXXXX" minSdkVersion 21 targetSdkVersion 23 versionCode 1 versionName "1.1" // 突破65535个方法限制,有风险,启动可能会造成ANR multiDexEnabled true }二、 如果项目的
minSdkVersion
是20或者更低,则要是用分包library,如下:
1.引入multidex的library:
dependencies { compile 'com.android.support:multidex:1.0.0' }2. 在项目gradle的设置 multiDexEnabled 为true:
3. 在 AndroidManifest.xml 中的 application 标签中添加:defaultConfig {
// 突破65535个方法限制,有风险,启动可能会造成ANR multiDexEnabled true}
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.vieboo.test"> <application ... android:name="android.support.multidex.MultiDexApplication"> ... </application> </manifest>如果项目有自己的MyApplication,则将MyApplication继承MultiDexApplication.
public class MyApplication extends MultiDexApplication { ... }
<application android:name=".MyApplication" android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> ... ... ... </application>