android resource file obfuscation - AndResGuard

What is AndResGuard

  AndResGuard is a tool to reduce APK size, its principle is similar to Java Proguard, but only for resources. It will shorten the otherwise lengthy resource path, such as res/drawable/wechat to r/d/a.


AndResGuard configuration

In build.gradle in the project root directory, add the dependencies of the plugin:

classpath 'com.tencent.mm:AndResGuard-gradle-plugin:1.2.10'

 You can create a separate gradle file and create the androidres_build.gradle file in the app directory, as shown in the figure:


Specific configuration and meaning:

apply plugin: 'AndResGuard'

andResGuard {
    mappingFile = null
    use7zip = true
    useSign = true
    keepRoot = false
    compressFilePattern = [
            "*.png",
            "*.jpg",
            "*.jpeg",
            "*.gif",
            "resources.arsc"
    ]
    whiteList = [
            // your icon
"R.drawable.icon",
            // for fabric
"R.string.com.crashlytics.*",
            // for umeng update
"R.string.tb_*",
            "R.layout.tb_*",
            "R.drawable.tb_*",
            "R.drawable.u1*",
            "R.drawable.u2*",
            "R.color.tb_*",
            // umeng share for sina
"R.drawable.sina*",
            // for google-services.json
"R.string.google_app_id",
            "R.string.gcm_defaultSenderId",
            "R.string.default_web_client_id"                                                            ,
            "R.string.ga_trackingId",
            "R.string.firebase_database_url",
            "R.string.google_api_key",
            "R.string.google_crash_reporting_api_key",

            //友盟
//            "R.string.umeng*",
//            "R.string.UM*",
//            "R.layout.umeng*",
//            "R.drawable.umeng*",
//            "R.id.umeng*",
//            "R.anim.umeng*",
//            "R.color.umeng*",
//            "R.style.*UM*",
//            "R.style.umeng*",
            //融云
//            "R.drawable.u*",
//            "R.drawable.rc_*",
//            "R.string.rc_*",
//            "R.layout.rc_*",
//            "R.color.rc_*",
//            "R.id.rc_*",
//            "R.style.rc_*",
//            "R.dimen.rc_*",
//            "R.array.rc_*"
]
    

    sevenzip {
        artifact = 'com.tencent.mm:SevenZip:1.2.10'
//path = "/usr/local/bin/7za"
}            
}

Among them, the whiteList (whitelist) specifies the resource path rules that do not need to be obfuscated, mainly some third-party SDKs, because some SDK codes refer to the corresponding resource files. If they are obfuscated, the corresponding resources will not be found. file, crash occurs, so it cannot be confused with its resource file. For Umeng and Rongyun, you can log out if you use it according to the above format. More whitelists can be viewed, AndResGuard whitelist.


  Since we are not adding the AndResGuard configuration in the build.gradle under the app module, but separately out androidres_build .gradle , we need to reference it in the build.gradle file under the app module, starting with the build.gradle file under the app module Add the following code references:

apply from: 'androidres_build.gradle'

Use of AndResGuard

After integrating AndResGuard, in the gradle tasks of the app, there is an additional task called andresguard, as shown in the figure:


If you want to play the debug package, execute the resguardDebug command;

If you want to play the preview package, execute the resguardPreview command;

If you want to release the package, execute the resguardRelease command.


  To demo the debug package, we double-click to execute resguard ReleaseAfter executing the command, we can find the obfuscated Apk in the /build/output/apk/ debug /AndResGuard_{apk_name}/ folder in the app directory:


Double click to view the apk:


You can see that the res folder has changed to r, and the directory names in it have been obfuscated.


If you don't understand anything, just leave a comment below or send me a private message! !







Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325598203&siteId=291194637