为什么要加代码混淆
--------------------不想开源应用,为了加大反编译的成本,但是并不能彻底防止反编译
开启混淆
通常我们需要找到项目路径下app目录下的build.gradle文件
找到minifyEnabled这个配置,然后设置为true即可.
release{
minifyEnabled true//是否启动混淆 ture:打开 false:关闭
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
混淆的常见配置
Proguard关键字 | 描述 |
---|---|
dontwarn | dontwarn是一个和keep可以说是形影不离,尤其是处理引入的library时. |
keep | 保留类和类中的成员,防止被混淆或移除 |
keepnames | 保留类和类中的成员,防止被混淆,成员没有被引用会被移除 |
keepclassmembers | 只保留类中的成员,防止被混淆或移除 |
keepclassmembernames | 只保留类中的成员,防止被混淆,成员没有引用会被移除 |
keepclasseswithmembers | 保留类和类中的成员,防止被混淆或移除,保留指明的成员 |
keepclasseswithmembernames | 保留类和类中的成员,防止被混淆,保留指明的成员,成员没有引用会被移除 |
哪些不应该混淆
####-----------基本配置-不能被混淆的------------
-optimizationpasses 5 #指定代码压缩级别
-dontusemixedcaseclassnames #混淆时不会产生形形色色的类名
-dontskipnonpubliclibraryclasses #指定不忽略非公共类库
-dontpreverify #不预校验,如果需要预校验,是-dontoptimize
-ignorewarnings #屏蔽警告
-verbose #混淆时记录日志
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* #优化
# 保留我们使用的四大组件,自定义的Application等等这些类不被混淆
# 因为这些子类都有可能被外部调用
-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.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.v7.** { *; } #过滤android.support.v7 注意这里v4还是v7要看gradle里面compile了那个扩展包
-keep interface android.support.constraint.** { *; }
-keep class com.alibaba.fastjson.** {*;} #保持第三方包fastjson不被混淆,否则会报错
-keep class javax.ws.rs.** { *; }
-dontwarn com.alibaba.fastjson.**
-keep public class * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.** {*;}
#保持注解继承类不混淆
-keep class * extends java.lang.annotation.Annotation {*;}
-keepclasseswithmembernames class * {
native ;
}
# 保持自定义控件类不被混淆
-keepclassmembers class * extends android.app.Activity{
public void *(android.view.View);
}
# 保持枚举 enum 类不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
#自定义组件不被混淆
-keep public class * extends android.view.View{
*** get*();
void set*(***);
public (android.content.Context);
public (android.content.Context, android.util.AttributeSet);
public (android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
public (android.content.Context, android.util.AttributeSet);
public (android.content.Context, android.util.AttributeSet, int);
}
# 保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
#保持Serializable不被混淆并且enum 类也不被混淆
-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();
}
-keep class **.R$* {
*;
}
-keepclassmembers class * {
void *(**On*Event);
}
#3.webview不被混淆
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
-keepclassmembers class * extends android.webkit.webViewClient {
public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
public boolean *(android.webkit.WebView, java.lang.String);
}
-keepclassmembers class * extends android.webkit.webViewClient {
public void *(android.webkit.webView, jav.lang.String);
}
###-----------第三方jar包library混淆配置---------
-dontwarn rx.**
-keepclassmembers class rx.** { *; }
# retrolambda
-dontwarn java.lang.invoke.*
#Retrofit简单使用及混淆配置
# Platform calls Class.forName on types which do not exist on Android to determine platform.
-dontnote retrofit2.Platform
# Platform used when running on RoboVM on iOS. Will not be used at runtime.
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
# Platform used when running on Java 8 VMs. Will not be used at runtime.
-dontwarn retrofit2.Platform$Java8
# Retain generic type information for use by reflection by converters and adapters.
-keepattributes Signature
# Retain declared checked exceptions for use by a Proxy instance.
-keepattributes Exceptions
#okhttp3不被混淆
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
#jar包不被混淆
-libraryjars libs/android-async-http-master.jar
-libraryjars libs/fastjson-1.1.45.jar
-libraryjars libs/GetuiSDK2.12.3.0.jar
-libraryjars libs/Msc.jar
-libraryjars libs/Sunflower.jar
#联网不被混淆
-keepattributes Signature
-keep class com.squareup.okhttp.** { *;}
-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-keep class de.greenrobot.event.** {*;}
-keepclassmembers class ** {
public void onEvent*(**);
void onEvent*(**);
}
#-dontwarn javax.annotation.**
-dontwarn javax.inject.**
#mport com.alipay.iot.sdk.APIManager;
#支付宝刷脸不被混淆
-keep class com.alipay.iot.** { *;}
-dontwarn com.alipay.iot.**
# retrofit2和RX不被混淆
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Signature
-keepattributes Exceptions
# RxJava RxAndroid
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
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;
}
# Gson
-keep class com.google.gson.stream.** { *; }
-keepattributes EnclosingMethod
-keep class com.lipengfei.meishiyiyun.errandapp.bean.**{*;}
-dontwarn butterknife.internal.**
-keep class **$$ViewInjector { *; }
-keepnames class * { @butterknife.InjectView *;}
-dontwarn com.viewpagerindicator.**
-keep class com.viewpagerindicator.** { *;}