Project address: github.com/JeasonWong/…
Usage scenarios
There are many scenarios, but let me start with two:
-
When multidex is used for subcontracting, app is started for the first time to optimize
-
Proxy applications, such as InstantRun and other Hotfix frameworks, are needed
- …
How to use
The official plan
There are two ways to add a specified class to maindex:
- MultiDexKeepProguard file (“. / maindex – rules. Pro ‘)
- MultiDexKeepFile file (“. / maindex. TXT “)
I won’t go into details about the use of these two schemes.
MainDexWrapper
configuration
MainDexWrapper supports two ways, one of which is similar to the official multiDexKeepProguard file(‘./maindex-rules.pro’).
Build. Gradle first relies on the plugin at the root:
buildscript {
repositories {
maven {
url 'https://dl.bintray.com/wangyuwei/maven'
}
}
dependencies {
classpath 'me.wangyuwei:maindexwrapper-plugin:1.0.0'
}
}
allprojects {
repositories {
maven {
url 'https://dl.bintray.com/wangyuwei/maven'
}
}
}Copy the code
Then use the plugin in your app’s build.gradle:
Apply the plugin: 'me. Wangyuwei. Maindexwrapper' compile 'me. Wangyuwei: maindexwrapper - annotations: 1.0.0'Copy the code
use
Two ways:
- Keep writing – rules
- Add to the specified class
@KeepMainDex
annotations
1, Write keep-rules:
# #-keep class me.wangyuwei.maindexwrapper.demo.Demo {*; }Copy the code
Just like configuration obfuscation, write the rules that you need to break into maindex’s classes.
2. Annotate the specified class with @keepmaindex
import me.wangyuwei.maindexwrapper.annotations.KeepMainDex;
@KeepMainDex
public class Demo {
public class InnerClass {
}
}Copy the code
Like obfuscating the support @keep annotation, it’s easy to use.
The principle of
It’s really simple.
Add our keep-rules to /intermediates/multi-dex/${varie.dirname}/manifest_keep.txt before executing the multidexTask.
Manifest_keep. TXT is an important document in the multidex subcontracting process, where the keep class is entered into maindex.
There are now two easy ways to implement plug-ins:
The first is to copy the contents of the developer’s written Maindex-rules.pro directly into the manifest_keep.txt.
The second is to annotate the classes before the dex is generated. The classes that contain the @keepmaindex annotation are collected and copied into the manifest_keep.txt like the former. The ASM traversal classes I use here, Note that you need to replace the “/” in ClassName with “.”, and the rest is simple.
The tail language
Plugins down the whole relatively simple, as a practice can also be.