18

I'm trying to get a better picture of what happens behind the scenes in Android Studio when building an Android application. I've been reading up on Gradle, but one thing I cannot figure out is how to see the respective CLI command and arguments that is being invoked by Gradle. It seems to be abstracted and not logged to the Gradle Console or Event Log.

The closest I've gotten to seeing what's going on inside Gradle is the AOSP code.

2.2.2 Source:

https://android.googlesource.com/platform/tools/base/+/gradle_2.2.2/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks

Goals

I want to be able to see the respective CLI command that is generated by the Gradle tasks inside Android Studio.

Use Case Example

I want to view the Legacy Android Build Process in depth. This includes going through the following:

Source Code / Library Code -> javac -> Java bytecode (.class) -> proguard -> minimized bytecode (.class) -> dex -> DEX bytecode (.dex)

For example I would want to see the respective javac command invoked by AndroidJavaCompile. https://android.googlesource.com/platform/tools/base/+/gradle_2.2.2/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/factory/AndroidJavaCompile.java

I fear that the only way to do this is to look directly through source code or even build directly from source.

Due Diligence

I've done quite a bit of searching on Google, Android blogs, Google I/O talks, Android books, and much more. I haven't been able to find a straight-forward answer.

Jon Douglas
  • 13,006
  • 4
  • 38
  • 51

1 Answers1

16

That's not possible. Simply, because most of the Gradle tasks do not invoke CLI commands.

Every Gradle build file is a piece of Groovy code that gets executed in a JVM along with the Gradle API (written in Java). Therefor, you can implement any task or configuration functionality directly in any JVM language, from which most plugins make use of instead of executing command line tools. Nevertheless, this is possible by using or extending the Exec task.

The compilation step is handled by a AndroidJavaCompile task, which extends the common JavaCompile Gradle task by some version checks and the Instant Run feature. However, you don't know how Gradle actually compiles the .java files. In the internal source files for the JavaCompile task of the Gradle API, there seem to be various implementations (DaemonJavaCompiler, JdkJavaCompiler and even CommandLineJavaCompiler). Since you can specify CompilerOptions with your task, Gradle seems to choose the real compiler based on these options. Please note, that even if a CommandLineJavaCompiler exists, it is also possible (and highly likely), that Gradle prefers to use the javax.tools package and its JavaCompiler implementation to compile the source files instead of invoking a command line tool.

I also took a look on the ProGuard step in your example build process: ProGuard can be used as command line tool, where you can specify arguments to define how it'll work. But ProGuard also provides a Gradle task (ProGuardTask), that executes without invoking ProGuard from command line. The ProGuard Java code will be executed in the Gradle JVM.

As you can see, even if each Gradle task may be replaced by one (or multiple) CLI command(s), Gradle does not execute these commands. Instead, the functionality is called directly in the Gradle JVM. If you want to get a better insight, you can increase the Gradle log level. Good implementations of Gradle tasks should provide all necessary information in logs.

Lukas Körfer
  • 13,515
  • 7
  • 46
  • 62
  • Thanks for taking the time to answer this! Other than Gradle log levels being increased, is there a fairly simple way to debug these internal gradle tasks to see what's going on? I assume it's not too trivial. – Jon Douglas Jul 24 '17 at 17:40
  • To be honest, I never tried to debug the internal code of Gradle and / or its plugins, because I never had such complex problems. Is this related to any particular problem? – Lukas Körfer Jul 25 '17 at 15:22
  • But OP wants to see the compiler options (and I'd like to see how the debugger is invoked). These are reasonable things to want to know! Is it really true that you can't get Gradle to log this level of detail (without drowning in extraneous debug output)? – BobHy Oct 28 '22 at 03:20