Android Studio 混淆,多渠道打包,反编译 使用

来源:互联网 发布:阿里云ecs地域选择 编辑:程序博客网 时间:2024/05/20 07:14

混淆

简单配置

build.gradle的android节点下添加:

buildTypes {        release {            minifyEnabled true            proguardFiles 'proguard-rules.pro'        }        debug {            minifyEnabled true            proguardFiles 'proguard-rules.pro'        }    }

或者

buildTypes {        release {            minifyEnabled true            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }        debug {            minifyEnabled true            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }

这样配置需要通过Build-Generate Signed Apk…进行对应的release或debug打包。proguard-rules.pro文件的配置稍候会给出。

带签名的配置

和简单配置的区别是即使在debug版本下生成的包也是带签名的。

首先需要在app目录下添加签名文件,注意一定是要在app目录下,如图:
添加签名文件

build.gradle配置:

buildTypes {        debug {            minifyEnabled false            signingConfig signingConfigs.debug//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'            proguardFiles 'proguard-rules.pro'        }        release {            minifyEnabled true            signingConfig signingConfigs.debug//            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'            proguardFiles 'proguard-rules.pro'        }    }    signingConfigs {        debug {            storeFile file('newpro.keystore')//你的签名文件            storePassword "123456" //签名密码            keyAlias "newpro" //Alias名称            keyPassword "123456" //Alias密码        }    }

这样生成的包就是带有签名文件的包。

混淆配置文件

现在来配置proguard-rules.pro文件:

-optimizationpasses 5-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-verbose-ignorewarnings-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*-keep class android.** {*; }-keep public class * extends android.app.Activity-keep public class * extends android.view.View-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.app.backup.BackupAgentHelper-keep public class * extends android.preference.Preference-keep public class com.android.vending.licensing.ILicensingService-keepclasseswithmembernames class * {    native <methods>;}-keepclasseswithmembers class * {    public <init>(android.content.Context, android.util.AttributeSet);}-keepclasseswithmembers class * {    public <init>(android.content.Context, android.util.AttributeSet, int);}-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 * {   public <init> (org.json.JSONObject);}-keepclassmembers class fqcn.of.javascript.interface.for.webview {   public *;}##########################################################################################不混淆注解类型# For using Eventbus @Subscribe annotation-keepattributes *Annotation*##---------------Begin: proguard configuration for glide  -----------keep public class * implements com.bumptech.glide.module.GlideModule-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {     **[] $VALUES;     public *;}##---------------End: proguard configuration for glide  ----------##---------------Begin: proguard configuration for xutils  -----------keepattributes Signature,*Annotation*-keep public class org.xutils.** {    public protected *;}-keep public interface org.xutils.** {    public protected *;}-keepclassmembers class * extends org.xutils.** {    public protected *;}-keepclassmembers @org.xutils.db.annotation.* class * {*;}-keepclassmembers @org.xutils.http.annotation.* class * {*;}-keepclassmembers class * {    @org.xutils.view.annotation.Event <methods>;}##---------------End: proguard configuration for xutils  ----------##---------------Begin: proguard configuration for BQMM  -----------dontwarn com.melink.**-dontwarn com.thirdparty.**-keep class com.melink.** {*;}-keep class com.thirdparty.** {*;}##---------------End: proguard configuration for BQMM  ----------##---------------Begin: proguard configuration for Umeng  -----------keep public class com.umeng.** { *;}-dontwarn com.tencent.mm.**-keep class com.tencent.mm.**{*;}-dontwarn com.tencent.android.**-keep class com.tencent.android.**{*;}-keepclassmembers class * {   public <init>(org.json.JSONObject);}##---------------End: proguard configuration for Umeng  -----------dontwarn org.apache.http.**-keep class org.apache.http.** { *; }-keep class de.greenrobot.event.** {*;}

“#########################################################################################”上面的是基本配置,必须有的。之下的要根据你项目集成的jar或者第三方库相应添加。

这里有个技巧,混淆打包的时候难免出现错误,像下面截图这种错误是很常见的。
混淆报错1
混淆报错2

看起来有好多的警告,其实就只涉及到org.apache.http这个类,我们只要不混淆这个类就可以了。
此时我们可以根据报错提示来决定不混淆哪个类,逐个排除。比如针对截图的情况,可以这样配置,就ok了。

#按压警告-dontwarn org.apache.http.**#保持类不被混淆-keep class org.apache.http.** { *; }

如果你没有混淆(minifyEnabled false),那就不需要考虑了。
需要说明一下,混淆完以后整个项目需要全面测试一下,尤其是第三方的东西,因为有的第三方的在混淆的是很不会报错,有的类也只有在运行起来的时候才会发现没有相关类。

多渠道打包

在清单文件application节点下配置:

 <meta-data                 android:name="UMENG_CHANNEL"      android:value="${UMENG_CHANNEL_VALUE}" />

build.gradle的android节点下添加:

productFlavors {        baidu {}//百度        anzhi {}//安智        yingyongbao {}//腾讯应用宝        qihu360 {}//360        wandoujia {}//豌豆荚        mumayi {}//木蚂蚁        appchina {}//应用汇        leshi {}//乐视        huawei {}//华为        xiaomi {}//小米        meizu {}//魅族        oppo {}//oppo        vivo {}//vivo        pp {}//pp助手        gfan {}//机锋市场        nduo {}//n多市场        lenovo {}//乐商店        eoe {}//优亿市场        gionee {}//金立        coolpad {}//酷派        myzte {}//中兴        productFlavors.all { flavor ->            flavor.manifestPlaceholders = [UMENG_CHANNEL_VALUE: name]        }    }

显然,两者之间是通过UMENG_CHANNEL_VALUE建立联系的,此时我们Build-Generate Signed Apk…,在打包的时候会发现多了一栏Flavors,列表项就是我们添加的打包渠道,选择你需要的渠道包,点击Finish,studio便会开始自动打包模式。

多渠道打包

打包完成后apk文件会自动命名:
打包完成

反编译

有混淆就有反编译,我们毕竟要查看一下混淆成果。我给大家提供了反编译的工具下载:
点击:反编译工具下载

安装产生这么两个东西,一个是产生jar文件的,一个是打开看混淆文件的。
反编译工具

使用说明:
*首先将apk文件,将后缀改为zip,解压,得到其中的classes.dex,它就是java文件编译再通过dx工具打包而成的;
将classes.dex拖动到dex2jar.bat上,会弹出一个命令框,无需理会,等待命令框消失。
消失以后在classes.dex所在的目录下会生成一个jar文件classes-dex2jar.jar
然后用jd_gui.exe直接File-Open File…打开即可。*

拖动classes.dex:
拖动classes.dex

正在生成jar文件,等待3~4秒:
正在生成jar文件

在和classes.dex同级目录下生成jar文件:
生成jar文件

下面这两个截图分别是未混淆和混淆后的,效果很显然:
未混淆的

混淆后的

更加详细的反编译教程请参考http://www.cnblogs.com/common1140/p/5198460.html,可以反编译出res和manifest文件,以及有Android Killer的详细使用说明。

谢谢阅览!!

0 0