通過(guò)命令行進(jìn)行R8混淆

背景介紹

Android Dex 的生成過(guò)程沟突,離不開 DX组底、Proguard丈积、D8筐骇、R8 等工具的使用,關(guān)于它們的關(guān)系與歷史背景江滨,不清楚的可以參考這篇博客【Android代碼壓縮工具R8詳解 android.enableR8=true

較低版本Gradle中R8的使用

Android Studio 升級(jí)到3.3及以上版本后铛纬,只需在項(xiàng)目的 gradle.properties 里加上:

android.enableR8=true

注意,AGP 7.0開始唬滑,已經(jīng)不再支持上述開關(guān)告唆,R8 成為默認(rèn)的混淆和dexing工具。

R8 普通模式是兼容 Proguard 的晶密,若原項(xiàng)目里已使用了proguard擒悬,直接啟用 R8 即可。同時(shí)稻艰,R8 也有完全模式懂牧,與 Proguard 不直接兼容∽鹞穑可以在 gradle.properties 文件中另外設(shè)置以下內(nèi)容:

android.enableR8.fullMode=true

R8與D8的使用

官方指導(dǎo)文檔

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.

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末僧凤,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子元扔,更是在濱河造成了極大的恐慌拼弃,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,277評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件摇展,死亡現(xiàn)場(chǎng)離奇詭異吻氧,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)咏连,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,689評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門盯孙,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人祟滴,你說(shuō)我怎么就攤上這事振惰。” “怎么了垄懂?”我有些...
    開封第一講書人閱讀 163,624評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵骑晶,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我草慧,道長(zhǎng)桶蛔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,356評(píng)論 1 293
  • 正文 為了忘掉前任漫谷,我火速辦了婚禮仔雷,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己碟婆,他們只是感情好电抚,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,402評(píng)論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著竖共,像睡著了一般蝙叛。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上公给,一...
    開封第一講書人閱讀 51,292評(píng)論 1 301
  • 那天甥温,我揣著相機(jī)與錄音,去河邊找鬼妓布。 笑死姻蚓,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的匣沼。 我是一名探鬼主播狰挡,決...
    沈念sama閱讀 40,135評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼释涛!你這毒婦竟也來(lái)了加叁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,992評(píng)論 0 275
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤唇撬,失蹤者是張志新(化名)和其女友劉穎它匕,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體窖认,經(jīng)...
    沈念sama閱讀 45,429評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡豫柬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,636評(píng)論 3 334
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了扑浸。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片烧给。...
    茶點(diǎn)故事閱讀 39,785評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖喝噪,靈堂內(nèi)的尸體忽然破棺而出础嫡,到底是詐尸還是另有隱情,我是刑警寧澤酝惧,帶...
    沈念sama閱讀 35,492評(píng)論 5 345
  • 正文 年R本政府宣布榴鼎,位于F島的核電站,受9級(jí)特大地震影響晚唇,放射性物質(zhì)發(fā)生泄漏巫财。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,092評(píng)論 3 328
  • 文/蒙蒙 一缺亮、第九天 我趴在偏房一處隱蔽的房頂上張望翁涤。 院中可真熱鬧,春花似錦萌踱、人聲如沸葵礼。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,723評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)鸳粉。三九已至,卻和暖如春园担,著一層夾襖步出監(jiān)牢的瞬間届谈,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,858評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工弯汰, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留艰山,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,891評(píng)論 2 370
  • 正文 我出身青樓咏闪,卻偏偏與公主長(zhǎng)得像曙搬,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子鸽嫂,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,713評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容