Android-混淆学习

前言

今天对项目进行了混淆配置,一直没开启项目混淆,深感可耻,当然有对项目进行加固。前面迟迟不肯行动,畏惧于他人的错误经验,说是混淆出现的坑,这种旁人的意识观,真是害人不浅,其实敢于“对症下药”的去解决,不会花很多时间。

参考文章
APP瘦身大法–AndResGuard的使用

代码混淆

开启配置很简单,
比如在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
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值