前言
今天对项目进行了混淆配置,一直没开启项目混淆,深感可耻,当然有对项目进行加固。前面迟迟不肯行动,畏惧于他人的错误经验,说是混淆出现的坑,这种旁人的意识观,真是害人不浅,其实敢于“对症下药”的去解决,不会花很多时间。
-
代码混淆
在此我只是抛砖引玉,更高级的混淆知识,可以看如下文章
一篇文章带你领略Android混淆的魅力
在Android Studio中的混淆debug与release
https://zhuanlan.zhihu.com/p/49098044
https://www.jianshu.com/p/f67ac2b52813 -
资源混淆(推荐)
这个还是比较靠谱,可以做到,res下的资源被混淆
代码混淆
开启配置很简单,
比如在debug模式下设置minifyEnable 为true,然后在proguard-android.txt增加项目中对应的库的混淆规则。在处理这个过程中,你需要明白为什么要进行混淆。弄懂一些混淆的一些基础语法。碰到有的开源库又给出对应的混淆配置的写法,有的得自己去配置,比如gson,在官方文档中没有对应的混淆规则,
但是它需要去保证对应的bean不让混淆,所以有了以下的配置
-keep class com.qyy.cloudx.ui.me.bean.** {
*; }
至于要不要混淆,还是多去排查下,崩溃就是混淆了不该混淆的类
debug {
//是否优化zip
zipAlignEnabled true
// 移除无用的resource文件
shrinkResources true
minifyEnabled true
signingConfig signingConfigs.config
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
buildConfigField("boolean","LOG_DEBUG","${LOG_DEBUG}")
buildConfigField("boolean","BASE_URL_DEBUG","${BASE_URL_DEBUG}")
}
附上项目中的配置:(有点乱)
# 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
-optimizationpasses 5
# 混淆后类名都为小写 Aa aA
-dontusemixedcaseclassnames
# 指定不去忽略非公共库的类
-dontskipnonpubliclibraryclasses
#不做预校验的操作
-dontpreverify
# 混淆时不记录日志
-verbose
# 混淆采用的算法.
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
#保留代码行号,方便异常信息的追踪
-keepattributes SourceFile,LineNumberTable
#dump文件列出apk包内所有class的内部结构
-dump class_files.txt
#seeds.txt文件列出未混淆的类和成员
-printseeds seeds.txt
#usage.txt文件列出从apk中删除的代码
-printusage unused.txt
#mapping文件列出混淆前后的映射
-printmapping mapping.txt
# ----------------避免混淆Android基本组件
-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
#不提示V4包下错误警告
-dontwarn android.support.v4.**
#保持下面的V4兼容包的类不被混淆
-keep class android.support.v4.**{
*;}
#--------------避免混淆所有native的方法,涉及到C、C++
-keepclasseswithmembernames class * {
native <methods>;
}
#-------------避免混淆自定义控件类的get/set方法和构造函数
-keep public class * extends android.view.View{
*** get*();
void set*(***);
public <init>(android.content