r8 does not create output dex file

989 views Asked by At

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" />
1

There are 1 answers

2
sgjesse On BEST ANSWER

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:

  1. General keep rules for all Android apps
  2. Specific rules for the concrete app with all entry points (reflective access from the platform for e.g. view inflation), which can be generated with 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').

# This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
#
# Starting with version 2.2 of the Android plugin for Gradle, this file is distributed together with
# the plugin and unpacked at build-time. The files in $ANDROID_HOME are no longer maintained and
# will be ignored by new version of the Android plugin for Gradle.

# 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

-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Preserve some attributes that may be required for reflection.
-keepattributes *Annotation*,Signature,InnerClasses,EnclosingMethod

-keep public class com.google.vending.licensing.ILicensingService
-keep public class com.android.vending.licensing.ILicensingService
-keep public class com.google.android.vending.licensing.ILicensingService
-dontnote com.android.vending.licensing.ILicensingService
-dontnote com.google.vending.licensing.ILicensingService
-dontnote com.google.android.vending.licensing.ILicensingService

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}

# Keep setters in Views so that animations can still work.
-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 ** CREATOR;
}

# Preserve annotated Javascript interface methods.
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

# The support libraries 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.
-dontnote android.support.**
-dontnote androidx.**
-dontwarn android.support.**
-dontwarn androidx.**

# This class is deprecated, but remains for backward compatibility.
-dontwarn android.util.FloatMath

# Understand the @Keep support annotation.
-keep class android.support.annotation.Keep
-keep class androidx.annotation.Keep

-keep @android.support.annotation.Keep class * {*;}
-keep @androidx.annotation.Keep class * {*;}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <methods>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <fields>;
}

-keepclasseswithmembers class * {
    @android.support.annotation.Keep <init>(...);
}

-keepclasseswithmembers class * {
    @androidx.annotation.Keep <init>(...);
}

# These classes are duplicated between android.jar and org.apache.http.legacy.jar.
-dontnote org.apache.http.**
-dontnote android.net.http.**

# These classes are duplicated between android.jar and core-lambda-stubs.jar.
-dontnote java.lang.invoke.**

For the second part use aapt2 link with the --proguard option. That will generate additional required rules like:

-keep class com.example.app.MainActivity { <init>(); }

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.