1. 程式人生 > >Android混淆配置及總結

Android混淆配置及總結

Android打正式的release包混淆是必備的,避免APP被反編譯,使專案中隱祕的技術或資訊被別人檢視。

Android studio則寫在proguard-rules.pro中,當然寫在proguard-android.txt中也可以。

 

gradle配置

buildTypes {   
     release {        
           buildConfigField "boolean", "LOG_DEBUG", "false" //不顯示log    
           zipAlignEnabled true     //Zipalign優化   
shrinkResources true // 移除無用的resource檔案 minifyEnabled true //混淆 proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }

 

 經常用第三方SDK的時候會忘了順手把混淆加進來,到了打包的時候又去找,很是麻煩,因此在這裡總結一下

-optimizationpasses 5 # 指定程式碼的壓縮級別
-dontusemixedcaseclassnames # 是否使用大小寫混合 -dontpreverify # 混淆時是否做預校驗 -verbose # 混淆時是否記錄日誌 -optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆時所採用的演算法 -keep public class * extends android.app.Activity # 保持哪些類不被混淆 -keep public class * extends android.app.Application # 保持哪些類不被混淆 -keep public class * extends android.app.Service # 保持哪些類不被混淆
-keep public class * extends android.content.BroadcastReceiver # 保持哪些類不被混淆 -keep public class * extends android.content.ContentProvider # 保持哪些類不被混淆 -keep public class * extends android.app.backup.BackupAgentHelper # 保持哪些類不被混淆 -keep public class * extends android.preference.Preference # 保持哪些類不被混淆 -keep public class com.android.vending.licensing.ILicensingService # 保持哪些類不被混淆 -keepclasseswithmembernames class * { # 保持 native 方法不被混淆 native
<methods>; } -keepclasseswithmembers class * { # 保持自定義控制元件類不被混淆 public <init>(android.content.Context, android.util.AttributeSet); } -keepclasseswithmembers class * { # 保持自定義控制元件類不被混淆 public <init>(android.content.Context, android.util.AttributeSet, int); } -keepclassmembers class * extends android.app.Activity { # 保持自定義控制元件類不被混淆 public void *(android.view.View); } -keepclassmembers enum * { # 保持列舉 enum 類不被混淆 public static **[] values(); public static ** valueOf(java.lang.String); } -keep class * implements android.os.Parcelable {#保持Parcelable不被混淆 public static final android.os.Parcelable$Creator *; } # Explicitly preserve all serialization members. The Serializable interface # is only a marker interface, so it wouldn't save them.
# 顯式保留所有序列化成員。可序列化介面只是一個標記介面,因此不會儲存它們。
-keep public class * implements java.io.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(); } -keepclassmembers class * { public
<init> (org.json.JSONObject); } #com.demo.demo 是你的包名 -keep public class com.demo.demo.R$*{ public static final int *; }

 

1. Gson  GitHub地址

# removes such information by default, so configure it to keep all of it.
-keepattributes Signature
# Gson specific classes
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.google.gson.** { *;}
#這句非常重要,主要是濾掉 com.demo.demo.bean包下的所有.class檔案不進行混淆編譯,com.demo.demo是你的包名
-keep class com.demo.demo.bean.** {*;}

 

2. Butterknife  GitHub地址

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {    
   @butterknife.* <fields>;
}
-keepclasseswithmembernames class * {    
   @butterknife.* <methods>;
}

 

3. SlidingMenu   GitHub地址

-dontwarn com.jeremyfeinstein.slidingmenu.lib.**
-keep class com.jeremyfeinstein.slidingmenu.lib.**{*;}

 

4. ImageLoader   GitHub地址

#Universal Image Loader
-keep class com.nostra13.universalimageloader.** { *; }
-keepattributes Signature

 

5. ShareSDK   文件

-keep class cn.sharesdk.**{*;}
-keep class com.sina.**{*;}
-keep class **.R$* {*;}
-keep class **.R{*;}
-dontwarn cn.sharesdk.**
-dontwarn **.R$*
-dontwarn com.tencent.**
-keep class com.tencent.** {*;}

 

6. Okhttp-OkGo  GitHub地址

#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;} #okio -dontwarn okio.** -keep class okio.**{*;}

 

7. RxJava    GitHub地址

# RX
-dontwarn rx.**
-keepclassmembers class rx.** { *; }
# retrolambda
-dontwarn java.lang.invoke.*

 

8. Glide    GitHub地址

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {   
  **[] $VALUES; 
  public *;
}

 

9. 環信SDK   文件

-keep class com.easemob.** {*;}
-keep class org.jivesoftware.** {*;}
-keep class org.apache.** {*;}
-dontwarn  com.easemob.**
#2.0.9後的不需要加下面這個keep
#-keep class org.xbill.DNS.** {*;}
#另外,demo中傳送表情的時候使用到反射,需要keep SmileUtils
-keep class com.easemob.chatuidemo.utils.SmileUtils {*;}
#注意前面的包名,如果把這個類複製到自己的專案底下,比如放在com.example.utils底下,應該這麼寫(實際要去掉#)
#-keep class com.example.utils.SmileUtils {*;}
#如果使用easeui庫,需要這麼寫
-keep class com.easemob.easeui.utils.EaseSmileUtils {*;}
#2.0.9後加入語音通話功能,如需使用此功能的api,加入以下keep
-dontwarn ch.imvs.**
-dontwarn org.slf4j.**
-keep class org.ice4j.** {*;}
-keep class net.java.sip.** {*;}
-keep class org.webrtc.voiceengine.** {*;}
-keep class org.bitlet.** {*;}
-keep class org.slf4j.** {*;}
-keep class ch.imvs.** {*;}