Home » Android » android – How can I get Gradle module name programmatically

android – How can I get Gradle module name programmatically

Posted by: admin June 15, 2020 Leave a comment

Questions:

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.

How to&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

Answer:

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