Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get Gradle module name programmatically

Is it possible to obtain Gradle's module name programmatically in Java code?

I need it to access module's build files under the MODULE_NAME/build/intermediates directory, but hardcoding module name is a bad idea.

like image 538
tomrozb Avatar asked Jun 20 '15 08:06

tomrozb


2 Answers

To get the current's module name you can just access Gradle's project object

project.name

Will return, for the module app

app

See: https://docs.gradle.org/current/userguide/writing_build_scripts.html

like image 141
Patrick Favre Avatar answered Sep 19 '22 08:09

Patrick Favre


You can access project variable inside your build script that is instance of Project. It has various getters like projectDir or buildDir and since this is quite common to use them it is documented in user guide.

Alternatively you can find this path if you look for an output of a task that generates files there.

If you need to know this location in your test you can pass it from build script using system property like this: test { systemProperty "buildFolder", project.buildDir.absolutePath }

BTW: https://github.com/gradle/gradle/blob/master/gradle/idea.gradle#L192 has a trick how to update run configurations in IntelliJ's workspace with properties that you need for test execution. This can be useful if you run JUnit test inside IJ (without delegating to Gradle).

like image 24
Radim Avatar answered Sep 22 '22 08:09

Radim