Analysis of useless codes reported by online users
Due to unreasonable code design or the keep rules, static code inspection cannot find all useless code.
So we analyze from the perspective of the user, instrument each class, and report the information to the server during execution. Based on a large number of user reports, classes that are not used \can be defined as unused classes.
In Tiktok, we found 1/6 of the useless classes, excluding the resources they reference, for a total of 3M (Dex size 20M). If delete all, it will reduce the package size by 5%.
classpath "com.bytedance.android.byteX:coverage-lib:${Versions.BYTEX_VERSION}"
classpath "com.bytedance.android.byteX:coverage-plugin:${Versions.BYTEX_VERSION}"
After the plugin is applied, the code like CoverageHandler.addData(123)
will be inserted during init
;
The input number records the class or method information, which can be decoded by the generated mapping table file.
apply plugin: "bytex.coverage"
CoveragePlugin{
logLevel "DEBUG"
// we perfer input from CI
enable System.getProperty("constructor_coverage", "false") == 'true'
enableInDebug false
// only report <clinit> for better performance
clInitOnly true
whiteList = ["com/ss/android/ugc/bytex/+"]
}
Instrumentation will call an interface, we add dependencies, and implement this interface to handle reporting information.
implementation "com.bytedance.android.byteX:coverage_lib:${Versions.BYTEX_VERSION}"
// Avoid to use this Implementation, just a demo
CoverageHandler.init(DemoCoverageImp.getInstance());
-keep class com.ss.android.ugc.bytex.coverage_lib.** { *; }
The files are saved in app/build/ByteX/${variant}Release/CoveragePlugin/coverage
.
Mapping_latest.txt is the id-class mapping table file, and the graph file records the parent-child relationship.
You can convert id to class info with the mapping. And you can locate the useless classes, frequency of code call, etc.
More information can be referred to the code under the test dir.