I'm trying to use r8
without much success:
$ wget https://repo1.maven.org/maven2/com/mikepenz/fastadapter/3.2.7/fastadapter-3.2.7.aar
$ wget https://maven.google.com/com/android/support/recyclerview-v7/26.0.1/recyclerview-v7-26.0.1.aar
$ unzip fastadapter-3.2.7.aar && mv classes.jar fastadapter.jar
$ unzip recyclerview-v7-26.0.1.aar && mv classes.jar recycleview.jar
$ echo "-dontwarn java.lang.Object" > proguard.cfg
$ java -jar build/libs/r8.jar --lib android-4.1.1.4.jar --lib recycleview.jar --release --output . --pg-conf proguard.cfg fastadapter.jar
$ dexdump -d classes.dex | grep "insns size" | wc -l
Unable to open 'classes.dex' : No such file or directory
0
But with d8
it works perfect (even without --lib android.jar
and --lib recycleview.jar
):
$ java -jar ../build/libs/d8.jar --release --output . fastadapter.jar
$ dexdump -d classes.dex | grep "insns size" | wc -l
390
EDIT
Trying to create the fastadapter proguard keep rules failed with:
$ cmdline-tools/build-tools/30.0.3/aapt2 link --proguard proguard_fastadapter.cfg -o proguard_fastadapter.cfg --manifest AndroidManifest.xml
AndroidManifest.xml:2: error: attribute android:versionCode not found.
AndroidManifest.xml:2: error: attribute android:versionName not found.
AndroidManifest.xml:7: error: attribute android:minSdkVersion not found.
AndroidManifest.xml:7: error: attribute android:targetSdkVersion not found.
error: failed processing manifest.
which is a bit odd because targetSdkVersion in fact does exist:
$ grep "targetSdkVersion" AndroidManifest.xml
android:targetSdkVersion="27" />
The problem is that the only rule you are passing to R8 is
-dontwarn java.lang.Object
. There is no single-keep
rule, which means that the output will be empty as no entry points are kept.What is missing is two set of keep rules:
aapt2
.For the first set of rules Android Studio (or more precisely AGP) bundles this rules file, which is passed to all builds as a result of
getDefaultProguardFile('proguard-android-optimize.txt')
.For the second part use
aapt2 link
with the--proguard
option. That will generate additional required rules like:Both sets of rules will have to be passed to R8 (it can take several
--pg-conf
options) together with additional application specific rules.When using Android Studio (again more precisely AGP) this is handled automatically. For reference these rules can be inspected in
app/build/intermediates/proguard-files
. See Shrink, obfuscate, and optimize your app for more details.