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

查看:50
本文介绍了如何在 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 来源:

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

目标

我希望能够看到由 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 命令.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

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 语言中实现任何任务或配置功能,大多数插件都使用这些语言而不是执行命令行工具.不过,这可以通过使用或扩展 执行任务.

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.

编译步骤由 AndroidJavaCompile 任务,它扩展了常见的JavaCompile Gradle 任务通过一些版本检查和即时运行特征.但是,您不知道 Gradle 实际上是如何编译 .java 文件的.在 内部源文件,似乎有各种实现(DaemonJavaCompilerJdkJavaCompiler甚至 CommandLineJavaCompiler).由于您可以在任务中指定 CompilerOptions,Gradle 似乎会根据这些选项选择真正的编译器.请注意,即使 CommandLineJavaCompiler 存在,Gradle 也有可能(而且很有可能)更喜欢使用 javax.tools 包及其JavaCompiler 实现编译源文件而不是调用命令行工具.

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天全站免登陆