在Android开发过程中,业务的增加会导致项目越来大。此时,很容易遇到65536异常。原因在于——在Android中,一个Dex中存储方法id用的是short类型数据,所以我们的方法总数不能超过64K
-
解决方案
- 在主module的build中加入
multiDexEnabled true
android { ... defaultConfig { ... multiDexEnabled true//transformDexArchiveWithExternalLibsDexMergerForDebug' ... } ... }
- 在主module的build中加入
dexOptions{}
android { ... dexOptions { jumboMode true javaMaxHeapSize "4g" } ... }
- 在主module的build中依赖
com.android.support:multidex:version
dependencies { ... implementation 'com.android.support:multidex:1.0.3' ... }
- 修改
Application
public class YouApplication extends MultiDexApplication { ... } ////////////////////////////////////或者 public class YouApplication extends Application { ... @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); MultiDex.install(this); } ... }
- 在主module的build中加入