使用Android Studio混淆打包(ProGuard基礎語法和打包配置)

前言:前一段時間被領導叫去使用ProGuard對項目進行混淆,折騰了兩天才弄好,在學習的過程中發現了這篇文章,對自己的幫助很大,特意摘抄下來分享給大家。

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混淆規則和語法,直接上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(...);
}

以上是原文

以下個人的一些擴充
打印混淆日誌,可以查看混淆的文件以及混淆的情況

#apk包內所有class 的內部結構
#-dump class_files.txt
#未混淆的類和成員
#-printseeds seeds.txt
#列出從apk中刪除的代碼
#-printusage unused.txt
#混淆前後的映射
#-printmapping mapping.txt

最後爲了不報錯我還是加入這個,其實如果可以這個語句大家還是少用比較好

#忽略警告(慎用)
-ignorewarnings

原文鏈接:ProGuard基礎語法和打包配置(GitHub)

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