This content could be a part of the yesterday article but I think it’s too important, so let’s publish a dedicated one about these tips used for debugging Gradle and Gradle Android Plugin like a real Android tools developer.

Some use cases:

  • Debug TaskManager.java where all tasks are loaded and understand AGP
  • Found reason of this unknown/cryptic lint issue during build

Debug Gradle itself

Open Gradle sources on Intellij:

git clone https://github.com/gradle/gradle
./gradlew idea

Start a build waiting debugger:

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

And use the “Remote debug port 5005” in Run/Debug configurations list generated by idea task. (see gradle/idea.gradle script if you’re curious about how this was declared)

Debug Android Gradle Plugin

For that, we need to reference AGP in Intellij. Luckily, we already downloaded them while working on our Android projects.

Just go to Intellij Project structure > Project Settings > Modules and add following jars under ~/.gradle/caches/modules-2/files-2.1/com.android.tools.build/:

  • gradle-core/2.3.0/895cde36ab5bc7cfcb8e68b520c73e9c05e96841/gradle-core-2.3.0.jar
  • builder/2.3.0/e2f05a09930ca699f9ca64b21564f252eca24fc9/builder-2.3.0.jar

Select add JARs or libraries: Add AGP libs to your projects And see the result: AGP libs added in Intellij

Nice, but it’s just unreadable binary files… We need also sources code!!

You have 2 choices: download them via Google maven repo:

But I personally prefer to just do like before via gradle cache, by looking for files like gradle-core-2.3.0-sources.jar.

Now, you’re ready to debug AGP inside Intellij:

Debug AGP inside Intellij

Patching AGP

Not only AGP but after inspecting a Gradle plugin, if you would like to change a behavior or try to fix an issue, how to patch it?

Twitter (quick/hacky) solution is to use buildSrc, where you can put classes added to classpath before others dependencies, and will replace default plugin implementation.

An example where 2 AGP classes are replaced by our Java files:

Fix AGP with your own classes