Android上架相关(代码混淆和反编译工具的使用,签名,打包,加固)
步骤:混淆 ,签名,打包(多渠道打包) ,加固
二次打包 :也就是 将已经打包的apk再次打包 生成 山寨版的 apk
对抗重打包:通过 判断 签名的密钥和 当前app的 签名密钥是否相同 若不相同则退出等操作
多渠道打包: 即不同的应用市场 去加上各自的标识 以便统计
生成 对应渠道的发行版 app
代码混淆(proguard)
作用:
1 可以混淆代码,提高反编译的难度
2 压缩(Shrink):检测并移除代码中无用的类、字段、方法和特性(Attribute)
3 优化(Optimize):对字节码进行优化,移除无用的指令
4 预检(Preveirfy):在Java平台上对处理后的代码进行预检,确保加载的class文件是可执行的
要保留:
四大组件 (因为在Manifest中注册了)
C++/C 引用的java类
R文件
Bean类
内嵌类
第三方库所需的混淆规则 也记得加上
JNI中的类
WebView中JavaScript调用的方法
…
在module的build.gradle中
buildTypes {
release {
minifyEnabled true
shrinkResources true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
开启混淆 并 开启 压缩,这样debug模式下就不会混淆 影响 debug的 编译速度
shrinkResources true
对资源进行压缩 对没有使用的资源文件(如图片) 不进行 构建加载
在 proguard-rules.pro 文件中 ,使用网络验证过很多次的 混淆规则与保留规则
如我的项目添加的混淆为
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# 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 *;
#}
# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
# 代码混淆压缩比,在0~7之间,默认为5,一般不做修改
-optimizationpasses 5
# 混合时不使用大小写混合,混合后的类名为小写
-dontusemixedcaseclassnames
# 指定不去忽略非公共库的类
-dontskipnonpubliclibraryclasses
# 这句话能够使我们的项目混淆后产生映射文件
# 包含有类名->混淆后类名的映射关系
-verbose
# 指定不去忽略非公共库的类成员
-dontskipnonpubliclibraryclassmembers
# 不做预校验,preverify是proguard的四个步骤之一,Android不需要preverify,去掉这一步能够加快混淆速度。
-dontpreverify
# 保留Annotation不混淆
-keepattributes *Annotation*,InnerClasses
# 避免混淆泛型
-keepattributes Signature
# 抛出异常时保留代码行号
-keepattributes SourceFile,LineNumberTable
# 指定混淆是采用的算法,后面的参数是一个过滤器
# 这个过滤器是谷歌推荐的算法,一般不做更改
-optimizations !code/simplification/cast,!field/*,!class/merging/*
#############################################
#
# Android开发中一些需要保留的公共部分
#
#############################################
# 保留我们使用的四大组件,自定义的Application等等这些类不被混淆
# 因为这些子类都有可能被外部调用
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Appliction
-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.Activity{
public void *(android.view.View);
}
# 保留枚举类不被混淆
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# 保留我们自定义控件(继承自View)不被混淆
-keep public class * extends android.view.View{
*** get*();
void set*(***);
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
# 保留Parcelable序列化类不被混淆
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
# 保留Serializable序列化的类不被混淆
-keepnames class * implements java.io.Serializable
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
!static !transient <fields>;
!private <fields>;
!private <methods>;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
# 对于带有回调函数的onXXEvent、**On*Listener的,不能被混淆
-keepclassmembers class * {
void *(**On*Event);
void *(**On*Listener);
}
# webView处理,项目中没有使用到webView忽略即可
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
#-keepclassmembers class * extends android.webkit.webViewClient {
# public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
# public boolean *(android.webkit.WebView, java.lang.String);
#}
#-keepclassmembers class * 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 class com.biniu.bottomnavigationbardemo.Bean.**{ *; }
-keep class com.biniu.bottomnavigationbardemo.OA.Bean.**{ *; }
#-----------处理第三方依赖库---------
# Umeng
-keep class com.umeng.commonsdk.** {*;}
# 高德
#3D 地图 V5.0.0之后:
-keep class com.amap.api.maps.**{*;}
-keep class com.autonavi.**{*;}
-keep class com.amap.api.trace.**{*;}
#定位
-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.**{*;}
#JPush 极光 还要下载 proguard.jar 去 替换 androidstudio的tool\proguard\lib的
-dontoptimize
-dontpreverify
-dontwarn cn.jpush.**
-keep class cn.jpush.** { *; }
-keep class * extends cn.jpush.android.helpers.JPushMessageReceiver { *; }
-dontwarn cn.jiguang.**
-keep class cn.jiguang.** { *; }
# ButterKnife
-keep public class * implements butterknife.Unbinder {
public <init>(**, android.view.View);
}
-keep class butterknife.*
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
# Gson
-keepattributes *Annotation*
-keep class sun.misc.Unsafe { *; }
-keep class com.google.gson.stream.** { *; }
-keepattributes EnclosingMethod
# OkHttp3
-dontwarn okhttp3.logging.**
-keep class okhttp3.internal.**{*;}
-keep class com.squareup.okhttp3.** { *;}
-dontwarn okio.**
-dontwarn javax.annotation.**
-dontwarn javax.inject.**
# Retrofit
-dontwarn retrofit2.**
-keep class retrofit2.** { *; }
-keepattributes Exceptions
# RxJava RxAndroid
-dontwarn sun.misc.**
-keepclassmembers class rx.internal.util.unsafe.*ArrayQueue*Field* {
long producerIndex;
long consumerIndex;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueProducerNodeRef {
rx.internal.util.atomic.LinkedQueueNode producerNode;
}
-keepclassmembers class rx.internal.util.unsafe.BaseLinkedQueueConsumerNodeRef {
rx.internal.util.atomic.LinkedQueueNode consumerNode;
}
-dontnote rx.internal.util.PlatformDependent
#AVLoadingIndicatorView 等待样式库
-keep class com.wang.avi.** { *; }
-keep class com.wang.avi.indicators.** { *; }
# Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
**[] $VALUES;
public *;
}
# for DexGuard only 一款收费的加固平台
#-keepresourcexmlelements manifest/application/meta-data@value=GlideModule
# Ucrop
-dontwarn com.yalantis.ucrop**
-keep class com.yalantis.ucrop** { *; }
-keep interface com.yalantis.ucrop** { *; }
#打包时 出现 wraning无法打包 加上 忽略警告
-ignorewarnings
问题:
Toast显示不了 也不能跳转 但是可以正确网络请求
混淆出错
每当 用一个开源库 要了解他的开源协议 要去加 混淆
反编译工具:
apktool
可以获得项目各个dpi图片,及媒体文件,可看到AndroidManifest.xml配置文件
apktool 可用于二次打包 也就是 将已经打包的apk再次打包 生成 山寨版的 apk
按照官方文档 步骤下载
然后对 apk的目录 右键 打开cmd 然后输入 apktool d apk名称即可
dexjar2
将字节码反编译为java代码
签名,打包参考:
https://blog.csdn.net/weixin_37577039/article/details/78900809
加固
这里使用的是360加固保:http://jiagu.360.cn/
对 release包 去一键加固
还要记得去重签名
下载签名工具 加载 签名文件 输入密码就可以对 软件签名