Android混淆配置总结-持续更新

Android打正式的release包混淆是必备的,避免APP被反编译,使项目中隐秘的技术或信息被别人查看。

  • Eclipse中混淆配置写在proguard-android.txt文件中,并在project.properties文件中设置 proguard.config=proguard-project.txt
  • 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的时候会忘了顺手把混淆加进来,到了打包的时候又去找,百度噼里啪啦一堆,很是麻烦,因此在这里总结一下,下次用的时候偶就可以直接CC了,哈哈。如有不对欢迎指正。

-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 *;
}

以上部分摘自:http://blog.csdn.net/bjstyle/article/details/47001277

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.** {*;}

Butterknife github地址

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

SlidingMenu github地址

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

ImageLoader github地址

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

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.** {*;}

环信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.** {*;}

Okhttputils github地址

#okhttputils
-dontwarn com.zhy.http.**
-keep class com.zhy.http.**{*;}
#okhttp
-dontwarn okhttp3.**
-keep class okhttp3.**{*;}
#okio
-dontwarn okio.**
-keep class okio.**{*;}

RxJava 相关文档

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

Glide github地址

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {   
  **[] $VALUES; 
  public *;
}
#下面这句不加
#-keepresourcexmlelements manifest/application/meta-data@value=GlideModule

暂时就这些,用到别的再加进来,大家需要别的混淆在下面评论啊。

    原文作者:钟子明
    原文地址: https://www.jianshu.com/p/155430a27f00
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞