在实际开发中我们在发布App时为了防止有人对App进行反编译我们需要对APK进行混淆,当然混淆的作用不只是为了防止对APK的反编译,还可以让我们开发的APK体积减小和方法数超过65535。
首先我们需要在app包下的build.gradle中开启编译混淆。
release {
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
zipAlignEnabledtrue
shrinkResourcesfalse // 移除无用的resource文件
}
然后需要对proguard-rules.pro文件下进行混淆操作把我们需要做的混淆操作进行处理。
本人直接先粘贴上自己项目中的混淆代码处理,下面的代码可能会让你少找很多混淆代码。另外对于一个刚开始混淆的人来说,最好的混淆方式就是一个个慢慢来,一个个添加,这样虽然说可能会耗费更多的时间,但是能保证我们在混淆时对于出错的地方有一个定位方便处理错误。
#-------------------------------------------基本不用动区域--------------------------------------------
#---------------------------------基本指令区----------------------------------
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/* #指定要启用和禁用的优化
-optimizationpasses 5
-dontskipnonpubliclibraryclassmembers #指定不去忽略包可见的库类的成员。
-printmapping proguardMapping.txt
-optimizations !code/simplification/cast,!field/*,!class/merging/*
-keepattributes *Annotation*,InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
-ignorewarnings
-allowaccessmodification #指定在预先处理中 类和类成员的访问性可能会扩大
-dontpreverify #指定不预先验证处理类文件
-dontusemixedcaseclassnames #指定不产生混合的情况下的类名称而混淆
-dontskipnonpubliclibraryclasses
-verbose
-keepattributes *Annotation* #保持注解
-keepattributes Signature
-keepattributes EnclosingMethod
-keep class * extends java.lang.annotation.Annotation { *; }
#----------------------------------------------------------------------------
#---------------------------------默认保留区---------------------------------
#继承activity,application,service,broadcastReceiver,contentprovider....不进行混淆
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.support.multidex.MultiDexApplication
-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 * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.** {*;}
-keep public class * extends android.view.View{
*** get*();
void set*(***);
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
#这个主要是在layout 中写的onclick方法android:onclick="onClick",不进行混淆
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-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$* {
*;
public static <fields>;
}
-keepclassmembers class * {
void *(*Event);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
#// natvie 方法不混淆
-keepclasseswithmembernames class * {
native <methods>;
}
#保持 Parcelable 不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-dontwarn android.support.v4.**
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep public class * extends android.support.v4.**
-keep public class * extends android.support.v4.app.Fragment
-keep public class * extends android.support.v4.app.FragmentActivity
#---------------------------------1.实体类---------------------------------
-keep class com.equipmentrental.bean.** { *; }
##################以下是第三方的混淆#############################
## gson 解析 https://github.com/google/gson
-dontwarn com.google.gson.**
-keep class com.google.gson.** {*;}
#-keep class com.google.**{*;}
#-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keep class com.google.gson.examples.android.model.** { *; }
-keep class com.google.** {
<fields>;
<methods>;
}
-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();
}
## RecyclerView适配器 https://github.com/CymChad/BaseRecyclerViewAdapterHelper
## com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.34
-keep class com.chad.library.adapter.** {
*;
}
-keep public class * extends com.chad.library.adapter.base.BaseQuickAdapter
-keep public class * extends com.chad.library.adapter.base.BaseViewHolder
-keepclassmembers class **$** extends com.chad.library.adapter.base.BaseViewHolder {
<init>(...);
}
## 左右滑动 未找到 https://github.com/anzaizai/EasySwipeMenuLayout
## com.github.anzaizai:EasySwipeMenuLayout:1.1.2
## rxjava
## io.reactivex.rxjava2:rxjava:2.1.1
## rxandroid
## io.reactivex.rxjava2:rxandroid:2.0.1
-dontwarn rx.*
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQuene*Field*{
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
## converter-gson
## com.squareup.retrofit2:converter-gson:2.3.0
## retrofit
## com.squareup.retrofit2:retrofit:2.3.0
#-keepclassmembernames,allowobfuscation interface * {
# @retrofit2.http.* <methods>;
#}
#-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement
-keep class retrofit2.** { *; }
-dontwarn retrofit2.**
-keepattributes Signature
-keepattributes Exceptions
-dontwarn okio.**
-dontwarn javax.annotation.**
## retrofit2-rxjava2-adapter
## com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0
## okhttp
## com.squareup.okhttp3:okhttp:3.8.1
-keep class com.squareup.okhttp.{*;}
-dontwarn com.squareup.okhttp.**
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.** { *;}
-dontwarn okio.**
## logging-interceptor
## com.squareup.okhttp3:logging-interceptor:3.4.1
## 下拉刷新 上拉加载更多 并不需要混淆
## com.scwang.smartrefresh:SmartRefreshLayout:1.1.0-alpha-5
## 状态栏
## com.gyf.barlibrary:barlibrary:2.3.0
-keep class com.gyf.barlibrary.* {*;}
## tablayout 未找到混淆 https://github.com/H07000223/FlycoTabLayout
## com.flyco.tablayout:FlycoTabLayout_Lib:2.1.2@aar
## 选择器 未找到混淆 https://github.com/Bigkoo/Android-PickerView
## com.contrarywind:Android-PickerView:4.1.4
## 密码输入框 暂时未找到官方的混淆
## com.jacktuotuo.customview:verificationcodeview:1.0.3
## Banner
## com.youth.banner:banner:1.4.10
# banner 的混淆代码
-keep class com.youth.banner.** {*;}
## 拼音 暂时未找到
## com.github.promeg:tinypinyin:1.0.0
## 高德地图
## com.amap.api:3dmap:5.0.0
## com.amap.api:location:3.3.0
## com.amap.api:search:5.0.0
# map
-keep class com.amap.api.maps.**{*;}
-keep class com.autonavi.**{*;}
-keep class com.amap.api.trace.**{*;}
#location
-keep class com.amap.api.location.**{*;}
-keep class com.amap.api.fence.**{*;}
-keep class com.autonavi.aps.amapapi.model.**{*;}
#search
-keep class com.amap.api.services.**{*;}
#2dmap
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}
## 事件线管理 混淆
## org.greenrobot:eventbus:3.1.1
-keepattributes *Annotation*
-keepclassmembers class * {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
## 权限管理 官方暂时未提供
## com.github.tbruyelle:rxpermissions:0.10.2
-keep class com.tbruyelle.rxpermissions2.** { *; }
-keep interface com.tbruyelle.rxpermissions2.** { *; }
#######极光推送
## cn.jiguang.sdk:jpush:3.1.6
## cn.jiguang.sdk:jcore:1.2.5
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
-keep class * extends cn.jpush.android.helpers.JPushMessageReceiver { *; }
-dontwarn cn.jiguang.**
-keep class cn.jiguang.** { *; }
#==================gson && protobuf==========================
-dontwarn com.google.**
-keep class com.google.gson.** {*;}
-keep class com.google.protobuf.** {*;}
## glide 图片加载 https://github.com/bumptech/glide
## com.github.bumptech.glide:glide:3.7.0
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
## 图片选择器
## cn.finalteam.rxgalleryfinal:library:1.1.2
#1.support-v7-appcompat
-keep public class android.support.v7.widget.** { *; }
-keep public class android.support.v7.internal.widget.** { *; }
-keep public class android.support.v7.internal.view.menu.** { *; }
-keep public class * extends android.support.v4.view.ActionProvider {
public <init>(android.content.Context);
}
##2.rx
-dontwarn io.reactivex.**
#-keep io.reactivex.**
-keepclassmembers class io.reactivex.** { *; }
#3.retrolambda
-dontwarn java.lang.invoke.*
#4.support-v4
-keep class android.support.v4.** { *; }
-keep interface android.support.v4.** { *; }
#5.ucrop
-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }
#6.photoview
-keep class uk.co.senab.photoview** { *; }
-keep interface uk.co.senab.photoview** { *; }
#7.rxgalleryfinal
-keep class cn.finalteam.rxgalleryfinal.ui.widget** { *; }
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepclassmembers class **.R$* {
public static <fields>;
}
-keepattributes *Annotation*
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclassmembers public class * extends android.view.View {
void set*(***);
*** get*();
网友评论