tomrozb
tomrozb

Reputation: 26251

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.

Upvotes: 16

Views: 19220

Answers (2)

Patrick
Patrick

Reputation: 35224

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

Upvotes: 35

Radim
Radim

Reputation: 4808

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

Upvotes: 2

Related Questions