I'm using includeBuild
to include modules from my own library in settings.gradle
:
rootProject.name = "MyApp"
include ':app'
includeBuild '/usr/local/library/Android/Event'
includeBuild '/usr/local/library/Android/Location'
includeBuild '/usr/local/library/Android/Widget'
I know I can iterate these later with:
gradle.includedBuilds.each{ includeBuild ->
println includeBuild.name
}
However, that prints:
Event
Location
Widget
Is there a simple way to get the rootProject.name
s that I have defined in each of those individual library projects' settings.gradle
files?
I know I can do the following to give alternative names:
includeBuild('/usr/local/library/Android/Event', {name = 'com.example.android.event'})
includeBuild('/usr/local/library/Android/Location', {name = 'com.example.android.location'})
includeBuild('/usr/local/library/Android/Widget', {name = 'com.example.android.widget'})
... but that is redundant and cumbersome when I've already defined those as rootProject.name
is their respective settings.gradle
.
Rather, I'm looking for something akin to:
gradle.includedBuilds.each{ includeBuild ->
println includeBuild.rootProject.name
}
For instance, I know about includeBuild.projectDir
. Can I somehow get a (parsed) reference to the settings.gradle
file in that directory?
CodePudding user response:
I've managed to solve it using org.gradle.tooling.GradleConnector
:
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.ProjectConnection
import org.gradle.tooling.model.GradleProject
def getIncludedProjectNamesMap(Project project) {
def projectNamesMap = new HashMap<String, String>()
project.gradle.includedBuilds.each { includedBuild ->
ProjectConnection connection = GradleConnector.newConnector()
.forProjectDirectory(includedBuild.projectDir)
.connect()
GradleProject includedProject = connection.getModel(GradleProject.class);
def name = includedProject.getName();
connection.close();
projectNamesMap.put includedBuild.name, name;
}
return projectNamesMap
}
println getIncludedProjectNamesMap(project)
... which prints:
{Event=com.example.android.event, Location=com.example.android.location, Widget=com.example.android.widget}
... but that appears to be rather slow, probably due to all the connections it need to make. It does the job for now, but I'm still looking for alternative approaches, if available.