Search code examples
androidandroid-r8android-d8

How Can I debug R8 during compilation?


Is there any way to debug R8 during compilation of an Android project ?

I have a really weird error and the casual fix won't help


Solution

  • Assuming that you have the R8 project checked out (from https://r8.googlesource.com/r8) and is using IntelliJ as the debugger, this is a way to debug Gradle building an Android Studio project.

    First build r8:

    tools/gradle.py r8
    

    The r8.jar is built in build/libs and should then be referenced in the main build.gradle file (in the buildscript.repositories section) like this:

    buildscript {
    
        dependencies {
            classpath files($PATH_TO_R8_JAR)  // Must be before the Gradle Plugin for Android.
            classpath 'com.android.tools.build:gradle:3.4.0-rc03'
         }
    }
    

    To debug a command line Gradle run pass the options -Dorg.gradle.debug=true and --no-daemon when invoking Gradle, E.g. for a release build of an Android Studio project:

    ./gradlew assembleRelease -Dorg.gradle.debug=true --no-daemon
    

    The gradle command will wait for the debugger to attach. In IntelliJ create a standard remote debugging configuration and attach. The gradle command will continue and breakpoints in the R8 code will be hit.

    NOTE: That for a range of the 3.5.0 aplha releases adding r8.jar to build.gradle can cause issues for Kotlin projects (see Issue 129240946).