Android confuse the underlying mechanism

Confusion, the class names, method names, variables and other members rename the short name of the meaningless, to increase the difficulty of reverse engineering, while reducing the package size by removing unused classes.

proguard-android.txt: Sdk confusion default configuration
proguard-rules.pro: special treatment for this module confuse made

Confuse basic grammar

Confuse the syntax mainly used to define the code does not need to be confused.

There are a lot of confusing grammar, syntax and below lists the common use:

  • Not to be confused specified class
-keep class com.xx.Test{*;}
  • All classes / not to obscure the specified path interface
-keep class com.xx.model.** { *; }
  • Do not confuse the function of a particular class
-keepclassmembers class com.xx.Test{
    public void 函数名(java.lang.String);
}

For the constructor:

-keepcalssmembers clsss com.xx.Test{
    public <init>(java.lang.String)
}
  • Do not confuse a class / subclass of interface / implementation
-keep public class * extend com.xx.Test

-keep class * implements com.xx.Test {
  public static final com.xx.Test$Creator *;
}
  • If the project confusion, will be in build/outputs/mapping/xxgenerating the following file directory:

mapping.txt: Confused mapping between before and after;

seeds.txt: Not confused and class members;

usage.txt: Apk removed from the code; (confusion will optimize the code, will remove some unused class)

dump.txt: All the internal structure of the class.

When using the mapping.txt, receive an exception report, you can locate the source of the problem, but it should be noted that before the package will cover every confusion mapping.txt.

  • Other (optional)
-ignorewarnings # 忽略警告,避免打包时某些警告出现
-optimizationpasses 5 # 指定代码的压缩级别
-dontusemixedcaseclassnames  # 不使用大小写混合类名
-dontskipnonpubliclibraryclasses # 不混淆第三方 jar 中的非 public classes
-dontskipnonpubliclibraryclassmembers #不混淆第三方 jar 的非 public class
-dontpreverify   # 混淆时不做预校验
-dontshrink # 禁止删除无用包
-dontoptimize # 不启用优化、合并代码
-verbose    # 混淆时记录日志
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆时所采用的算法
-renamesourcefileattribute SourceFile 

More configurable options can be found at Android code obfuscation Item Details

Notes confusion

Because the confusion will be renamed the class names, method names, variable names, when some of the code requires original name call, the code after the class name can not be found because of confusion can lead to crash. The following situation can not / do not recommend confusion (confusion as basic rule configuration file in confusion, extra configuration will not cause compilation to fail):

1 . Class variable reflective or used;

2 .bean objects;

3 . Four assembly; (four components need to register the AndroidManifest)

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Fragment
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v4.app.FragmentActivity
-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.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService

4 . Annotation; (the time for running the compiler reflection)

-keepattributes *Annotation*

5 . Enumeration;

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

6 .JNI call Java methods;

7 .Java Native method invocation;

-keepclasseswithmembernames class * {
    native <methods>;
}

8 .JS call Java methods;

-keepattributes *JavascriptInterface*

9 .WebView in the JavaScript method call must not be confused;

-keepclassmembers class fqcn.of.javascript.interface.for.Webview {
    pub1ic *;
}

-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);
}

10 . Third-party libraries using its own confusing rules, or simply all do not be confused;

-dontwarn com.xx.xx.**
-keep class com.xx.xx.** { *;}

. 11 .Parcelable sequence of related classes;

-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

-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();
}

12 .Gson;

-keep class com.google.gson.** { *; }
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.google.** {
    <fields>;
    <methods>;
}
-dontwarn com.google.gson.**

Part of self-reference "Android componentized architecture," a book, and Network access.

Reproduced in: https: //www.jianshu.com/p/266774a713c0

Guess you like

Origin blog.csdn.net/weixin_34257076/article/details/91220508