Android Studio ProGuard基礎語法和打包配置


這是對Android打包混淆所需配置所作的總結.javascript

ProGuard經常使用語法

下面列出一些經常使用的語法html

  • -libraryjars class_path 應用的依賴包,如android-support-v4java

  • -keep [,modifier,...] class_specification 不混淆某些類android

  • -keepclassmembers [,modifier,...] class_specification 不混淆類的成員git

  • -keepclasseswithmembers [,modifier,...] class_specification 不混淆類及其成員github

  • -keepnames class_specification 不混淆類及其成員名web

  • -keepclassmembernames class_specification 不混淆類的成員名json

  • -keepclasseswithmembernames class_specification 不混淆類及其成員名網絡

  • -assumenosideeffects class_specification 假設調用不產生任何影響,在proguard代碼優化時會將該調用remove掉。如system.out.println和Log.v等等app

  • -dontwarn [class_filter] 不提示warnning

更多關於ProGuard混淆規則和語法,直接上ProGuard官網

Android的混淆原則

Android混淆打包混淆的原則以下

  • 反射用到的類不混淆

  • JNI方法不混淆

  • AndroidMainfest中的類不混淆,四大組件和Application的子類和Framework層下全部的類默認不會進行混淆

  • Parcelable的子類和Creator靜態成員變量不混淆,不然會產生android.os.BadParcelableException異常

  • 使用GSON、fastjson等框架時,所寫的JSON對象類不混淆,不然沒法將JSON解析成對應的對象

  • 使用第三方開源庫或者引用其餘第三方的SDK包時,須要在混淆文件中加入對應的混淆規則

  • 有用到WEBView的JS調用也須要保證寫的接口方法不混淆

經常使用的混淆配置和解析

先看看下面Google默認配置的混淆文件,文件的位置:

AndroidStudio的安裝目錄\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
-keepclasseswithmembernames class * {//指定不混淆全部的JNI方法
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {//全部View的子類及其子類的get、set方法都不進行混淆
   void set*(***);
   *** get*();
}

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

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {//不混淆Enum類型的指定方法
    public static **[] values();
    public static ** valueOf(java.lang.String);
}
//不混淆Parcelable和它的子類,還有Creator成員變量
-keep class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator *;
}
//不混淆R類裏及其全部內部static類中的全部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.**//不提示兼容庫的錯誤警告

Google爲咱們提供好的混淆代碼如上,可是每每咱們還須要添加本身的混淆規則,這是由於咱們可能會遇到如下幾種狀況:

  • 代碼中使用了反射,如一些ORM框架的使用

  • 使用GSON、fastjson等JSON解析框架所生成的對象類

  • 繼承了Serializable接口的類

  • 引用了第三方開源框架或繼承第三方SDK,如開源的okhttp網絡訪問框架,百度定位SDK等

  • 有用到WEBView的JS調用接口

針對以上幾種狀況,分別給出下列對應解決的混淆規則

1.代碼中使用了反射,加入下面的混淆規則便可.

-keepattributes Signature
-keepattributes EnclosingMethod

2.使用GSON、fastjson等JSON解析框架所生成的對象類,加入下面的混淆規則便可.假設com.clock.bean包下全部的類都是JSON解析生成對象的類

-keep class com.clock.bean.**{*;}//不混淆全部的com.clock.bean包下的類和這些類的全部成員變量

3.繼承了Serializable接口的類,加上以下規則

//不混淆Serializable接口的子類中指定的某些成員變量和方法
-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();
}

4.引用了第三方開源框架或繼承第三方SDK狀況比較複雜,開發過程當中使用了知名成熟的開發框架都會有給出它的混淆規則,第三方的SDK也同樣。這裏以okhttp框架和百度地圖sdk爲例

//okhttp框架的混淆
-dontwarn com.squareup.okhttp.internal.http.*
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-keepnames class com.levelup.http.okhttp.** { *; }
-keepnames interface com.levelup.http.okhttp.** { *; }
-keepnames class com.squareup.okhttp.** { *; }
-keepnames interface com.squareup.okhttp.** { *; }

//百度sdk的混淆
-keep class com.baidu.** {*;}
-keep class vi.com.** {*;}
-dontwarn com.baidu.**

5.有用到WEBView的JS調用接口,需加入以下規則

-keepclassmembers class fqcn.of.javascript.interface.for.webview {
   public *;
}
-keep class com.xxx.xxx.** { *; }//保持WEB接口不被混淆 此處xxx.xxx是本身接口的包名

ProGuard語法的延伸

下面對ProGuard語法作一些小小的延伸和拓展

  • 不混淆某個類

-keep class com.clock.**//不混淆全部com.clock包下的類,** 換成具體的類名則表示不混淆某個具體的類
  • 不混淆某個類和成員變量

-keep class com.clock.**{*;}//不混淆全部com.clock包下的類和類中的全部成員變量,**能夠換成具體類名,*能夠換成具體的字段,可參照Serialzable的混淆
  • 不混淆類中的方法或變量,參照AndroidStudio給的默認混淆文件的書寫方式

  • 使用assumenosideeffects移除代碼

//移除Log類打印各個等級日誌的代碼,打正式包的時候能夠作爲禁log使用,這裏能夠做爲禁止log打印的功能使用,另外的一種實現方案是經過BuildConfig.DEBUG的變量來控制
-assumenosideeffects class android.util.Log {
    public static *** v(...);
    public static *** i(...);
    public static *** d(...);
    public static *** w(...);
    public static *** e(...);
}

總結

大體的作了ProGuard的使用總結,光看不寫是不會有好代碼出來的,動手打碼吧,感謝相關參考資料的做者。

相關文章
相關標籤/搜索