在Android Studio當(dāng)中混淆APK實(shí)在是太簡單了散庶,借助SDK中自帶的Proguard工具蕉堰,只需要修改app module 下的build.gradle中的一行配置即可凌净。可以看到屋讶,現(xiàn)在build.gradle中minifyEnabled的值是false冰寻,這里我們只需要把值改成true,編譯出來的APK包就會(huì)是混淆過的了皿渗。如下所示:
其中:
minifyEnabled用于設(shè)置是否啟用混淆斩芭;
shrinkResources用于設(shè)置刪除無效的Resources,shrinkResources依賴于minifyEnabled乐疆,必須一起使用划乖;
proguardFiles用于選定混淆配置文件。注意這里是在release閉包內(nèi)進(jìn)行配置的诀拭,因此只有編譯出正式版的APK才會(huì)進(jìn)行混淆迁筛,Debug版的APK是不會(huì)混淆的。當(dāng)然這也是非常合理的耕挨,因?yàn)镈ebug版的APK文件我們只會(huì)用來內(nèi)部測(cè)試,不用擔(dān)心被人破解尉桩。
proguard-android.txt混淆配置文件筒占,該文件存放于android studio的sdk目錄下androidsdk\tools\proguard,打開文件如下:
# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose
# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.
-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
? ? ? ? native ;
}
# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
? ? ? ? void set*(***);
? ? ? ? *** get*();
}
# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
? ? ? ? public void *(android.view.View);
}
# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
? ? ? ? public static **[] values();
? ? ? ? public static ** valueOf(java.lang.String);
}
-keepclassmembers class * implements android.os.Parcelable {
? ? ? ? public static final android.os.Parcelable$Creator CREATOR;
}
-keepclassmembers class **.R$* {
? ? ? ? public static ;
}
# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.? We know about them, and they are safe.
-dontwarn android.support.**
# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep @android.support.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
? ? ? ? @android.support.annotation.Keep ;
}
-keepclasseswithmembers class * {
? ? ? ? @android.support.annotation.Keep ;
}
-keepclasseswithmembers class * {
? ? ? ? @android.support.annotation.Keep (...);
}
我就問你 是不是感覺頭很大蜘犁,哈哈 別著急下面還有解釋翰苫。
這個(gè)就是默認(rèn)的混淆配置文件了,我們來一起逐行閱讀一下这橙。
好了奏窑,這就是proguard-android.txt文件中所有默認(rèn)的配置,而我們混淆代碼也是按照這些配置的規(guī)則來進(jìn)行混淆的屈扎。經(jīng)過我上面的講解之后埃唯,相信大家對(duì)這些配置的內(nèi)容基本都能理解了。
回到Android Studio項(xiàng)目當(dāng)中鹰晨,APK雖然已經(jīng)成功混淆了墨叛,但是混淆的規(guī)則都是按照proguard-android.txt中默認(rèn)的規(guī)則來的,當(dāng)然我們也可以修改proguard-android.txt中的規(guī)則模蜡,但是直接在proguard-android.txt中修改會(huì)對(duì)我們本機(jī)上所有項(xiàng)目的混淆規(guī)則都生效漠趁,那么有沒有什么辦法只針對(duì)當(dāng)前項(xiàng)目的混淆規(guī)則做修改呢?當(dāng)然是有辦法的了忍疾,你會(huì)發(fā)現(xiàn)任何一個(gè)Android Studio項(xiàng)目在app模塊目錄下都有一個(gè)proguard-rules.txt(proguard-rules.pro)文件闯传,這個(gè)文件就是用于讓我們編寫只適用于當(dāng)前項(xiàng)目的混淆規(guī)則的。自定義混淆通用模板如下:
#############################################
#
#對(duì)于一些基本指令的添加
#
#############################################
#代碼混淆壓縮比卤妒,在0~7之間甥绿,默認(rèn)為5叠必,一般不做修改
-optimizationpasses5
#混合時(shí)不使用大小寫混合,混合后的類名為小寫
-dontusemixedcaseclassnames
#指定不去忽略非公共庫的類
-dontskipnonpubliclibraryclasses
#這句話能夠使我們的項(xiàng)目混淆后產(chǎn)生映射文件
#包含有類名->混淆后類名的映射關(guān)系
-verbose
#指定不去忽略非公共庫的類成員
-dontskipnonpubliclibraryclassmembers
#不做預(yù)校驗(yàn)妹窖,preverify是proguard的四個(gè)步驟之一纬朝,Android不需要preverify,去掉這一步能夠加快混淆速度骄呼。
-dontpreverify
#保留Annotation不混淆
-keepattributes*Annotation*,InnerClasses
#避免混淆泛型
-keepattributesSignature
#拋出異常時(shí)保留代碼行號(hào)
-keepattributesSourceFile,LineNumberTable
#指定混淆是采用的算法共苛,后面的參數(shù)是一個(gè)過濾器
#這個(gè)過濾器是谷歌推薦的算法,一般不做更改
-optimizations!code/simplification/cast,!field/*,!class/merging/*
#############################################
#
# Android開發(fā)中一些需要保留的公共部分
#
#############################################
#保留我們使用的四大組件蜓萄,自定義的Application等等這些類不被混淆
#因?yàn)檫@些子類都有可能被外部調(diào)用
-keeppublic class * extends android.app.Activity
-keeppublic class * extends android.app.Appliction
-keeppublic class * extends android.app.Service
-keeppublic class * extends android.content.BroadcastReceiver
-keeppublic class * extends android.content.ContentProvider
-keeppublic class * extends android.app.backup.BackupAgentHelper
-keeppublic class * extends android.preference.Preference
-keeppublic class * extends android.view.View
-keeppublic class com.android.vending.licensing.ILicensingService
#保留support下的所有類及其內(nèi)部類
-keepclass android.support.** {*;}
#保留繼承的
-keeppublic class * extends android.support.v4.**
-keeppublic class * extends android.support.v7.**
-keeppublic class * extends android.support.annotation.**
#保留R下面的資源
-keepclass **.R$* {*;}
#保留本地native方法不被混淆
-keepclasseswithmembernamesclass * {
native ;
}
#保留在Activity中的方法參數(shù)是view的方法隅茎,
#這樣以來我們?cè)趌ayout中寫的onClick就不會(huì)被影響
-keepclassmembersclass * extends android.app.Activity{
public void *(android.view.View);
}
#保留枚舉類不被混淆
-keepclassmembersenum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
#保留我們自定義控件(繼承自View)不被混淆
-keeppublic 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);
}
#保留Parcelable序列化類不被混淆
-keepclass * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
#保留Serializable序列化的類不被混淆
-keepclassmembersclass * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient ;
!private ;
!private ;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
#對(duì)于帶有回調(diào)函數(shù)的onXXEvent、**On*Listener的嫉沽,不能被混淆
-keepclassmembersclass * {
void *(**On*Event);
void *(**On*Listener);
}
# webView處理辟犀,項(xiàng)目中沒有使用到webView忽略即可
-keepclassmembersclass fqcn.of.javascript.interface.for.webview {
public *;
}
-keepclassmembersclass * extends android.webkit.webViewClient {
public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
public boolean *(android.webkit.WebView, java.lang.String);
}
-keepclassmembersclass * extends android.webkit.webViewClient {
public void *(android.webkit.webView, jav.lang.String);
}
#移除Log類打印各個(gè)等級(jí)日志的代碼,打正式包的時(shí)候可以做為禁log使用绸硕,這里可以作為禁止log打印的功能使用
#記得proguard-android.txt中一定不要加-dontoptimize才起作用
#另外的一種實(shí)現(xiàn)方案是通過BuildConfig.DEBUG的變量來控制
#-assumenosideeffects class android.util.Log {
#? ? public static int v(...);
#? ? public static int i(...);
#? ? public static int w(...);
#? ? public static int d(...);
#? ? public static int e(...);
#}
#############################################
#
#項(xiàng)目中特殊處理部分
#
#############################################
#-----------處理反射類---------------
#-----------處理js交互---------------
#-----------處理實(shí)體類---------------
#在開發(fā)的時(shí)候我們可以將所有的實(shí)體類放在一個(gè)包內(nèi)堂竟,這樣我們寫一次混淆就行了。
#-keep public class com.ljd.example.entity.** {
#? ? public void set*(***);
#? ? public *** get*();
#? ? public *** is*();
#}
#-----------處理第三方依賴庫---------
下面介紹常用第三方混淆配置
#############################################
#
#處理第三方依賴庫
#
#############################################
# AndroidEventBus
-keepclass org.simple.** {*;}
-keepinterface org.simple.** {*;}
-keepclassmembersclass * {
@org.simple.eventbus.Subscriber ;
}
#百度地圖(jar包換成自己的版本玻佩,記得簽名要匹配)
-libraryjarslibs/baidumapapi_v2_1_3.jar
-keepclass com.baidu.** {*;}
-keepclass vi.com.** {*;}
-keepclass com.sinovoice.** {*;}
-keepclass pvi.com.** {*;}
-dontwarncom.baidu.**
-dontwarnvi.com.**
-dontwarnpvi.com.**
# Bugly
-dontwarncom.tencent.bugly.**
-keepclass com.tencent.bugly.** {*;}
# ButterKnife
-keepclass butterknife.** {*;}
-dontwarnbutterknife.internal.**
-keepclass **$$ViewBinder {*;}
-keepclasseswithmembernamesclass * {
@butterknife.* ;
}
-keepclasseswithmembernamesclass * {
@butterknife.* ;
}
# EventBus
-keepattributes*Annotation*
-keepclassmembersclass ** {
@org.greenrobot.eventbus.Subscribe ;
}
-keepenum org.greenrobot.eventbus.ThreadMode {*;}
-keepclass com.facebook.** {*;}
-keepinterface com.facebook.** {*;}
-keepenum com.facebook.** {*;}
# FastJson
-dontwarncom.alibaba.fastjson.**
-keepclass com.alibaba.fastjson.** {*;}
-keepattributesSignature
-keepattributes*Annotation*
# Fresco
-keepclass com.facebook.fresco.** {*;}
-keepinterface com.facebook.fresco.** {*;}
-keepenum com.facebook.fresco.** {*;}
#高德相關(guān)依賴
#集合包:3D地圖3.3.2導(dǎo)航1.8.0定位2.5.0
-dontwarncom.amap.api.**
-dontwarncom.autonavi.**
-keepclass com.amap.api.**{*;}
-keepclass com.autonavi.**{*;}
#地圖服務(wù)
-dontwarncom.amap.api.services.**
-keepclass com.map.api.services.** {*;}
# 3D地圖
-dontwarncom.amap.api.mapcore.**
-dontwarncom.amap.api.maps.**
-dontwarncom.autonavi.amap.mapcore.**
-keepclass com.amap.api.mapcore.**{*;}
-keepclass com.amap.api.maps.**{*;}
-keepclass com.autonavi.amap.mapcore.**{*;}
#定位
-dontwarncom.amap.api.location.**
-dontwarncom.aps.**
-keepclass com.amap.api.location.**{*;}
-keepclass com.aps.**{*;}
#導(dǎo)航
-dontwarncom.amap.api.navi.**
-dontwarncom.autonavi.**
-keepclass com.amap.api.navi.** {*;}
-keepclass com.autonavi.** {*;}
# Glide
-keeppublic class * implements com.bumptech.glide.module.GlideModule
-keeppublic enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
# Gson
-keepattributesSignature-keepattributes *Annotation*
-keepclass sun.misc.Unsafe {*;}
-keepclass com.google.gson.stream.** {*;}
#使用Gson時(shí)需要配置Gson的解析對(duì)象及變量都不混淆出嘹。不然Gson會(huì)找不到變量。
#將下面替換成自己的實(shí)體類
-keepclass com.example.bean.** {*;}
# Jackson
-dontwarnorg.codehaus.jackson.**
-dontwarncom.fasterxml.jackson.databind.**
-keepclass org.codehaus.jackson.** {*;}
-keepclass com.fasterxml.jackson.** {*;}
#極光推送
-dontoptimize
-dontpreverify
-dontwarncn.jpush.**
-keepclass cn.jpush.** {*;}
# OkHttp3
-dontwarncom.squareup.okhttp3.**
-keepclass com.squareup.okhttp3.** {*;}
-dontwarnokio.**
# Okio
-dontwarncom.squareup.**
-dontwarnokio.**
-keeppublic class org.codehaus.* {*;}
-keeppublic class java.nio.* {*;}
# OrmLite
-keepattributes*DatabaseField*
-keepattributes*DatabaseTable*
-keepattributes*SerializedName*
-keepclass com.j256.**
-keepclassmembersclass com.j256.** {*;}
-keepenum com.j256.**
-keepclassmembersenum com.j256.** {*;}
-keepinterface com.j256.**
-keepclassmembersinterface com.j256.** {*;}
# Realm
-keepclass io.realm.annotations.RealmModule
-keep@io.realm.annotations.RealmModule class *
-keepclass io.realm.internal.Keep
-keep@io.realm.internal.Keep class * {*;}
-dontwarnjavax.**
-dontwarnio.realm.**
# Retrofit
-dontwarnretrofit2.**
-keepclass retrofit2.** {*;}
-keepattributesSignature
-keepattributesExceptions
# Retrolambda
-dontwarnjava.lang.invoke.*
# RxJava RxAndroid
-dontwarnsun.misc.**
-keepclassmembersclass rx.internal.util.unsafe.*ArrayQueue*Field* {
long producerIndex;
long consumerIndex;
}
-keepclassmembersclass rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembersclass rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
#微信支付
-dontwarncom.tencent.mm.**
-dontwarncom.tencent.wxop.stat.**
-keepclass com.tencent.mm.** {*;}
-keepclass com.tencent.wxop.stat.**{*;}
#信鴿
-keeppublic class * extends android.app.Service
-keeppublic class * extends android.content.BroadcastReceiver
-keepclass com.tencent.android.tpush.**? {* ;}
-keepclass com.tencent.mid.**? {* ;}
-keepattributes*Annotation*
#新浪微博
-keepclass com.sina.weibo.sdk.* {*;}
-keepclass android.support.v4.* {*;}
-keepclass com.tencent.* {*;}
-keepclass com.baidu.* {*;}
-keepclass lombok.ast.ecj.* {*;}
-dontwarnandroid.support.v4.**
-dontwarncom.tencent.**s
-dontwarncom.baidu.**
#訊飛語音
-dontwarncom.iflytek.**
-keepclass com.iflytek.** {*;}
#銀聯(lián)
-dontwarncom.unionpay.**
-keepclass com.unionpay.** {*;}
#友盟統(tǒng)計(jì)分析
-keepclassmembersclass * {public (org.json.JSONObject);}
-keepclassmembersenum com.umeng.analytics.** {
public static **[] values();
public static ** valueOf(java.lang.String);
}
#友盟自動(dòng)更新
-keepclassmembersclass * {public (org.json.JSONObject);}
-keeppublic class cn.irains.parking.cloud.pub.R$*{public static final int *;}
-keeppublic class * extends com.umeng.**
-keepclass com.umeng.** {*;}
#支付寶錢包
-dontwarncom.alipay.**
-dontwarnHttpUtils.HttpFetcher
-dontwarncom.ta.utdid2.**
-dontwarncom.ut.device.**
-keepclass com.alipay.android.app.IAlixPay{*;}
-keepclass com.alipay.android.app.IAlixPay$Stub{*;}
-keepclass com.alipay.android.app.IRemoteServiceCallback{*;}
-keepclass com.alipay.android.app.IRemoteServiceCallback$Stub{*;}
-keepclass com.alipay.sdk.app.PayTask{public *;}
-keepclass com.alipay.sdk.app.AuthTask{public *;}
-keepclass com.alipay.mobilesecuritysdk.*
-keepclass com.ut.*
好了咬崔,到這里此文已經(jīng)結(jié)束了税稼,如果幫到了您,請(qǐng)給我點(diǎn)個(gè)喜歡唄垮斯。