Proguard语法及常用proguard.cfg代码段

本文主要ProGuard常用语法、标准proguard.cfg文件内容、常用proguard.cfg代码段及proguard与log level结合解决debug模式日志问题。关于ProGuard的作用、使用及bug分析可见ProGuard的作用、使用及bug分析。

  1、ProGuard的常用语法
 
  1.  -libraryjars class_path 应用的依赖包,如android-support-v4
  2.   -keep [,modifier,...] class_specification 不混淆某些类
  3.   -keepclassmembers [,modifier,...] class_specification 不混淆类的成员
  4.   -keepclasseswithmembers [,modifier,...] class_specification 不混淆类及其成员
  5.   -keepnames class_specification 不混淆类及其成员名
  6.   -keepclassmembernames class_specification 不混淆类的成员名
  7.   -keepclasseswithmembernames class_specification 不混淆类及其成员名
  8.   -assumenosideeffects class_specification 假设调用不产生任何影响,在proguard代码优化时会将该调用remove掉。如system.out.println和Log.v等等
  9.   -dontwarn [class_filter] 不提示warnning
复制代码

  关于proguard更多语法可见:http://proguard.sourceforge.net/index.html#manual/usage.htm l

  2、标准proguard.cfg文件内容

  参考android标准,修改如下:

  标准proguard.cfg文件内容
  
  1. XHTML
  2.   # see http://sourceforge.net/tracker/?func=detail&aid=2787465&group_id=54750&atid=474707
  3.   -optimizations !code/simplification/arithmetic
  4.   -optimizations !code/simplification/cast
  5.   -allowaccessmodification
  6.   # To prevent name conflict in incremental obfuscation.
  7.   -useuniqueclassmembernames
  8.   # dex does not like code run through proguard optimize and preverify steps.
  9.   -dontoptimize
  10.   -dontpreverify
  11.   # Don't obfuscate. We only need dead code striping.
  12.   # -dontobfuscate
  13.   # Add this flag in your package's own configuration if it's needed.
  14.   #-flattenpackagehierarchy
  15.   # Some classes in the libraries extend package private classes to chare common functionality
  16.   # that isn't explicitly part of the API
  17.   -dontskipnonpubliclibraryclasses -dontskipnonpubliclibraryclassmembers
  18.   # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
  19.   -keepclassmembers enum * {
  20.   public static **[] values();
  21.   public static ** valueOf(java.lang.String);
  22.   }
  23.   # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
  24.   -keepclasseswithmembernames class * {
  25.   native ;
  26.   }
  27.   # class$ methods are inserted by some compilers to implement .class construct,
  28.   # see http://proguard.sourceforge.net/manual/examples.html#library
  29.   -keepclassmembernames class * {
  30.   java.lang.Class class$(java.lang.String);
  31.   java.lang.Class class$(java.lang.String, boolean);
  32.   }
  33.   # Keep classes and methods that have the guava @VisibleForTesting annotation
  34.   -keep @com.google.common.annotations.VisibleForTesting class *
  35.   -keepclassmembers class * {
  36.   @com.google.common.annotations.VisibleForTesting *;
  37.   }
  38.   # Keep serializable classes and necessary members for serializable classes
  39.   # Copied from the ProGuard manual at http://proguard.sourceforge.net.
  40.   -keepnames class * implements java.io.Serializable
  41.   -keepclassmembers class * implements java.io.Serializable {
  42.   static final long serialVersionUID;
  43.   private static final java.io.ObjectStreamField[] serialPersistentFields;
  44.   !static !transient ;
  45.   private void writeObject(java.io.ObjectOutputStream);
  46.   private void readObject(java.io.ObjectInputStream);
  47.   java.lang.Object writeReplace();
  48.   java.lang.Object readResolve();
  49.   }
  50.   # Please specify classes to be kept explicitly in your package's configuration.
  51.   # -keep class * extends android.app.Activity
  52.   # -keep class * extends android.view.View
  53.   # -keep class * extends android.app.Service
  54.   # -keep class * extends android.content.BroadcastReceiver
  55.   # -keep class * extends android.content.ContentProvider
  56.   # -keep class * extends android.preference.Preference
  57.   # -keep class * extends android.app.BackupAgent
  58.   -keep class * implements android.os.Parcelable {
  59.   public static final android.os.Parcelable$Creator *;
  60.   }
  61.   # The support library contains references to newer platform versions.
  62.   # Don't warn about those in case this app is linking against an older
  63.   # platform version. We know about them, and they are safe.
  64.   # See proguard-android.txt in the SDK package.
  65.   -dontwarn android.support.**
  66.   # see http://sourceforge.net/tracker/?func=detail&aid=2787465&group_id=54750&atid=474707
  67.   -optimizations !code/simplification/arithmetic
  68.   -optimizations !code/simplification/cast
  69.   -allowaccessmodification
  70.   # To prevent name conflict in incremental obfuscation.
  71.   -useuniqueclassmembernames
  72.   # dex does not like code run through proguard optimize and preverify steps.
  73.   -dontoptimize
  74.   -dontpreverify
  75.   # Don't obfuscate. We only need dead code striping.
  76.   # -dontobfuscate
  77.   # Add this flag in your package's own configuration if it's needed.
  78.   #-flattenpackagehierarchy
  79.   # Some classes in the libraries extend package private classes to chare common functionality
  80.   # that isn't explicitly part of the API
  81.   -dontskipnonpubliclibraryclasses -dontskipnonpubliclibraryclassmembers
  82.   # For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
  83.   -keepclassmembers enum * {
  84.   public static **[] values();
  85.   public static ** valueOf(java.lang.String);
  86.   }
  87.   # For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
  88.   -keepclasseswithmembernames class * {
  89.   native ;
  90.   }
  91.   # class$ methods are inserted by some compilers to implement .class construct,
  92.   # see http://proguard.sourceforge.net/manual/examples.html#library
  93.   -keepclassmembernames class * {
  94.   java.lang.Class class$(java.lang.String);
  95.   java.lang.Class class$(java.lang.String, boolean);
  96.   }
  97.   # Keep classes and methods that have the guava @VisibleForTesting annotation
  98.   -keep @com.google.common.annotations.VisibleForTesting class *
  99.   -keepclassmembers class * {
  100.   @com.google.common.annotations.VisibleForTesting *;
  101.   }
  102.   # Keep serializable classes and necessary members for serializable classes
  103.   # Copied from the ProGuard manual at http://proguard.sourceforge.net.
  104.   -keepnames class * implements java.io.Serializable
  105.   -keepclassmembers class * implements java.io.Serializable {
  106.   static final long serialVersionUID;
  107.   private static final java.io.ObjectStreamField[] serialPersistentFields;
  108.   !static !transient ;
  109.   private void writeObject(java.io.ObjectOutputStream);
  110.   private void readObject(java.io.ObjectInputStream);
  111.   java.lang.Object writeReplace();
  112.   java.lang.Object readResolve();
  113.   }
  114.   # Please specify classes to be kept explicitly in your package's configuration.
  115.   # -keep class * extends android.app.Activity
  116.   # -keep class * extends android.view.View
  117.   # -keep class * extends android.app.Service
  118.   # -keep class * extends android.content.BroadcastReceiver
  119.   # -keep class * extends android.content.ContentProvider
  120.   # -keep class * extends android.preference.Preference
  121.   # -keep class * extends android.app.BackupAgent
  122.   -keep class * implements android.os.Parcelable {
  123.   public static final android.os.Parcelable$Creator *;
  124.   }
  125.   # The support library contains references to newer platform versions.
  126.   # Don't warn about those in case this app is linking against an older
  127.   # platform version. We know about them, and they are safe.
  128.   # See proguard-android.txt in the SDK package.
  129.   -dontwarn android.support.**
  130.   源文件见/build/core/proguard.flags , 将14行 -dontobfuscate解除注释。
复制代码

  3、常用proguard.cfg代码段

  不混淆某类的构造方法,需指定构造函数的参数类型,如JSONObject
  
  1. Java
  2.   -keepclassmembers class cn.trinea.android.common.service.impl.ImageCache {
  3.   public (int);
  4.   }
  5.   123-keepclassmembers class cn.trinea.android.common.service.impl.ImageCache {
  6.   public (int);
  7.   }
复制代码

  不混淆某个包所有类或某个类class、某个接口interface, 不混淆指定类则把**换成类名
  
  1. Java
  2.   -keep class cn.trinea.android.common.** { *; }
  3.   1-keep class cn.trinea.android.common.** { *; }
复制代码

  不混淆指某个方法,*可换成指定的方法或类名
 
  1.  Java
  2.   -keepclassmembers class cn.trinea.android.common.service.impl.ImageCache {
  3.   public boolean get(java.lang.String, android.view.View);
  4.   }
  5.   123-keepclassmembers class cn.trinea.android.common.service.impl.ImageCache {
  6.   public boolean get(java.lang.String, android.view.View);
  7.   }
复制代码

  不混淆Parcelable的子类,防止android.os.BadParcelableException
  
  1. Java
  2.   -keep class * implements android.os.Parcelable {
  3.   public static final android.os.Parcelable$Creator *;
  4.   }
  5.   123-keep class * implements android.os.Parcelable {
  6.   public static final android.os.Parcelable$Creator *;
  7.   }
复制代码

  添加android-support-v4.jar依赖包
  
  1. Java
  2.   -libraryjars libs/android-support-v4.jar
  3.   -dontwarn android.support.v4.**
  4.   -keep class android.support.v4.** { *; }
  5.   -keep interface android.support.v4.app.** { *; }
  6.   1234-libraryjars libs/android-support-v4.jar
  7.   -dontwarn android.support.v4.**
  8.   -keep class android.support.v4.** { *; }
  9.   -keep interface android.support.v4.app.** { *; }
复制代码

  4、proguard与log level结合解决debug模式Log问题

  常见的Android debug日志的打法是定义一个静态变量DEBUG_STATUS,如果为true,则打印log,否则不打印。对于release模式该变量为false,debug模式变量为true。这里介绍一个更好的方法,不用担心正式发布时一不小心错改了该变量。

  proguard的作用就是在release模式压缩、优化、混淆代码,其中的压缩和优化就包括去除不必要的代码,我们可以利用这一特性解决debug日志的问题,在proguard.cfg中添加
  
  1. Java
  2.   -assumenosideeffects class android.util.Log {
  3.   public static *** d(...);
  4.   public static *** v(...);
  5.   }
  6.   1234-assumenosideeffects class android.util.Log {
  7.   public static *** d(...);
  8.   public static *** v(...);
  9.   }
复制代码

  表示Log.d和Log.v代码无副作用,在proguard时会被从源码中remove掉,这样release模式(正式发布)就不会打印日志了,而debug模式(平常调试)照常打印,不用修改一点代码大赞吧,嘿嘿。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值