如何查看Android Studio中Gradle任务执行的CLI命令? [英] How can I view the CLI command executed by a Gradle task in Android Studio?

查看:292
本文介绍了如何查看Android Studio中Gradle任务执行的CLI命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图更好地了解构建Android应用程序时Android Studio幕后发生的情况.我一直在阅读Gradle,但我无法弄清楚的一件事是如何查看Gradle所调用的相应CLI命令和参数.它似乎是抽象的,没有记录到Gradle ConsoleEvent Log.

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.

我最了解Gradle内部发生的事情是AOSP代码.

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

2.2.2来源:

目标

我希望能够看到由Android Studio内部的Gradle任务生成的相应CLI命令.

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

用例示例

我想深入查看旧版Android构建过程.这包括以下步骤:

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

源代码/库代码-> javac-> Java字节码(.class)-> proguard->最小字节码(.class)-> dex->​​ DEX字节码(.dex)

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

例如,我想查看AndroidJavaCompile分别调用的javac命令.

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.

尽职调查

我已经在Google,Android博客,Google I/O对话,Android书籍等等上进行了大量搜索.我一直找不到简单的答案.

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.

推荐答案

不可能.简而言之,因为大多数Gradle任务都不调用CLI命令.

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

每个Gradle构建文件都是一段Groovy代码,该代码与Gradle API(用Java编写)一起在JVM中执行.因此,您可以直接使用任何JVM语言来实现任何任务或配置功能,大多数插件都使用JVM语言而不是执行命令行工具.不过,可以通过使用或扩展 Exec 任务.

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.

我还查看了示例构建过程中的ProGuard步骤:ProGuard可用作命令行工具,您可以在其中指定参数以定义其工作方式.但是ProGuard还提供了Gradle任务( ProGuardTask ),无需从命令行调用ProGuard即可执行. ProGuard Java代码将在Gradle JVM中执行.

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.

如您所见,即使每个Gradle任务都可以被一个(或多个)CLI命令代替,Gradle也不执行这些命令.而是直接在Gradle JVM中调用该功能.如果您想获得更好的见解,可以提高Gradle日志级别.良好的Gradle任务实现应在日志中提供所有必要的信息.

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.

这篇关于如何查看Android Studio中Gradle任务执行的CLI命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆