Open the confusion

buildTypes {
    release {
        minifyEnabled true. }}Copy the code

Component configuration

Difference between consumerProguardFiles and proguardFiles:

  • The ProGuard configured on consumerProguardFiles will enter the AAR, while the ProGuard configured on proguardFiles will not
  • The proguardFiles configuration only applies to the library code. It takes effect when the aar is compiled and published. When the library relies on the APP module as a module, The ProGuard configured by consumerProguardFiles will be appended to the App module’s ProGuard configuration

The component obfuscation rules are written in the proguard-rules.pro file

Ask the component owner to write specific obfuscation rules

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

Common commands

For more usage, see the official website

The command role
dontwarn Often used with keep, especially when introducing a library, in order to ignore the library’s warnings and ensure that the build runs properly
keep Preserves the class and its members to prevent them from being renamed or removed
keepnames Retain the class and its members to prevent renaming. Members that are not referenced will be removed
keepclassmembers Only members of the class are retained to prevent renaming or removal
keepclassmembernames Only members of the class are retained to prevent renaming. Members without references will be removed
keepclasseswithmembers Retain the class and member that owns the member to prevent renaming or removal
keepclasseswithmembernames Keep the class and member that owns the member to prevent renaming
[hold command] [class] {[member]}Copy the code

“Class” represents a class-related qualification, and it will eventually locate some classes that meet that qualification. Its contents can be used:

  • Concrete classes
  • Access modifier (public, protected, private)
  • Wildcard *, which matches characters of any length without the package name separator (.)
  • Wildcard ** that matches any character length and contains the package name separator (.)
  • Extends, which specifies the base class of a class
  • Implement, matches a class that implements an interface
  • $, inner class

“Member” represents a qualification related to a class member, and it will eventually locate some class member that meets that qualification. Its contents can be used:


  • matches all constructors

  • matches all member variables

  • Matches all methods
  • Wildcard *, which matches characters of any length without the package name separator (.)
  • Wildcard ** that matches any character length and contains the package name separator (.)
  • The wildcard character is ***, which matches any parameter type
  • … To match any type parameter of any length. Such as void test (…). We can match any void test(String a) or void test(int a, String b) method.
  • Access modifier (public, protected, private)

General configuration

Generally written in the main project

# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- basic instruction area -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- # code confusion, compression ratio between 0 ~ 7, the default is 5, - OptimizationPasses 5 # does not use case blending when blending Blend to class called lowercase - dontusemixedcaseclassnames # specified not to ignore the public library classes - dontskipnonpubliclibraryclasses # specified not to ignore the class members of the public library - dontskipnonpubliclibraryclassmembers # after this sentence can confuse our project # generate mapping file contains the name of the class - > confusion after the mapping relationship between the name of the class - verbose # don't do check in advance, Preverify is one of the four steps in ProGuard. Android doesn't require preverify, and removing it will speed up confusion. -dontpreverify -printmapping proguardmapping. TXT # keep annotations without obfuscation -keepattributes *Annotation*,InnerClasses # avoid obfuscation of generics - keepattributes Signature # when they throw an exception code line number - keepattributes SourceFile, LineNumberTable # specified confusion is to use an algorithm, # Optimizations is an algorithm recommended by Google and is not subject to change -optimizations! code/simplification/cast,! field/*,! class/merging/* # ---------------------------------------------------------------------------- # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- the default reserves -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- keep public class * extends android.app.Activity -keep public class * extends android.app.Application -keep public class * extends android.app.Service -keep public class * extends android.app.View -keep public class * extends android.content.BroadcastReceiver -keep public class * extends android.content.ContentProvider -keep public class * extends android.app.backup.BackupAgentHelper -keep public class * extends android.preference.Preference -keep public class * extends android.view.View -keep public class * extends java.lang.Throwable {*; } -keep public class * extends java.lang.Exception {*; } -keep class android.support.** {*; } # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- don't optimize Parcelable implementation class -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- keep class implements android.os.Parcelable {*; } # -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- don't optimize the serialization implementation class -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- keep class implements java.io.Serializable { *; } -keepclassmembers class * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); }Copy the code

The sample

  • Do not confuse a class
-keep public class com.kotlin.Test {*; }Copy the code
  • Do not confuse all classes of a package
-keep public class com.kotlin. * *{*; }Copy the code
  • Do not confuse subclasses of a class
-keep public class * extends com.kotlin.Test {*; }Copy the code
  • Do not confuse the inner classes of a class
-keep class com.kotlin.Test$* {*; }Copy the code
  • Do not confuse the implementation class of an interface
-keep public class * implements com.kotlin.Callback {*; }Copy the code
  • Do not confuse the constructor of a class
-keepclassmembers class com.kotlin.Test {
    public <init> (); }Copy the code
  • Do not confuse specific methods of a class
-keepclassmembers class com.kotlin.Test {
    public void test(java.lang.String);
}
Copy the code