• 通过命令行进行R8混淆


    背景介绍

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

    较低版本Gradle中R8的使用

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

    android.enableR8=true
    
    • 1

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

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

    android.enableR8.fullMode=true
    
    • 1

    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
    
    • 1

    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
    
    • 1
    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
    
    • 1
    • 2
    • 3

    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
    
    • 1

    Release mode build:

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

    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
    
    • 1

    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
    
    • 1

    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
    
    • 1
    • 2
    // 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
    
    • 1
    • 2
    • 3
    • 4

    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
    
    • 1
    • 2
    • 3

    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
    
    • 1
    Testing

    Typical steps to run tests:

    $ tools/test.py --no_internal
    
    • 1

    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.

  • 相关阅读:
    ProTable高级表格获取表单数据
    Java IO流
    vue内容自适应方法
    React 中 useEffect
    精研科技融入汽车内饰 Laedana打造车内空间新生态
    一个数组去重(filter函数)
    LeetCode //C++ - 427. Construct Quad Tree
    Arm Cache学习资料大汇总
    能解决你80%关于存储的疑惑
    外包干了2个月,技术倒退2年。。。
  • 原文地址:https://blog.csdn.net/lovelease/article/details/127920621