Some common templates

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # for some basic instructions to add # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # code mixed compression ratio, Optimizationpasses 5 # OptimizationPasses 5 # optimizationPasses 5 # Blend to class called lowercase - dontusemixedcaseclassnames # specified not to ignore the public library classes - dontskipnonpubliclibraryclasses # this sentence can make us generate mapping file # after the confusion of the project Contains the name of the class - > confusion after the mapping relationship between the name of the class - verbose # specified not to ignore the class members of the public library - dontskipnonpubliclibraryclassmembers # don't do the divverify, preverify is one of the four steps of proguard, Android does not require PreVerify, and removing this step can speed up obfuscation. - DontPreVerify # Preserve annotations without obfuscating - KeepAttributes *Annotation*,InnerClasses # Avoid obfuscating generics - KeepAttributes Signature # Throw an exception when keep code line number - keepattributes SourceFile, LineNumberTable # specified confusion is to use an algorithm, the parameter is a filter behind the # this filter is recommended by the Google algorithm, generally do not change - optimizations. code/simplification/cast,! field/*,! Class/done / * # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Android development in some of the need to keep the part # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # to retain the four major components, we use -keep public class * extends Android.app. Activity -keep public class * extends android.app.Appliction -keep public class * extends android.app.Service -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. The view - keep public class com. Android. Vending. Licensing. ILicensingService # support Keep class android.support.** {*; ** -keep public class * extends Android.support.v7.** -keep Public class * extends android. Support. The annotation. * * # retain R the following resources - keep class * * * R ${*; } # retain local native method is not be confused - keepclasseswithmembernames class * {native < the methods >; } # Keep the method arguments in the Activity as view methods, Keepclassmembers class extends Android.app.activity {public void keepClassMembers extends Android.app.activity {public void keepClassMembers extends Android.app.activity {public void keepClassMembers extends Android.app.activity {public void keepClassMembers extends Android.app.activity {public void keepClassMembers extends Android.app.activity *(android.view.View); {public static **[] values();} # keepClassMembers enum * {public static **[] values(); public static ** valueOf(java.lang.String); } # public class extends android.view.View {*** get*(); void set*(***); public <init>(android.content.Context); public <init>(android.content.Context, android.util.AttributeSet); public <init>(android.content.Context, android.util.AttributeSet, int); } # keep class * implements android.os.Parcelable {public static final android.os.Parcelable$Creator *; } # keep Serializable classes from being confused-keepNames * implements java.io.Serializable -keepclassmembers * implements java.io.Serializable { static final long serialVersionUID; private static final java.io.ObjectStreamField[] serialPersistentFields; ! static ! transient <fields>; ! private <fields>; ! private <methods>; private void writeObject(java.io.ObjectOutputStream); private void readObject(java.io.ObjectInputStream); java.lang.Object writeReplace(); java.lang.Object readResolve(); Keepclassmembers class * {void *(**On*Event); keepclassMembers class * {void *(**On*Event); void *(**On*Listener); } # webView handling, project did not use to the webView can be ignored - keepclassmembers class FQCN. Of the javascript. Interface. For. WebView {public *; } -keepclassmembers class * extends android.webkit.webViewClient { public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap); public boolean *(android.webkit.WebView, java.lang.String); } -keepclassmembers class * extends android.webkit.webViewClient { public void *(android.webkit.webView, java.lang.String); } # js -keepattributes JavascriptInterface -keep class android.webkit.JavascriptInterface { *; } -keepclassmembers class * { @android.webkit.JavascriptInterface <methods>; } # @Keep -keep,allowobfuscation @interface android.support.annotation.Keep -keep @android.support.annotation.Keep class  * -keepclassmembers class * { @android.support.annotation.Keep *; }Copy the code

Some custom templates

# Wildcard * matches any length of character, but does not contain the package name delimiter (.) # wildcard ** matches characters of any length and contains the package name delimiter (.). Keep public class com.jasonwu.demo.Test {*; } # keep class com.jasonwu.demo.test.** {*; } # keep public class * com.jasonwu.demo.test {*; } # keep public class **.*model*.** {*; } # don't confuse the implementation of an interface - keep class * implements com. Jasonwu. Demo. TestInterface {*; } # keepclassMembers class com.jasonwu.demo.test {public <init>(); } # keepClassMembers class com.jasonwu.demo.Test {public void Test (java.lang.string); }Copy the code

Add separate obfuscated configuration to aar

Android {··· defaultConfig {··· · consumerProguardFile'proguard-rules.pro'}...}Copy the code

Check for confusion and track anomalies

When Proguard is enabled, Proguard outputs the following files each build:

Dump. TXT describes the internal structure of all class files in APK.

Mapping.txt provides conversions between original and obfuscated class, method, and field names.

Seeds.txt lists classes and members that have not been obfuscated.

Usage.txt lists the code removed from APK.

These files are stored in the/build/outputs/mapping/release /. We can look at seeds.txt to see if it’s the one we want to keep, and usage.txt to see if it’s deleted incorrectly. The mapping.txt file is very important, because part of our code has been renamed, if there is a bug in this part, the classes or members in the corresponding exception stack information have also been renamed, so it is difficult to locate the problem. We can use the retrace script (retrace.bat on Windows; Retrace.sh) on Mac/Linux. It is located in the /tools/proguard/ directory. This script uses the mapping.txt file and your exception stack file to generate an unscrambled exception stack file so you can see where the problem is. The syntax for using retrace is as follows:

retrace.bat|retrace.sh [-verbose] mapping.txt [<stacktrace_file>]
Copy the code

conclusion

The above is a brief introduction to common confusion problems in Android, there are still many deficiencies welcome to add, WISH everyone a happy New Year, technology getting better and better, fighting