Android MultiDex - Questions on Inner Workings

老子叫甜甜 提交于 2019-11-29 04:07:39

1) The gradle plugin internally uses Proguard to create two jar files in the intermediates/multi-dex build folder. One will be the primary dex, the rest will be spread out over dex 2, 3 etc.

The collect{variant}MultiDexComponents task is responsible for creating the keep file for proguard, you can see this file and other proguard parameters used in the variant specific subdirectory of the folder I mentioned above. Hopefully this will be customisable in the long run.

There is also currently a bug relating to test projects in 1.0.0-rc1 of the gradle plugin (https://code.google.com/p/android/issues/detail?id=80741). With some small changes the workaround I posted there can also be used to add your own entries to the keep list (thus ensuring your classes end up in the primary dex) right now.

2) Modules refer to modules from a Gradle perspective, but these can indeed be the different items mentioned in the list you linked to. If you do a pre-lollipop gradle build from the commandline with --info as a flag you can see all the dex files being passed to dx. (Note that this should not a multidex enabled build or one with preDexLibraries = false).

What is being placed in main-dex?

There are three sequential tasks that calculate which classes should be packaged in main dex:

collect{variant}MultiDexComponents task

This task writes the class names of all the application components (application, activities, services, receivers, providers) to a text file according to manifest. That is, if you have a component that is not registered in manifest it won't be placed in main-dex. And there're other non-manifest classes - annotations for example. To see a full list check out the CreateManifestKeepList.groovy task in plugin sources.
The output of this task is manifest_keep.txt file.

shrink{variant}MultiDexComponents task

This task invokes ProGuard to create a shrinked jar with only the classes that are mentioned in manifest_keep.txt file.
The output of this task is componentClasses.jar file.

create{variant}MainDexClassList task

This task takes the componentClasses.jar file, and for each class calculates direct references hierarchy (for more info see implementation). That is, if one of your component classes has a field of type X, then this X class will be added to main-dex list as well.
The output of this task is maindexlist.txt, which includes list of all the classes that will be packaged into a main dex.

What happens if my minSdk is 21?

None of the above tasks are run if your minSdk version is 21 - no main-dex list calculation required. This is because in ART, all the dex files converted into a single .oat file during the app installation. And therefore no runtime ClassLoader patching required.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!