122

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)?

Mahozad
  • 18,032
  • 13
  • 118
  • 133
chamberlainpi
  • 4,854
  • 8
  • 32
  • 63
  • I think I just figured it out, didn't notice the `Gradle Console` until now. A simple `printf "Your String here..."` seems to do the trick. – chamberlainpi Nov 30 '16 at 01:56
  • ^ that being said, it seems to print it out twice in a row everytime I run any 'assemble...', 'build...' or 'deploy...' Gradle commands. What could be causing the duplicate prints? – chamberlainpi Nov 30 '16 at 02:04
  • 1
    `System.out.println(message);` – Rajesh Nov 30 '16 at 05:18
  • I gotta say, I'm surprised this question still gets quite a few visits / votes to this day! Would of imagined there would be better alternatives / simplified build-workflows for Android by now. Glad this question sheds some lights to others! Good luck! – chamberlainpi Oct 28 '19 at 14:47
  • @chamberlainpi Why didn't you accept an answer? – The incredible Jan May 23 '23 at 05:36

6 Answers6

160

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')
LarsH
  • 27,481
  • 8
  • 94
  • 152
JBirdVegas
  • 10,855
  • 2
  • 44
  • 50
  • 3
    If you just want to log something without configuring anything use `project.logger.lifecycle('my message visible by default')` as @JbirdVegas suggested. You can view the log in the *Gradle console* tab. – vovahost Jan 08 '18 at 16:43
80

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

Jayakrishnan
  • 4,457
  • 29
  • 29
  • 2
    `println` will not log though the logging frameworks it is a method forward to `System.out.println(String)` method. This will always print not just at a specific log level. – JBirdVegas Nov 30 '16 at 18:40
  • 1
    Reference on `println` method in groovy: http://docs.groovy-lang.org/latest/html/api/groovy/lang/Script.html#println() – JBirdVegas Nov 30 '16 at 18:47
  • It is taken from release note of Gradle version 3.2.1. I will update my answer – Jayakrishnan Dec 01 '16 at 01:33
  • 2
    @JBird: "Gradle redirects anything written to standard output to its logging system at the QUIET log level." (https://docs.gradle.org/current/userguide/logging.html, which is for Gradle 4.2.1). So according to this documentation, `println` *will* log through the logging framework. However, I have not been able to successfully test this in practice. – LarsH Oct 03 '17 at 15:16
  • This works on Android Studio while the accepted solution did not – Chisko Mar 22 '18 at 17:16
  • 1
    @Chisko There currently is no accepted answer. Which one did you mean and why didn't it work? Why didn't you comment there? – The incredible Jan May 23 '23 at 05:38
  • @TheincredibleJan I guess I meant the most voted answer, this was more than 5 years ago – Chisko May 25 '23 at 17:12
25

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}"
Gk Mohammad Emon
  • 6,084
  • 3
  • 42
  • 42
11

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.

Mahozad
  • 18,032
  • 13
  • 118
  • 133
9

This works in Android Studio for me:

println("Log: current build type is $buildTypeName")
Droid Chris
  • 3,455
  • 28
  • 31
3

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
Prakash
  • 7,794
  • 4
  • 48
  • 44