混淆是发布app最基本的安全保护方法 也是让很多android开发人员头疼的问题 在此我也列出比较常用的混淆(第三方) 包括一些从网上找到的(当然是有用的)简书的排版有些缩进和*****号需自行补充下
<pre><code>
基础配置
。
。
。
。
#######指定代码的压缩级别
-optimizationpasses 5
#######包明不混合大小写
-dontusemixedcaseclassnames
#######不去忽略非公共的库类
-dontskipnonpubliclibraryclasses
#######优化 不优化输入的类文件
-dontoptimize
#######预校验
-dontpreverify
混淆时是否记录日志
-verbose
#######混淆时所采用的算法
-optimizations !code/simplification/arithmetic,!field/,!class/merging/
#######保护注解
-keepattributes Annotation
####### 保持哪些类不被混淆
-keep public class * extends android.app.Fragment
-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
#######如果有引用v4包可以添加下面这行
-keep public class * extends android.support.v4.app.Fragment
#######忽略警告
-ignorewarning
#####################记录生成的日志数据,gradle build时在本项目根目录输出################
#######apk 包内所有 class 的内部结构
#######-dump class_files.txt
#######未混淆的类和成员
-printseeds seeds.txt
#######列出从 apk 中删除的代码
#######-printusage unused.txt
#######混淆前后的映射
#######-printmapping mapping.txt
#######如果引用了v4或者v7包
-dontwarn android.support.**
#######禁止优化泛型
-keepattributes Signature
#######保持 native 方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;
}
#######保持自定义控件类不被混淆
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
#######保持自定义控件类不被混淆
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
#######保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator ;
}
-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set(...);
}
#######保持 Serializable 不被混淆
#######-keepnames class * implements java.io.Serializable
#######保持 Serializable 不被混淆并且enum 类也不被混淆
#######-keepclassmembers class * implements java.io.Serializable {
####### static final long serialVersionUID;
####### private static final java.io.ObjectStreamField[] serialPersistentFields;
####### !static !transient <fields>;
####### !private <fields><span></span>;
####### !private <methods>;
####### private void writeObject(java.io.ObjectOutputStream);
####### private void readObject(java.io.ObjectInputStream);
####### java.lang.Object writeReplace();
####### java.lang.Object readResolve();
#######}
#######保持枚举 enum 类不被混淆 如果混淆报错,建议直接使用上面的 -keepclassmembers class * implements java.io.Serializable即可
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keepclassmembers class * {
public void *ButtonClicked(android.view.View);
}
#######不混淆资源类
-keepclassmembers class *.R$ {
public static <fields>;
}</code>
极光
-dontwarn cn.jpush.**
-keep class cn.jpush.** { ; }
-keep class com.ddcar.receiver.MyReceiver {;}
高德
-dontwarn com.amap.api.**
-keep class com.amap.api.location.{;}
-keep class com.amap.api.fence.{;}
-keep class com.autonavi.aps.amapapi.model.*{;}
讯飞
-keep class com.iflytek.*{;}
butterknife
-keep class butterknife.** { ; }
-dontwarn butterknife.internal.*
-keep class *$$ViewInjector { ; }
-keepclasseswithmembernames class * {
@butterknife. <fields>;
}
-keepclasseswithmembernames class * {
@butterknife. <methods>;
}
#######picasso
-dontwarn com.squareup.okhttp.**
litepal
-dontwarn org.litepal.*
-keep class org.litepal.** { ; }
-keep enum org.litepal.*
-keep interface org.litepal.** { ; }
-keep public class * extends org.litepal.*
-keepattributes Annotation
-keepclassmembers class * extends org.litepal.crud.DataSupport{*;}
rxjava
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.ArrayQueueField* {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
retrofit2.X
-dontwarn retrofit.**
-keep class retrofit.** { *; }
-keepattributes Signature
-keepattributes Exceptions
retrofit使用的实体类不能混淆
-keep class com.life.me.entity.postentity{;}
-keep class com.life.me.entity.resultentity{;}
okhttp
-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.{;}
-keep class com.zhy.http.okhttp.{;}
-keep interface com.squareup.okhttp.** { ; }
-dontwarn okio.*
-keep class com.google.gson.* {*;}
-keep class com.google.gson.JsonObject { *; }
bmob
这里根据具体的SDK版本修改
-keep class cn.bmob.v3.** {*;}
保证继承自BmobObject、BmobUser类的JavaBean不被混淆
-keep class com.example.bmobexample.bean.BankCard{;}
-keep class com.example.bmobexample.bean.GameScore{;}
-keep class com.example.bmobexample.bean.MyUser{;}
-keep class com.example.bmobexample.bean.Person{;}
-keep class com.example.bmobexample.file.Movie{;}
-keep class com.example.bmobexample.file.Song{;}
-keep class com.example.bmobexample.relation.Post{;}
-keep class com.example.bmobexample.relation.Comment{;}
参考 http://youmu178.com/android-development/proguard-sample.html