Android 代码混淆,以下是使用 Android studio 进行的代码混淆
(包含了对一些第三方服务混淆:
android.support.v4,
fastjson,
imageloader,
volley,
core,
jpush,
baidui,
友盟统计)
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in C:\Users\lenovo\AppData\Local\Android\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-ignorewarnings #忽略警告
-optimizationpasses 5 # 指定代码的压缩级别
-dontusemixedcaseclassnames # 是否使用大小写混合(否)
-dontskipnonpubliclibraryclasses # 是否混淆第三方jar(否)
-dontpreverify # 混淆时是否做预校验(否)
-verbose # 混淆时是否记录日志(是)
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆时所采用的算法
-keepattributes Signature #过滤泛型(能会出现的类型转换错误)
-keepattributes *Annotation* #不混淆项目注解
-keep class **.R$* { *; } #保持R文件不被混淆,否则,你的反射是获取不到资源id的
-keepclasseswithmembernames class * {
# 保持 native 方法不被混淆 native <methods>;
}
-keepclassmembers class * extends android.app.Activity {
# 保持自定义控件类不被混淆
public void *(android.view.View);
}
-keepclassmembers enum * {
# 保持枚举 enum 类不被混淆
public static **[] values();
public static ** valueOf(java.lang.String);
}
-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*(...);
}
-keep class * implements android.os.Parcelable {
# 保持 Parcelable 不被混淆
public static final android.os.Parcelable$Creator *;
}
#主要确保JOSN解析字段名称不被混淆
-keepnames class * implements java.io.Serializable #需要序列化和反序列化的类不能被混淆(注:Java反射用到的类也不能被混淆)
-keepclassmembers class * implements java.io.Serializable {
#保护实现接口Serializable的类中,指定规则的类成员不被混淆
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
#webView
-keep class **.Webview2JsInterface { *; } #保护WebView对HTML页面的API不被混淆
-keepclassmembers class * extends android.webkit.WebViewClient {
#如果你的项目中用到了webview的复杂操作 ,最好加入
public void *(android.webkit.WebView,java.lang.String,android.graphics.Bitmap);
public boolean * (android.webkit.WebView,java.lang.String);
}
-keepclassmembers class * extends android.webkit.WebChromeClient {
#如果你的项目中用到了webview的复杂操作 ,最好加入
public void *(android.webkit.WebView,java.lang.String);
}
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
#butterknife
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
#-------------------------------------------------------------------------
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Fragment
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.preference.Preference
-keep class com.yungui.service.model.** { *; }#实体类不混淆
#必要时还需混淆自定义控件
#AndroidStudio 解决 Classes警告
-keepattributes EnclosingMetho
-keepattributes InnerClasses#不混淆第三方jar
-dontoptimize
-dontpreverify
#start----------------------------------------------------------------------
# android.support.v4
-dontwarn android.support.v4.**
-keep interface android.support.v4.app.** { *; }
-keep public class * extends android.support.v4.**
#fastjson
-dontwarn com.alibaba.fastjson.**
-keep class com.alibaba.fastjson.** { *; }
#imageloader
-dontwarn com.nostra13.universalimageloader.**
-keep class com.nostra13.universalimageloader.** { *; }
# volley
-dontwarn com.android.volley.jar.**
-keep class com.android.volley.**{*;}
#core
-dontwarn cn.google.zxing.**
-keep class cn.google.zxing.** { *; }
# jpush
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
# baidui map
-dontwarn com.baidu.**
-keep class com.baidu.**{*;}
-keep class vi.com.**{*;}
#友盟APP统计提供的混淆规则
-keepclassmembers class * {
public <init> (org.json.JSONObject);
}
-keep public class [com.yungui.service].R$*{
public static final int *;
}
#end----------------------------------------------------------------------
以上混淆规则已经过测试,如有失误欢迎指正