android studio apk混淆

在Android Studio当中混淆APK实在是太简单了,借助SDK中自带的Proguard工具,只需要修改app module 下的build.gradle中的一行配置即可。可以看到,现在build.gradle中minifyEnabled的值是false,这里我们只需要把值改成true,编译出来的APK包就会是混淆过的了。如下所示:

android studio apk混淆_第1张图片

其中:

minifyEnabled用于设置是否启用混淆;

shrinkResources用于设置删除无效的Resources,shrinkResources依赖于minifyEnabled,必须一起使用;

proguardFiles用于选定混淆配置文件。注意这里是在release闭包内进行配置的,因此只有编译出正式版的APK才会进行混淆,Debug版的APK是不会混淆的。当然这也是非常合理的,因为Debug版的APK文件我们只会用来内部测试,不用担心被人破解。

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 (...);

}


我就问你 是不是感觉头很大,哈哈 别着急下面还有解释。

这个就是默认的混淆配置文件了,我们来一起逐行阅读一下。

好了,这就是proguard-android.txt文件中所有默认的配置,而我们混淆代码也是按照这些配置的规则来进行混淆的。经过我上面的讲解之后,相信大家对这些配置的内容基本都能理解了。

回到Android Studio项目当中,APK虽然已经成功混淆了,但是混淆的规则都是按照proguard-android.txt中默认的规则来的,当然我们也可以修改proguard-android.txt中的规则,但是直接在proguard-android.txt中修改会对我们本机上所有项目的混淆规则都生效,那么有没有什么办法只针对当前项目的混淆规则做修改呢?当然是有办法的了,你会发现任何一个Android Studio项目在app模块目录下都有一个proguard-rules.txt(proguard-rules.pro)文件,这个文件就是用于让我们编写只适用于当前项目的混淆规则的。自定义混淆通用模板如下:

#############################################

#

#对于一些基本指令的添加

#

#############################################

#代码混淆压缩比,在0~7之间,默认为5,一般不做修改

-optimizationpasses5

#混合时不使用大小写混合,混合后的类名为小写

-dontusemixedcaseclassnames

#指定不去忽略非公共库的类

-dontskipnonpubliclibraryclasses

#这句话能够使我们的项目混淆后产生映射文件

#包含有类名->混淆后类名的映射关系

-verbose

#指定不去忽略非公共库的类成员

-dontskipnonpubliclibraryclassmembers

#不做预校验,preverify是proguard的四个步骤之一,Android不需要preverify,去掉这一步能够加快混淆速度。

-dontpreverify

#保留Annotation不混淆

-keepattributes*Annotation*,InnerClasses

#避免混淆泛型

-keepattributesSignature

#抛出异常时保留代码行号

-keepattributesSourceFile,LineNumberTable

#指定混淆是采用的算法,后面的参数是一个过滤器

#这个过滤器是谷歌推荐的算法,一般不做更改

-optimizations!code/simplification/cast,!field/*,!class/merging/*

#############################################

#

# Android开发中一些需要保留的公共部分

#

#############################################

#保留我们使用的四大组件,自定义的Application等等这些类不被混淆

#因为这些子类都有可能被外部调用

-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下的所有类及其内部类

-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中的方法参数是view的方法,

#这样以来我们在layout中写的onClick就不会被影响

-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();

}

#对于带有回调函数的onXXEvent、**On*Listener的,不能被混淆

-keepclassmembersclass * {

void *(**On*Event);

void *(**On*Listener);

}

# webView处理,项目中没有使用到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类打印各个等级日志的代码,打正式包的时候可以做为禁log使用,这里可以作为禁止log打印的功能使用

#记得proguard-android.txt中一定不要加-dontoptimize才起作用

#另外的一种实现方案是通过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(...);

#}

#############################################

#

#项目中特殊处理部分

#

#############################################

#-----------处理反射类---------------

#-----------处理js交互---------------

#-----------处理实体类---------------

#在开发的时候我们可以将所有的实体类放在一个包内,这样我们写一次混淆就行了。

#-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 {*;}

# Facebook

-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.** {*;}

#高德相关依赖

#集合包:3D地图3.3.2导航1.8.0定位2.5.0

-dontwarncom.amap.api.**

-dontwarncom.autonavi.**

-keepclass com.amap.api.**{*;}

-keepclass com.autonavi.**{*;}

#地图服务

-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.**{*;}

#导航

-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时需要配置Gson的解析对象及变量都不混淆。不然Gson会找不到变量。

#将下面替换成自己的实体类

-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

[email protected] class *

-keepclass io.realm.internal.Keep

[email protected] 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.** {*;}

#银联

-dontwarncom.unionpay.**

-keepclass com.unionpay.** {*;}

#友盟统计分析

-keepclassmembersclass * {public (org.json.JSONObject);}

-keepclassmembersenum com.umeng.analytics.** {

public static **[] values();

public static ** valueOf(java.lang.String);

}

#友盟自动更新

-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.*


好了,到这里此文已经结束了,如果帮到了您,请给我点个喜欢呗。


android studio apk混淆_第2张图片

你可能感兴趣的:(android studio apk混淆)