当前位置:   article > 正文

安卓混淆java代码_Android R8代码混淆

android 只混淆java代码

Android Gradle插件升级至3.4.0版本之后,带来一个新特性-新一代混淆工具R8,做为D8的升级版替代Proguard;在应用压缩、应用优化方面提供更极致的体验。

3 k# A- k7 Y6 L& A1 ZR8 和 Proguard

2 u9 z+ ]( O* |0 o. y

1 t. K' y' T2 i) i6 Z: q  CR8 一步到位地完成了所有的缩减(shrinking),去糖(desugaring)和 转换成 Dalvik 字节码(dexing )过程。0 d* x- n8 B1 w9 ?1 u2 Z

缩减(shrinking)过程实现以下三个重要的功能:

' L" l# H) O- K& l! H/ J% B) O2 D4 M) O  r  C, P; L

代码缩减:从应用及其库依赖项中检测并安全地移除未使用的类、字段、方法和属性。

* t  p0 e5 u8 S2 b8 z6 H; J

资源缩减:从封装应用中移除不使用的资源,包括应用库依赖项中的不使用的资源。6 p& [, M  ]7 }+ P

优化:检查并重写代码,以进一步减小应用的 DEX 文件的大小。" p! o8 S9 F( T

混淆:缩短类和成员的名称,从而减小 DEX 文件的大小。, p! I4 ^, k& v" I7 NR8 和当前的代码缩减解决方案 Proguard 相比,R8 可以更快地缩减代码,同时改善输出大小。下面将通过几张数据图来对比(数据源自于 benchmark):

5 q" Y7 V. r  u$ W* `+ I3 M4 c0 x) z' o

65556f982685a310ffe99a28e18d572e.png

7be12d108f6d7ea5e04a0628d949a702.png

( s9 k5 n3 `: b: {. f% K

d961e41bdaf6fe719b2bc50237f39df1.pngR8混淆使用

# V* n) w! c- `9 a# H& d& P5 ^. E% a! S1 r$ P2 h% C( s. j

R8的使用非常简单,使用方式与Proguard并无差异,Android Studio创建项目时默认是关闭的,因为这会加长工程打包时间,所以开发阶段不建议开启。开启方式如下:

R5 z# r7 t7 @* ~: xbuildTypes {        release {            // 启用代码收缩、混淆和优化。            minifyEnabled true            // 启用资源缩减            shrinkResources true            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'        }    }可以看到gradle中加载了两个混淆配置文件, 其中 proguard-rules.pro 供开发者自定义混淆规则;proguard-android-optimize.txt 这是默认的配置文件,包含一些通用的混淆规则,在sdk/tools/proguard目录下,其中包含的内容如下:$ X6 f1 H& u  c9 u9 m4 _

# This is a configuration file for ProGuard.# http://proguard.sourceforge.net/index.html#manual/usage.html## This file is no longer maintained and is not used by new (2.2+) versions of the# Android plugin for Gradle. Instead, the Android plugin for Gradle generates the# default rules at build time and stores them in the build directory.# Optimizations: If you don't want to optimize, use the# proguard-android.txt configuration file instead of this one, which# turns off the optimization flags.  Adding optimization introduces# certain risks, since for example not all optimizations performed by# ProGuard works on all versions of Dalvik.  The following flags turn# off various optimizations known to have issues, but the list may not# be complete or up to date. (The "arithmetic" optimization can be# used if you are only targeting Android 2.0 or later.)  Make sure you# test thoroughly if you go this route.-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*-optimizationpasses 5-allowaccessmodification-dontpreverify# The remainder of this file is identical to the non-optimized version# of the Proguard configuration file (except that the other file has# flags to turn off optimization).-dontusemixedcaseclassnames-dontskipnonpubliclibraryclasses-verbose-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 ;}# 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 ;}# 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 ;}-keepclasseswithmembers class * {    @android.support.annotation.Keep ;}-keepclasseswithmembers class * {    @android.support.annotation.Keep (...);}ProGuard常用规则

, n. u* F2 A$ T5 T- A5 S# e8 x$ e8 W( I9 {4 E  g, l: \

关闭压缩! ?3 l3 D3 M" p  f! ]

-dontshrink关闭代码优化

- S9 Y& N! B3 Y" F* _% T-dontoptimize关闭混淆7 n) S* ]- Z$ a2 r2 y

-dontobfuscate指定代码优化级别,值在0-7之间,默认为5

/ F0 O; P" k6 W4 \, g$ Z-optimizationpasses 5混淆时不使用大小写混合类名( R$ D1 C) w  ^) q- |4 P

-dontusemixedcaseclassnames不忽略库中的非public的类

6 K. T4 J, W5 O1 G# ~# O0 u-dontskipnonpubliclibraryclasses不忽略库中的非public的类成员; x$ m8 V5 v5 e1 u- }+ R; k

-dontskipnonpubliclibraryclassmembers输出详细信息

?% f- @& Z, E  k-verbose不做预校验,预校验是作用在Java平台上的,Android平台上不需要这项功能,去掉之后还可以加快混淆速度

% e- p3 n, K0 r2 h- z; M3 B-dontpreverify保持指定包下的类名,不包括子包下的类名

( }& h2 R0 ~. F, n& d-keep class com.xy.myapp*保持指定包下的类名,包括子包下的类名

3 T) B5 V3 ]: {$ x% ^-keep class com.xy.myapp**保持指定包下的类名以及类里面的内容

& ?' x: C9 z' M8 u# S4 q8 F-keep class com.xy.myapp.* {*;}保持所有继承于指定类的类

% Z0 X/ f6 X, j. C/ ~-keep public class * extends android.app.Activity其它keep方法:

7 n( _5 N# b$ Z; Y保留防止被移除或者被混淆防止被混淆类和类成员-keep-keepnames仅类成员-keepclassmembers-keepclassmembernames如果拥有某成员,保留类和类成员-keepclasseswithmembers-keepclasseswithmembernames如果我们要保留一个类中的内部类不被混淆则需要用$符号,如下例子表示保持MyClass内部类JavaScriptInterface中的所有public内容。

" t& C2 l8 Z& v6 X4 @6 z4 P-keepclassmembers class com.xy.myapp.MyClass$JavaScriptInterface {   public *;}保持指定类的所有方法

2 h# C1 B' k! Y9 C7 n, e-keep class com.xy.myapp.MyClass {    public ;}保持指定类的所有字段' O, r% _2 l( N( ~

-keep class com.xy.myapp.MyClass {    public ;}保持指定类的所有构造器3 }3 |5 ?' ?# g/ k

-keep class com.xy.myapp.MyClass {    public ;}保持用指定参数作为形参的方法

- q3 @2 A- `& l- d" N$ f+ c. E-keep class com.xy.myapp.MyClass {    public (java.lang.String);}类文件除了定义类,字段,方法外,还为它们附加了一些属性,例如注解,异常,行号等,优化操作会删除不必要的属性,使用-keepattributes可以保留指定的属性

3 t+ \4 b0 g* c6 Z0 y6 m-keepattributes Exceptions,InnerClasses,Signature,Deprecated,                SourceFile,LineNumberTable,*Annotation*,EnclosingMethod使指定的类不输出警告信息" V7 W' y# [; x

-dontwarn com.squareup.okhttp.**常用混淆模版( J6 D' B1 }; k8 @

' o) x2 H6 M  o: j/ g$ F# 指定代码的压缩级别-optimizationpasses 5     # 不忽略库中的非public的类成员-dontskipnonpubliclibraryclassmembers # google推荐算法-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*# 避免混淆Annotation、内部类、泛型、匿名类-keepattributes *Annotation*,InnerClasses,Signature,EnclosingMethod# 抛出异常时保留代码行号-keepattributes SourceFile,LineNumberTable# 保持四大组件-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.**# 保持自定义控件-keep public class * extends android.view.View{    *** get*();    void set*(***);    public (android.

% c8 i/ b+ e% C7 {; cJava吧 收集整理 java论坛 www.java8.com

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/887262
推荐阅读
相关标签
  

闽ICP备14008679号