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

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

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

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

 

2、标准proguard.cfg文件内容
参考android标准,修改如下:

# see http://sourceforge.net/tracker/?func=detail&aid=2787465&group_id=54750&atid=474707
-optimizations !code/simplification/arithmetic
-optimizations !code/simplification/cast
-allowaccessmodification

# To prevent name conflict in incremental obfuscation.
-useuniqueclassmembernames

# dex does not like code run through proguard optimize and preverify steps.
-dontoptimize
-dontpreverify

# Don't obfuscate. We only need dead code striping.
# -dontobfuscate

# Add this flag in your package's own configuration if it's needed.
#-flattenpackagehierarchy

# Some classes in the libraries extend package private classes to chare common functionality
# that isn't explicitly part of the API
-dontskipnonpubliclibraryclasses -dontskipnonpubliclibraryclassmembers

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# class$ methods are inserted by some compilers to implement .class construct,
# see http://proguard.sourceforge.net/manual/examples.html#library
-keepclassmembernames class * {
    java.lang.Class class$(java.lang.String);
    java.lang.Class class$(java.lang.String, boolean);
}

# Keep classes and methods that have the guava @VisibleForTesting annotation
-keep @com.google.common.annotations.VisibleForTesting class *
-keepclassmembers class * {
@com.google.common.annotations.VisibleForTesting *;
}

# Keep serializable classes and necessary members for serializable classes
# Copied from the ProGuard manual at http://proguard.sourceforge.net.
-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 void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

# Please specify classes to be kept explicitly in your package's configuration.
# -keep class * extends android.app.Activity
# -keep class * extends android.view.View
# -keep class * extends android.app.Service
# -keep class * extends android.content.BroadcastReceiver
# -keep class * extends android.content.ContentProvider
# -keep class * extends android.preference.Preference
# -keep class * extends android.app.BackupAgent

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

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
# See proguard-android.txt in the SDK package.
-dontwarn android.support.**


源文件见<android_root>/build/core/proguard.flags , 将14行 -dontobfuscate解除注释。

 

3、常用proguard.cfg代码段

不混淆某类的构造方法,需指定构造函数的参数类型,如JSONObject

-keepclassmembers class cn.trinea.android.common.service.impl.ImageCache {
   public <init>(int);
}


不混淆某个包所有类或某个类class、某个接口interface, 不混淆指定类则把**换成类名

-keepclasscn.trinea.android.common.**{*;}


不混淆指某个方法,*可换成指定的方法或类名

-keepclassmembers class cn.trinea.android.common.service.impl.ImageCache {
   public boolean get(java.lang.String, android.view.View);
}

 

不混淆Parcelable的子类,防止android.os.BadParcelableException

-keepclass*implementsandroid.os.Parcelable{
  publicstaticfinalandroid.os.Parcelable$Creator*;
}


添加android-support-v4.jar依赖包

-libraryjars   libs/android-support-v4.jar
-dontwarn android.support.v4.**    
-keep class android.support.v4.** { *; }  
-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中添加

-assumenosideeffectsclassandroid.util.Log{
    publicstatic***d(...);
    publicstatic***v(...);
}

表示Log.d和Log.v代码无副作用,在proguard时会被从源码中remove掉,这样release模式(正式发布)就不会打印日志了,而debug模式(平常调试)照常打印,不用修改一点代码大赞吧,嘿嘿*_^




转载地址:http://www.trinea.cn/android/proguard-grammar/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值