Android : proguard-android.txt 解析

android sdk 在目錄 \sdk\tools\proguard\proguard-android.txt 下提供了默認的混淆配置,下面將介紹每一條規則的作用。

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

# 包名不混合大小寫
-dontusemixedcaseclassnames
# 不忽略非公共的庫類
-dontskipnonpubliclibraryclasses
# 輸出混淆日誌
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
# 不進行優化
-dontoptimize
# 不進行預檢驗
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

# 不混淆註解(註解不能被混淆)
-keepattributes *Annotation*
# 不混淆指定類
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

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

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
# 不混淆繼承View的所有類的set和get方法
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
# 不混淆繼承Activity的所有類的中的參數類型爲View的方法
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
# 不混淆枚舉類型的values和valueOf方法
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

# 不混淆繼承Parcelable的所有類的CREATOR
-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

# 不混淆R類中所有static字段
-keepclassmembers class **.R$* {
    public static <fields>;
}

# 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.
# 忽略兼容庫的所有警告
-dontwarn android.support.**

# Understand the @Keep support annotation.
# 不混淆指定的類及其類成員
-keep class android.support.annotation.Keep

# 不混淆使用註解的類及其類成員
-keep @android.support.annotation.Keep class * {*;}

# 不混淆所有類及其類成員中的使用註解的方法
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

# 不混淆所有類及其類成員中的使用註解的字段
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

# 不混淆所有類及其類成員中的使用註解的初始化方法
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

從上面的解析可以看出,proguard-android.txt 中只提供了基本的內容,在實際使用 ProGuard 時通常需要配置大量的規則。例如,引用的第三方庫的混淆配置,不混淆自定義控件,不混淆反射的類等。


參考:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章