To enable code obfuscation, the simplest configuration in the build.gradle file might look like this:

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
Copy the code

The “proguardFiles” property is used to configure the “obturate rule file”, while the “ProGuard-rules.pro”, one of the default files, is automatically added when we create a new module and usually contains no rules and needs to be added ourselves.

We add and maintain obfuscation rules on a daily basis, usually directly in the ProGuard-rules.pro file.

The downside of this is:

  • As more modules are introduced, the bloated ProGuard-rules.pro file is difficult to maintain
  • All kinds of rules are filled in. When problems arise due to code confusion, it is difficult to locate the specific confusion rules and fix them

The key to solving this problem is the proguardFiles property, which allows us to add additional obfuscation rules by adding multiple obfuscation rule files.

We can follow the single responsibility principle and split the obfuscation rule file into the following three files according to the general classification of obfuscation rules:

  • Common obfuscation rules file (Proguard-common.pro)
# -- -- -- -- -- -- -- -- -- -- - confusion of general basic configuration rules -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # specified not to ignore the public library class - dontskipnonpubliclibraryclasses # specified not to ignore the public libraries of the members of the class - dontskipnonpubliclibraryclassmembers # retained inherited from the Activity, Application and fragments subclass - keep these classespublic class * extends android.app.Fragment
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.ServiceThe following is omitted...Copy the code
  • Project obfuscation rules file (Proguard-rules.pro)
# -- -- -- -- -- -- -- -- -- -- - for the objective of the confusing rules -- -- -- -- -- -- -- -- -- -- -- -- -- -- # reserves the specified package and contains child package under the name of the class and class member variables and methods) don't be confused - keepclass com.xxx.database.constants. * *{*; } -keepclass com.xxx.database.beanextra. * *{*; } -keepclass com.arthenica.mobileffmpeg. * *{*; } # keep the specified class name and class members (variables and methods) from being confused -keepclass com.xxx.comp.dynamic.manager.AVUManager{*; } -keepinterface com.xxx.remote.util.MessageEntityConverter{*; } Below omit...Copy the code
  • Third-party SDK rule file (Proguard-third.pro)
# -- -- -- -- -- -- -- -- -- -- - the third party libraries of confusing rules -- -- -- -- -- -- -- -- -- -- -- -- -- -- # OkHttp - dontwarn com. Squareup. Okhttp3. * * - keepclass com.squareup.okhttp3. * *{ *;}
-dontwarn okio.**

# Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$* *{
**[] $VALUES;
public *;
}
-keep class com.bumptech. * *{*; } Below omit...Copy the code

Then add the other two obfuscation rule files to the proguardFiles property:

    buildTypes {
        release {
            minifyEnabled tue
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro', 'proguard-common.pro', 'proguard-third.pro'
        }
    }
Copy the code

The general obfuscation rules are basically unchanged. The third-party SDK obfuscation rules only need to be added when new class libraries are introduced. We usually maintain the obfuscation rules file of the project itself.

When there are problems caused by code confusion, we can directly check the confusion rules file of the project itself, quickly locate the specific confusion rules and fix them.

If it helps you, please don’t forget to like and follow oh, thank you ~


Hi, MY name is Zifeng Chuangchen. I have changed my name to IM Developer. Just as my profile says:

Based on the mobile terminal, the collection of “instant messaging”, “WebRTC”, “mobile development” and other content, is committed to creating a service for instant messaging enthusiasts technology sharing platform.

In the future, I will devote myself to pushing the content in these three aspects. Thank you for the company of those who still pay attention to these three aspects.

The content is one of my new attempt, because of the need to build a system of dry articles writing cycle is very long, and now the basic only fragments of spare time every day, but still want to keep a daily writing output, therefore used a similar “daily tips” this kind of form, hope I can help you practice to the day-to-day development quickly, improve the development efficiency.

Of course, the dry articles of the system will not be left behind. We will actively collect the content you are interested in according to the daily feedbacks and output it as a more systematic and complete article. Welcome to continue to pay attention to it.