美文网首页
通过命令行进行R8混淆

通过命令行进行R8混淆

作者: 未子涵 | 来源:发表于2022-11-17 13:23 被阅读0次

背景介绍

Android Dex 的生成过程,离不开 DX、Proguard、D8、R8 等工具的使用,关于它们的关系与历史背景,不清楚的可以参考这篇博客【Android代码压缩工具R8详解 android.enableR8=true

较低版本Gradle中R8的使用

Android Studio 升级到3.3及以上版本后,只需在项目的 gradle.properties 里加上:

android.enableR8=true

注意,AGP 7.0开始,已经不再支持上述开关,R8 成为默认的混淆和dexing工具。

R8 普通模式是兼容 Proguard 的,若原项目里已使用了proguard,直接启用 R8 即可。同时,R8 也有完全模式,与 Proguard 不直接兼容。可以在 gradle.properties 文件中另外设置以下内容:

android.enableR8.fullMode=true

R8与D8的使用

官方指导文档

The R8 project uses depot_tools from the chromium project to manage dependencies. Install depot_tools and add it to your path before proceeding.

Installing depot_tools

Install depot_tools

LINUX / MAC

Clone the depot_tools repository:

$ git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git

Add depot_tools to the front of your PATH (you will probably want to put this in your ~/.bashrc or ~/.zshrc). Assuming you cloned depot_tools to /path/to/depot_tools:

$ export PATH=/path/to/depot_tools:$PATH
WINDOWS

Download the depot_tools bundle and extract it somewhere.

Warning
DO NOT use drag-n-drop or copy-n-paste extract from Explorer, this will not extract the hidden “.git” folder which is necessary for depot_tools to autoupdate itself. You can use “Extract all…” from the context menu though.
Add depot_tools to the front of your PATH (must be ahead of any installs of Python)

Downloading and building R8 project

$ git clone https://r8.googlesource.com/r8
$ cd r8
$ tools/gradle.py d8 r8

The tools/gradle.py script will bootstrap using depot_tools to download a version of gradle to use for building on the first run. This will produce two jar files: build/libs/d8.jar and build/libs/r8.jar.

If you can't produce these tow jar files caused by some problems, you can try to find them other ways:

  • d8: /path-to-android-sdk/build-tools/33.0.0/lib/d8.jar
  • r8: /Applications/Android Studio.app/Contents/plugins/android/lib/r8.jar
Running D8

The D8 dexer has a simple command-line interface with only a few options.

The most important option is whether to build in debug or release mode. Debug is the default mode and includes debugging information in the resulting dex files. Debugging information contains information about local variables used when debugging dex code. This information is not useful when shipping final Android apps to users and therefore, final builds should use the --release flag to remove this debugging information to produce smaller dex files.

Typical invocations of D8 to produce dex file(s) in the out directoy:

Debug mode build:

$ java -jar build/libs/d8.jar --output out input.jar

Release mode build:

$ java -jar build/libs/d8.jar --release --output out input.jar

The full set of D8 options can be obtained by running the command line tool with the --help option.

Running R8

R8 is a Proguard replacement for whole-program optimization, shrinking and minification. R8 uses the Proguard keep rule format for specifying the entry points for an application.

Typical invocations of R8 to produce optimized dex file(s) in the out directory:

$ java -jar build/libs/r8.jar --release --output out --pg-conf proguard.cfg input.jar

If you use r8.jar from Android Studio plugin folder, it will fail to process the above command with error:

no main manifest attribute, in ./r8.jar

It's because there aren't a main-class specification in MANIFEST.MF file of the jar, so you have to tell java where is main-class of r8.jar. There are two ways to do this:

  • adding main-class into MANIFEST.MF of r8.jar(by zip command)
Manifest-Version: 1.0
Main-Class: com.android.tools.r8.R8
// delete /META-INF/MANIFEST.MF
zip -d ./r8.jar  '/META-INF/MANIFEST.MF'
// add a new /META-INF/MANIFEST.MF
zip -u ./r8.jar ./META-INF/MANIFEST.MF

Notice that using -u requires the adding file path must be the same with the origin path in jar.

  • Use the following command to process:
java -cp r8.jar com.android.tools.r8.R8 --help
// example:java -cp r8.jar com.android.tools.r8.R8 --release --output . --pg-conf ./proguard-project.txt ./input.jar
java -cp r8.jar com.android.tools.r8.R8 --release --output out --pg-conf proguard.cfg input.jar

The full set of R8 options can be obtained by running the command line tool with the --help option.

dex is default output format, use --classfile option to get jar formatted output:

java -jar r8.jar --classfile --release --output ./class-pg.jar --pg-conf proguard.cfg input.jar
Testing

Typical steps to run tests:

$ tools/test.py --no_internal

The tools/test.py script will use depot_tools to download a lot of tests and test dependencies on the first run. This includes prebuilt version of the art runtime on which to validate the produced dex code.

相关文章

  • android 新一代混淆技术

    之前都是用Proguard进行代码混淆压缩,从android studio 3.3开始,提供R8配合Proguar...

  • Android混淆详解

    1.混淆的作用 利用Proguard或者R8工具,对代码进行重命名,并删掉没有被引用的类、字段或者方法。对无用资源...

  • Android 代码混淆(二)

    文章转自我个人博客 Android 代码混淆(一) 中已经记录并走了混淆的整个流程,用命令行进行混淆的操作,并验证...

  • Android R8

    版本:1.4.94地址:r8 介绍 r8包含了D8 的功能, 实现了对 java 字节码优化,混淆并转换成 dex...

  • 干货!如何使用R8来压缩你的应用

    人们更倾向于安装并保留较小和安装占用空间更小的应用,在新兴市场中尤为明显。有了 R8 编译器,您可以通过压缩、混淆...

  • Android使用R8压缩,混淆,优化App

    在Android开发中,为了使App尽可能小,可以使用R8来压缩,混淆,优化App,当使用Android Grad...

  • Bugly使用篇之Java错误堆栈还原

    前言 前面介绍了 Android混淆代码错误堆栈还原,相信大家已经知道如何通过Retrace在本地进行混淆代码还原...

  • Sophix热更新踩坑记录

    版本1.0 和1.0.0不一样image.png 这三个值别配错了image.png 关闭R8混淆, andro...

  • Android使用mapping文件还原混淆代码

    还原混淆文件 ProGuard 提供了命令行来还原混淆后的代码,目录如下: /tools/proguard/bin...

  • vue-cli 3.0(1)

    cli 是 Command Line Interface,也就是命令行接口,所谓接口就是我们可以通过命令行方式进行...

网友评论

      本文标题:通过命令行进行R8混淆

      本文链接:https://www.haomeiwen.com/subject/kkrpxdtx.html