当前位置: 移动技术网 > IT编程>移动开发>Android > Android Studio多渠道批量打包及代码混淆

Android Studio多渠道批量打包及代码混淆

2019年07月24日  | 移动技术网IT编程  | 我要评论

梁宏达博客,我查,刑事情报科国语

一、批量打包

1、集成了友盟统计,并在androidmanifest.xml中添加了如下代码

<meta-data
android:name="umeng_channel"
android:value="${channel_value}"/>

2、在app的build.gradle的android标签下添加如下代码:

productflavors {
myapp {}
_360 {}
appchina {}
hiapk {}
}
productflavors.all {
flavor -> flavor.manifestplaceholders = [channel_value: name]
}

3、检查是否配置了gradle path环境变量,命令行下执行gradle -v,如果不能识别则到androidstudio的安装目录下找到gradle目录,把其下的bin目录添加到path中,然后执行如下命令:

gradle assemblerelease

坐等编译打包成功,不知道是不是我第一次用的原因,执行完这个命令后一直在download什么东西,过了大概四五分钟,开始打包不同渠道的apk了,最终如下图所示:

这里写图片描述

这里写图片描述

以上这是通过命令行打包,当然也可以直接通过ui方式,选择菜单build–>generate signed apk–>选择创建好的密钥keystore(没有就创建一个),然后点击next就会弹出如下图所示的对话框:

这里写图片描述 

这里也可以选择渠道或者build type,flavors最少选择一个,点击finish同样可以多渠道打包。

感慨一下:以前使用eclipse多渠道打包的时候感觉好麻烦,现在as的多渠道打包感觉好方便快捷。

二、代码混淆

1、把build.gradle中的buildtypes下的 minifyenable置为true

shrinkresources false

上面这行代码是为了溢出未使用的不必要的资源文件以便减少最后安装包的体积大小,在release模式下开启为true,debug下不需要设置true,不然为报warnings

2、编辑app目录下的proguard-rules.pro文件如下:

# add project specific proguard rules here.
# by default, the flags in this file are appended to flags specified
# in e:\androisdk/tools/proguard/proguard-android.txt
# you can edit the include path and order by changing the proguardfiles
# directive in build.gradle.
#
# for more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# add any project specific keep options here:
# if your project uses webview with js, uncomment the following
# and specify the fully qualified class name to the javascript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
#----------------通用混淆start---------------------
-optimizationpasses 5 # 指定代码的压缩级别
-dontusemixedcaseclassnames # 是否使用大小写混合
-dontpreverify # 混淆时是否做预校验
-verbose # 混淆时是否记录日志
-ignorewarnings # 忽略警告
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/* # 混淆时所采用的算法
-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.app.backup.backupagenthelper # 保持哪些类不被混淆
-keep public class * extends android.preference.preference # 保持哪些类不被混淆
-keep public class com.android.vending.licensing.ilicensingservice # 保持哪些类不被混淆
-keepclasseswithmembernames class * { # 保持 native 方法不被混淆
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 * { # 保持枚举 enum 类不被混淆
public static **[] values();
public static ** valueof(java.lang.string);
}
-keep class * implements android.os.parcelable { # 保持 parcelable 不被混淆
public static final android.os.parcelable$creator *;
}
#----------------通用混淆end---------------------
#----------------友盟5.0混淆start---------------------
-dontshrink
-dontoptimize
-dontwarn com.google.android.maps.**
-dontwarn android.webkit.webview
-dontwarn com.umeng.**
-dontwarn com.tencent.weibo.sdk.**
-dontwarn com.facebook.**
-keep enum com.facebook.**
-keepattributes exceptions,innerclasses,signature
-keepattributes *annotation*
-keepattributes sourcefile,linenumbertable
-keep public interface com.facebook.**
-keep public interface com.tencent.**
-keep public interface com.umeng.socialize.**
-keep public interface com.umeng.socialize.sensor.**
-keep public interface com.umeng.scrshot.**
-keep public class com.umeng.socialize.* {*;}
-keep public class javax.**
-keep public class android.webkit.**
-keep class com.facebook.**
-keep class com.facebook.** { *; }
-keep class com.umeng.scrshot.**
-keep public class com.tencent.** {*;}
-keep class com.umeng.socialize.sensor.**
-keep class com.umeng.socialize.handler.**
-keep class com.umeng.socialize.handler.*
-keep class com.tencent.mm.sdk.modelmsg.wxmediamessage {*;}
-keep class com.tencent.mm.sdk.modelmsg.** implements com.tencent.mm.sdk.modelmsg.wxmediamessage$imediaobject {*;}
-keep class im.yixin.sdk.api.yxmessage {*;}
-keep class im.yixin.sdk.api.** implements im.yixin.sdk.api.yxmessage$yxmessagedata{*;}
-dontwarn twitter4j.**
-keep class twitter4j.** { *; }
-keep class com.tencent.** {*;}
-dontwarn com.tencent.**
-keep public class com.umeng.soexample.r$*{
public static final int *;
}
-keep public class com.umeng.soexample.r$*{
public static final int *;
}
-keep class com.tencent.open.tdialog$*
-keep class com.tencent.open.tdialog$* {*;}
-keep class com.tencent.open.pkdialog
-keep class com.tencent.open.pkdialog {*;}
-keep class com.tencent.open.pkdialog$*
-keep class com.tencent.open.pkdialog$* {*;}
-keep class com.sina.** {*;}
-dontwarn com.sina.**
-keep class com.alipay.share.sdk.** {
*;
}
-keepnames class * implements android.os.parcelable {
public static final ** creator;
}
-keep class com.linkedin.** { *; }
-keepattributes signature
#----------------友盟5.0混淆end---------------------
#----------------高德地图混淆start---------------------
-dontwarn com.amap.api.mapcore2d.**
#定位
-keep class com.amap.api.location.**{*;}
-keep class com.amap.api.fence.**{*;}
-keep class com.autonavi.aps.amapapi.model.**{*;}
#搜索
-keep class com.amap.api.services.**{*;}
#2d地图
-keep class com.amap.api.maps2d.**{*;}
-keep class com.amap.api.mapcore2d.**{*;}
#----------------高德地图混淆end---------------------
#----------------极光推送混淆start---------------------
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
#gson
-dontwarn com.google.**
-keep class com.google.gson.** {*;}
#protobuf
-dontwarn com.google.**
-keep class com.google.protobuf.** {*;}
#----------------极光推送混淆end---------------------
#----------------mob短信验证混淆start---------------------
-keep class android.net.http.sslerror
-keep class android.webkit.**{*;}
-keep class cn.sharesdk.**{*;}
-keep class cn.smssdk.**{*;}
-keep class com.mob.**{*;}
#----------------mob短信验证混淆end---------------------

代码虽然多,但也很容易明白,首先通用混淆一定要有(参考android studio实现代码混淆),其他的就根据项目里添加了哪些第三方库就到第三方开发者平台下找到他们提供的混淆内容即可

a.如果混淆打包成功后,应用跑起来报了如下图的错误:

这里写图片描述 

那肯定是因为没有添加mob短信验证的混淆代码,因为在他们官方的集成文档里没提到,不过去论坛里倒是找到了解答,就如上面代码最后一段。

b.如果数据的封装用到了gson,那么也要添加混淆,包括项目中所有的javabean,也就是实体类,混淆代码如下(可参考官方github说明):

##---------------begin: proguard configuration for gson ----------
# gson uses generic type information stored in a class file when working with fields. proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes signature
# gson specific classes
-keep class sun.misc.unsafe { *; }
#-keep class com.google.gson.stream.** { *; }
# application classes that will be serialized/deserialized over gson
#-keep class com.google.gson.examples.android.model.** { *; }
##---------------end: proguard configuration for gson ----------
-keep class com.xxx.xxxxxxxxx.bean.** { *; } # 保持项目中的实体类不被混淆

c.如果你的项目中集成了mpandroidchart图表,那么也得加上混淆,参考这里,代码如下:

-keep class com.github.mikephil.charting.** { *; } # 确保mpandroidchart加载动画可用

d.如果你的项目中使用了webview并且自定义了javainterface有js交互的,那么需要添加如下混淆代码:

#----------------js混淆start---------------------
-keepclassmembers class com.xxxxx.xxx.xxxactivity$myjavascriptinterface {
public *;
}
-keepattributes *annotation*
-keepattributes *javascriptinterface*
#----------------js混淆end---------------------

e.如果在生成apk失败,报了很多警告,那么可以添加这行代码忽略警告

-ignorewarnings # 忽略警告

等待生成apk成功后,看看是否异常,如果运行到某一步崩溃了,那么再根据具体的崩溃信息去添加相应的混淆规则即可,加了这句也是为了方便调试。

3、执行打包即可

4、对打包所得apk用dex2jar以及jdgui反编译后,查看java源码,发现确实有很多文件被混淆了。

以上所述是小编给大家介绍的android studio多渠道批量打包及代码混淆,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网