'In Gradle, how to print out a message in the console / Event Log?
I'm trying to verify that my source and target paths are properly setup when I execute a deploy command.
See the example below:
(copied from: 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;
}
Is there any way to display the above targetProjDir
string variable to some sort of console, or the Event Log in Android Studio (assuming that is it's console's name)?
Solution 1:[1]
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')
Solution 2:[2]
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
Solution 3:[3]
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}"
Solution 4:[4]
This works in Android Studio for me:
println("Log: current build type is $buildTypeName")
Solution 5:[5]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | LarsH |
Solution 2 | |
Solution 3 | Gk Mohammad Emon |
Solution 4 | Droid Chris |
Solution 5 |