Android 的apk打包及混淆
打包Debug的的apk
1. 选择Build apk(s)打包app_debug.apk
2. 获取对应的app_debug.apk文件
打包Release的apk
1. 选择signedbundle/apk
2. 选择打包bundle还是apk
3. 选择已有的签名证书或者创建证书
注:演示选择创建证书
4. 创建证书,并点击ok
5. 生成release的apk包
代码混淆
1. build.gradle(app)
修改 minifyEnabled 为 true
2. 修改app下proguard-rules.pro文件,添加相关规则
- 通用混淆配置(app通用)
##################################通过混淆配置#################################
# 代码混淆压缩比,在0~7之间,默认为5,一般不做修改
-optimizationpasses 5
# 混合时不使用大小写混合,混合后的类名为小写
-dontusemixedcaseclassnames
# 指定不去忽略非公共库的类
-dontskipnonpubliclibraryclasses
# 这句话能够使我们的项目混淆后产生映射文件
# 包含有类名->混淆后类名的映射关系
-verbose
# 指定不去忽略非公共库的类成员
-dontskipnonpubliclibraryclassmembers
# 不做预校验,preverify是proguard的四个步骤之一,Android不需要preverify,去掉这一步能够加快混淆速度。
-dontpreverify
-dontoptimize
# 混淆时是否记录日志
-verbose
-ignorewarnings
# 保留Annotation不混淆
-keepattributes *Annotation*
# 避免混淆泛型
-keepattributes Signature-keepattributes Exceptions,InnerClasses
-dontnote com.google.vending.licensing.ILicensingService-dontnote com.android.vending.licensing.ILicensingService
# 抛出异常时保留代码行号
-keepattributes SourceFile,LineNumberTable-keepattributes Deprecated,Synthetic,EnclosingMethod
# 重命名抛出异常时的文件名称
-renamesourcefileattribute SourceFile
# 指定混淆是采用的算法,后面的参数是一个过滤器
# 这个过滤器是谷歌推荐的算法,一般不做更改
-optimizations !code/simplification/cast,!field/*,!class/merging/*
- APP需要保留的公共部分(通用)
- 四大组件以及子类;
- 自定义Application;
- support下面的继承子类
- R下面的资源
- native方法
- Activity中参数是view的方法
- 枚举
- 自定义View
- 序列化(Parcelable,Serializable)
- 带有回调函数(On*Listener,OnEvent)
- WebView
#############################################
#
# Android开发中一些需要保留的公共部分
#
#############################################
# 保留我们使用的四大组件,自定义的Application等等这些类不被混淆
# 因为这些子类都有可能被外部调用
-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 * extends android.view.View
#-keep public class com.android.vending.licensing.ILicensingService
# 保留support下的所有类及其内部类
-keep class android.support.** {
*;}
# 保留继承的
-keep public class * extends android.support.v4.**-keep public class * extends android.support.v7.**-keep public class * extends android.support.annotation.**
# 保留R下面的资源
-keep class **.R$* {
*;}
# 保留本地native方法不被混淆
-keepclasseswithmembernames class * {
native <methods>;}
# 保留在Activity中的方法参数是view的方法,
# 这样以来我们在layout中写的onClick就不会被影响
-keepclassmembers class * extends android.app