Skip to content

Latest commit

 

History

History
73 lines (47 loc) · 2.29 KB

README.md

File metadata and controls

73 lines (47 loc) · 2.29 KB

Online Unused Code Analysis

English | 简体中文

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%.

how to use

1. Add classpath

classpath "com.bytedance.android.byteX:coverage-lib:${Versions.BYTEX_VERSION}"
classpath "com.bytedance.android.byteX:coverage-plugin:${Versions.BYTEX_VERSION}"

2. Apply plugin

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/+"]
}

3. Add dependency

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}"

4. Regist report

// Avoid to use this Implementation, just a demo
CoverageHandler.init(DemoCoverageImp.getInstance());

5. Add proguard rules

-keep class com.ss.android.ugc.bytex.coverage_lib.** { *; }

Mapping file

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.

retrace the data

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.