Android混淆笔记

概述

module的build.gradle中有以下内容
buildTypes {
release {
minifyEnabled true
proguardFiles getDefaultProguardFile(‘proguard-android.txt’), ‘proguard-rules.pro’
}
}
其中:
- minifyEnabled表示是否在生成release版本时混淆代码
- getDefaultProguardFile(‘proguard-android.txt’) 方法可从 Android SDK tools/proguard/ 文件夹获取默认的 ProGuard 设置。
- proguard-rules.pro 文件用于添加自定义 ProGuard规则。默认情况下,该文件位于模块根目录(build.gradle 文件旁)
每次构建时 ProGuard 都会输出下列文件:
- dump.txt 说明 APK 中所有类文件的内部结构。
- mapping.txt 提供原始与混淆过的类、方法和字段名称之间的转换。
- seeds.txt 列出未进行混淆的类和成员。
- usage.txt 列出从 APK 移除的代码。
这些文件保存在 /build/outputs/mapping/release/ 中。

混淆配置相关

SDK中有一个默认的配置文件存放在android-sdk-windows\tools\proguard,内容如下:

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
-dontoptimize
-dontpreverify
# Note that if you want to enable optimization, you cannot just
# include optimization flags in your own project configuration file;
# instead you will need to point to the
# "proguard-android-optimize.txt" file instead of this one from your
# project.properties file.

-keepattributes *Annotation*
-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
    native <methods>;
}

# keep setters in Views so that animations can still work.
# see http://proguard.sourceforge.net/manual/examples.html#beans
-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

# We want to keep methods in Activity that could be used in the XML attribute onClick
-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

-keepclassmembers class **.R$* {
    public static <fields>;
}

# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version.  We know about them, and they are safe.
-dontwarn android.support.**

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

一点点看上面内容
-dontusemixedcaseclassnames #是否使用大小写混合
-dontskipnonpubliclibraryclasses #非公开类也会被混淆
-verbose #打印混淆日志

-dontoptimize#关闭优化
-dontpreverify#混淆时是否做预校验

-keepattributes Annotation#指定混淆是采用的算法

-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService

keep表示不混淆,上边就是不混淆上述两个类

-keepclasseswithmembernames class * {
    native <methods>;
}

保留所有的本地native方法不被混淆

-keepclassmembers public class * extends android.view.View {
   void set*(***);
   *** get*();
}

不混淆所有继承android.view.View的类的set个get方法

-keepclassmembers class * extends android.app.Activity {
   public void *(android.view.View);
}

不混淆Activity里参数为View的方法,比如在layout中设置view的onclick就会出错

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

不混淆enum类的values和valueOf方法

-keepclassmembers class * implements android.os.Parcelable {
  public static final android.os.Parcelable$Creator CREATOR;
}

不混淆Parcelable的CREATOR字段

-keepclassmembers class **.R$* {
    public static <fields>;
}

不混淆所有R文件静态字段,混淆会影响资源的id

-dontwarn android.support.**

忽略这个包下面的警告

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep @android.support.annotation.Keep class * {*;}
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}
-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

keep注解可以作用于包、类、接口、注解类型、构造器、方法、字段上,哪个地方加上@keep哪个地方就不会混淆。

  -dontwarn com.squareup.retrofit2.**
  -keep class com.squareup.retrofit2.** { *;}

对于第三方依赖的库加上以上说明就可以了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值