Android Some Common Confusions Proguard

  • 2021-12-11 09:09:40
  • OfStack

1 Some common templates


#############################################
#
# 对于1些基本指令的添加
#
#############################################
# 代码混淆压缩比,在 0~7 之间,默认为 5,1般不做修改
-optimizationpasses 5

# 混合时不使用大小写混合,混合后的类名为小写
-dontusemixedcaseclassnames

# 指定不去忽略非公共库的类
-dontskipnonpubliclibraryclasses

# 这句话能够使我们的项目混淆后产生映射文件
# 包含有类名->混淆后类名的映射关系
-verbose

# 指定不去忽略非公共库的类成员
-dontskipnonpubliclibraryclassmembers

# 不做预校验,preverify 是 proguard 的4个步骤之1,Android 不需要 preverify,去掉这1步能够加快混淆速度。
-dontpreverify

# 保留 Annotation 不混淆
-keepattributes *Annotation*,InnerClasses

# 避免混淆泛型
-keepattributes Signature

# 抛出异常时保留代码行号
-keepattributes SourceFile,LineNumberTable

# 指定混淆是采用的算法,后面的参数是1个过滤器
# 这个过滤器是谷歌推荐的算法,1般不做更改
-optimizations !code/simplification/cast,!field/*,!class/merging/*


#############################################
#
# Android开发中1些需要保留的公共部分
#
#############################################

# 保留我们使用的4大组件,自定义的 Application 等等这些类不被混淆
# 因为这些子类都有可能被外部调用
-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.View
-keep public class com.android.vending.licensing.ILicensingService


# 保留 support 下的所有类及其内部类
-keep class android.support.** { *; }

# 保留继承的
-keep public class * extends android.support.v4.**
-keep public class * extends android.support.v7.**
-keep public class * extends android.support.annotation.**

# 保留 R 下面的资源
-keep class **.R$* { *; }

# 保留本地 native 方法不被混淆
-keepclasseswithmembernames class * {
  native <methods>;
}

# 保留在 Activity 中的方法参数是view的方法,
# 这样以来我们在 layout 中写的 onClick 就不会被影响
-keepclassmembers class * extends android.app.Activity {
  public void *(android.view.View);
}

# 保留枚举类不被混淆
-keepclassmembers enum * {
  public static **[] values();
  public static ** valueOf(java.lang.String);
}

# 保留我们自定义控件(继承自 View)不被混淆
-keep 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);
}

# 保留 Parcelable 序列化类不被混淆
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}

# 保留 Serializable 序列化的类不被混淆
-keepnames class * implements java.io.Serializable
-keepclassmembers class * 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();
}

# 对于带有回调函数的 onXXEvent、**On*Listener 的,不能被混淆
-keepclassmembers class * {
  void *(**On*Event);
  void *(**On*Listener);
}

# webView 处理,项目中没有使用到 webView 忽略即可
-keepclassmembers class fqcn.of.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 *;
}

1 Some custom templates


#  Wildcard character * Matches any length character without package name separator (.)
#  Wildcard character ** Matches any length character and contains the package name separator (.)

#  Do not confuse a class 
-keep public class com.jasonwu.demo.Test { *; }

#  Do not confuse all classes of a package 
-keep class com.jasonwu.demo.test.** { *; }

#  Do not confuse subclasses of a class 
-keep public class * com.jasonwu.demo.Test { *; }

#  Do not confuse all class names that contain  ``model``  Class and its members 
-keep public class **.*model*.** {*;}

#  Do not confuse the implementation of an interface 
-keep class * implements com.jasonwu.demo.TestInterface { *; }

#  Do not confuse the construction method of a class 
-keepclassmembers class com.jasonwu.demo.Test { 
 public <init>(); 
}

#  Do not confuse specific methods of a class 
-keepclassmembers class com.jasonwu.demo.Test { 
 public void test(java.lang.String); 
}

Adding independent obfuscation configuration in aar


android {
   ... 
  defaultConfig {
     ... 
    consumerProguardFile 'proguard-rules.pro'
  }
   ... 
}

Check for confusion and trace exceptions

When Proguard is turned on, ProGuard outputs the following files on each build:

dump. txt describes the internal structure of all class files in APK.

mapping. txt provides conversion between original and obfuscated class, method, and field names.

seeds. txt lists classes and members that are not obfuscated.

usage. txt Lists the code removed from APK.

These files are saved in/build/outputs/mapping/release/. We can check seeds. txt to see if we need to keep it, and usage. txt to see if there is any code deleted by mistake. mapping. txt file is very important, because part of our code is renamed. If bug occurs in this part, the class or member in the corresponding exception stack information is also renamed, which makes it 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 unobfuscated exception stack file so you can see what went wrong. The syntax for using the retrace tool is as follows:


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

Check for confusion and trace exceptions

When Proguard is turned on, ProGuard outputs the following files on each build:

dump. txt describes the internal structure of all class files in APK.

mapping. txt provides conversions between primitive and obfuscated class, method, and field names.

seeds. txt lists classes and members that are not obfuscated.

usage. txt lists the code removed from APK.

These files are saved in/build/outputs/mapping/release/. We can see if seeds. txt is what we need to keep, and usage. txt to see if there is any code deleted by mistake. mapping. txt file is very important, because part of our code is renamed. If bug appears in this part, the class or member in the corresponding exception stack information is also renamed, which makes it 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 unobfuscated exception stack file so you can see what went wrong. The syntax for using the retrace tool is as follows:


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

Conclusion

The above is a brief introduction to the common confusion in Android, and there are still many shortcomings. Welcome to add a happy New Year to everyone, and the technology is getting better and better. fighting

The above is Android 1 some commonly used confusion Proguard details, more information about Android commonly used confusion Proguard please pay attention to other related articles on this site!


Related articles: