You have to understand the confusion --Android

Author  : CoderThc

Introduction confusion

You have to understand the confusion --Android

Proguard is a Java class file compression, optimizer, obfuscator, preverifier. Compressing stage will detect and remove unused classes, fields, methods, and properties. Optimization link will analyze and optimize bytecode methods. Confusion link will use short variable meaningless to rename classes, variables, methods. These steps make the code more streamlined, more efficient, but also more difficult to reverse (crack).

After the confusion generated by default in the / project directory app / build / outputs / mapping release (debug) a mapping.txt file, which is confusing rules, we can put this document in accordance with the code obfuscated reverse thrust back to the source of this code, Therefore, this document is very important to pay attention to protect. In principle, the more chaotic the more confused after the code is no better laws, but in some places we are to avoid confusion, or else the program will run error.

Proguard confusion introduce common operations

  • Followed by remarks Proguard official documents, other show can view their own operations
  • Compression (Shrinking): enabled by default, for reducing the volume of applications, and members of the class to remove unused and will be performed again after the operation in the optimization performed (as it may be exposed to members of classes and optimization is not used again )

    -dontshrink #关闭压缩
  • Optimization (Optimization): enabled by default, in the implementation of the optimization bytecode level, allowing applications to run faster
 -dontoptimize #关闭优化
 -optimizationpasses n #表示proguard对代码进行迭代优化的次 
 数,Android一般为5
  • Confusion (Obfuscation): enabled by default, to increase the difficulty of decompilation, classes and class members will be randomly named, unless the use of keep protection
-dontobfuscate #关闭混淆
  • A star that just keep classes within the package, and the package under the sub-category name will still be confused
-keep class com.thc.test.*
  • Two stars indicate the class name contained in the present package and subpackages are held;
-keep class com.thc.test.**

(The latter two remain above categories, you will find the class name, although not confused, but inside the specific methods and variable naming or changed)

  • May be maintained under the class name of the package, they can be kept inside the content class is not to be confused;
-keep class com.thc.test.*{*;}
  • Either to keep classes within the package and the child package, and can keep the contents inside the class not to be confused;
-keep class com.thc.test.**{*;}
  • Maintaining a class name is not to be confused (but internal content will be confused)
-keep class com.xlpay.sqlite.cache.BaseDaoImpl
  • All contents remain inside the class name and a class will not be confused
-keep class com.xlpay.sqlite.cache.BaseDaoImpl{*;}
  • Holding class specific content, and not all the content can be used as follows:
-keep class com.thc.gradlestudy.MyProguardBean{
 <init>; #匹配所有构造器
 <fields>;#匹配所有域
 <methods>;#匹配所有方法
 }

Above to hold all the constructors MyProguardBean this class, variables and methods

  • Can further specify the content not to be confused in <fields> or <methods> preceded by the private, public, native, etc.
-keep class com.xlpay.sqlite.cache.BaseDaoImpl{
 public <methods>;#保持该类下所有的共有方法不被混淆
 public *;#保持该类下所有的共有内容不被混淆
 private <methods>;#保持该类下所有的私有方法不被混淆
 private *;#保持该类下所有的私有内容不被混淆
 public <init>(java.lang.String);#保持该类的String类型的构造方法
 }
  • After the addition of method parameters, limited to a particular method (tested: only constructor can be confusing)
-keep class com.thc.gradlestudy.MyProguardBean{
 public <init>(String);
 }
  • To retain the inner class a class need not to be confused with the $ symbol
#保持ProguardTest中的MyClass不被混淆
 -keep class com.xlpay.sqlite.cache.ProguardTest$MyClass{*;}
  • Java basic rules used to protect a particular class is not to be confused, such as using extends, implement these and other Java rules, as follows: holding Android underlying components and classes do not confuse
-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.content.BroadcastReceiver
 -keep public class * extends android.content.ContentProvider
 -keep public class * extends android.view.View
  • If you do not keep the name of the class, only a specific method in this class need not be confused holding, use keepclassmembers, rather than keep, because the method will keep maintaining the class name.
#保持ProguardTest类下test(String)方法不被混淆
 -keepclassmembernames class com.xlpay.sqlite.cache.ProguardTest{
 public void test(java.lang.String);
 }
  • If you have a member, retention classes and class members
-keepclasseswithmembernames class com.xlpay.sqlite.cache.ProguardTest

Precautions

  • jni method can not be confused, because the method is native to the full package name of the class defined method names can not be modified, otherwise can not be found;
#保持native方法不被混淆
 -keepclasseswithmembernames class * { 
 native <methods>; 
 }
  • Reflection class used to note when confusion: the class name and method can be used as long as they reflected, does not need to be reflected to the entire class is held

  • The AndroidMainfest class not to be confused, so the next four components and subclasses Application Framework layer and all classes Default Do not be confused. View custom default will not be confused
  • When interacting with the server, using GSON, when fastjson other side frame resolution service data, JSON written as not to obscure the object class, or can not be resolved to JSON corresponding to the object;
  • When using third party open source libraries or other references to third-party SDK package, if you have special requirements, but also need to add confusing rules corresponding file in confusion;
  • JS helpful to call the WebView also need to make sure to write the interface method does not confuse cause and as the first;
  • Parcelable subclass Creator and static member variables are not confused, or will cause Android.os.BadParcelableException abnormal;
-keep class * implements Android.os.Parcelable { 
 # 保持Parcelable不被混淆 
 public static final Android.os.Parcelable$Creator *;
 }
  • Note the following two methods to avoid confusion when using enum type, enum class because of the particularity of the following two methods will be reflected to call, see the second rule.
-keepclassmembers enum * { 
 public static **[] values(); 
 public static ** valueOf(java.lang.String); 
 }
  • Recommendation: In addition to publishing an application to set minifyEnabled ture, you should also set zipAlignEnabled is true, like the Google Play mandatory application developers must be uploaded through the zipAlign, zipAlign allows installation package of resources aligned on 4-byte, this reduces memory consumption application at runtime.

Confusion Record

Examples of use: class classA with and classB, plus a variety of results in the case of confusion:

  1. If classA not keep, you will not see classA class files
  2. If classA not keep, classB is maintained, while classB references to classA, this time to see the confused classA class files, such as a display
  3. If classA by reflection, get to classB, then classB class name and the reflection method used must keep living
  4. jar package confusion, exposed parameter class, a method, the method needs to keep live
  5. Note: Demo project relies dependent channels millet, millet depend in turn depends on the Common, but not to obfuscate Common millet channels confusion, less dependent on millet used to Common classes need to keep in live

Android video learning PDF + Architecture + + source code documentation interview notes


Thank you for the ability to sit through

Xiao Bian here also share a copy of your collection of finishing Android studying architecture PDF + Video + Interview + document source notes , as well as advanced technical architecture Advanced Brain Mapping, Android interview with thematic development, advanced materials advanced architecture to help you learn ascended into order, saving everyone time online in search of information to learn, you can also share with close friends studying together

If you have a need, you can point like + comment , concern me , and then add my VX: 15388039515 I sent you
(or concerned about micro-channel public number "Android Development House" reply [information] for free to receive)
You have to understand the confusion --Android

You have to understand the confusion --Android

You have to understand the confusion --Android

You have to understand the confusion --Android

Guess you like

Origin blog.51cto.com/14573572/2452000