在 Gradle,如何在控制台/事件日志中打印消息?

我试图验证在执行部署命令时源路径和目标路径是否正确设置。

请看下面的例子:
(复制自: http://eppz.eu/blog/unity-android-plugin-tutorial-2/)

android.libraryVariants.all { variant ->
// Task names.
String variantName = "${variant.name.capitalize()}"; // Like 'Debug'
String deployTaskGroup = "plugin";
String deployTaskName = "deploy${variantName}PluginArchive"; // Like 'deployDebugPluginArchive'
String dependencyTaskName = "assemble${variantName}"; // Like 'assembleDebug'
// Source.
String sourceAARFolder = "${buildDir.getPath()}/outputs/aar/";
String sourceAARName = "${project.name}-${variant.name}.aar";
// Target.
String targetAssetFolder = "Assets/Plugins/My Plugin";
String targetAARFolder = "${rootDir.getPath()}/../../${targetAssetFolder}"; // Navigate into 'Assets'
String targetAARName = "My Plugin Android.aar"; // The form you ship your plugin


String targetProjDir = System.env.UNITY_PROJECT; // <-- Need to confirm this line!
//Log.i(targetProjDir); //??????????? something like this?


// Create task.
task(deployTaskName, dependsOn: dependencyTaskName, type: Copy) {
from(sourceAARFolder)
into(targetAARFolder)
include(sourceAARName)
rename(sourceAARName, targetAARName)
}.group = deployTaskGroup;
}

有没有什么方法可以将上面的 targetProjDir字符串变量显示到某种控制台或 Android Studio 中的事件日志(假设它是控制台的名称) ?

140704 次浏览

Gradle scripts are written in Groovy language. It is possible to log into console your own messages.

If your Gradle version of your project is 3.2.1 or above then there is a simple option for logging in your build file which is to write messages to standard output. Gradle redirects anything written to standard output to it's logging system.

Example

println 'A message which is logged at QUIET level'

Gradle logging system allows us to log message into multiple log levels (LIFECYCLE, QUIET, INFO, DEBUG )

Please go through below link for detailed study

https://docs.gradle.org/current/userguide/logging.html

Gradle utilizes a logging framework. You can log messages to that. By default, only log level lifecycle and above are shown, but you can log at other levels such as debug and info.

To log at debug level (visible with builds using gradle --debug or lower)

project.logger.debug('my debug message')

To log at info level (visible with gradle --info builds and lower)

project.logger.info('my info message')

To log at lifecycle level (visible by default)

project.logger.lifecycle('my message visible by default')

Basically you can print out a message by this gradle script -

 println "This is a simple gradle message"

but if you want to print a variable in the message then you can do it by the following code

def variable = "This is a simple variable"


println "Message: ${variable}"

This works in Android Studio for me:

println("Log: current build type is $buildTypeName")

This is for Kotlin DSL (build.gradle.kts) and Gradle 7.4.

When you use println (standard output), it is redirected to log level QUIET. So, it is always printed in all log levels (when no log level is specified, Gradle defaults to LIFECYCLE):

println("I am a log message.")

You can also use logger property of the project implicitly provided in the script:

logger.info("I am an {} log message", "info")
// OR
project.logger.info("I am an {} log message", "info")

Here are log levels available in Gradle and how to enable them from command line:

Order Name Command line option Levels outputted
6 ERROR does not have option (always printed) ERROR
5 QUIET -q or --quiet QUIET and higher
4 WARNING -w or --warn WARNING and higher
3 LIFECYCLE when no option is provided LIFECYCLE and higher
2 INFO -i or --info INFO and higher
1 DEBUG -d or --debug DEBUG and higher

You can also set the log level in gradle.properties file:

org.gradle.logging.level=quiet|warn|lifecycle|info|debug

See Gradle official documentations to learn more about logging.

You can create your own tasks and print values to console.

tasks.create("nameOfTest"){
println("Print anything")
}

and from terminal you can run above task

./gradlew nameOfTest