When your app and the libraries it references exceed 65,536 enmethods, we need to ship apk with multidex enable, usually follow the guides: Enable multidex for apps with over 64K methods.
However the main dex classes.dex
can still be too large; It' difficult to maintain the mainDexList.txt since your app's keep growing.
What's more, the gradle task for multidex can changes too, that may break our task hook.
The dexing
library can help you get rid of maintaining the mainDexList.txt,since we've minimize the classes needed before multidex
and optdex
:
1.Add dependence to dexing
aar:
implementation 'com.github.avenwu:dexing:$latest_version'
2.Install dex in application:
The install api is same as multidex support library
:
import cn.hacktons.dexing.Multidex;
public class CustomApplication extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
Multidex.install(this);
}
}
3.Keep rule Make sure the dexing package is included in main dex. If not, add this rule into your dex keep list:
# multidex-config.pro
-keep class cn.hacktons.dexing.**
multiDexKeepProguard file('multidex-config.pro')
We've provided a Activity with simple loading process bar when dex is installing. You may replace the loading layout through the second parameter of Multidex.install
:
Multidex.install(this, R.layout.custom_loading);
If there are ClassNotFoundException, keep missing class in the multiDexKeepProguard
file as Enable multidex for apps with over 64K methods mentioned.
If dex installation doest work well, you can enable logging to see the whole process:
// enable log
Multidex.enableLog();
Multidex.install(this);
Here are some example logs, including main process
and :nodex process
.