Ionut Negru
Ionut Negru

Reputation: 6314

Android Studio Gradle - local.properties variable(path) in settings.gradle

I'm trying to implement an flow so that it won't affect my version control when other people need to work on the project.

Currently I have an library module added into current project from another project. This I have done via the settings.gradle file like this:

include ':MainModule', ':ExternalModule'
project(':ExternalModule').projectDir = new File('C:\\Projects\\AnotherProject\\libraryModule')

My problem is that if I do this, I affect all other developers that work on the project and every time someone pushes to the repository, this file will be changed. I want to avoid this.

I was thinking to add the path of the external library module in the local.properties which is not pushed to the repository and handled by each developer. I did something like this:

include ':MainModule', ':ExternalModule'
project(':ExternalModule').projectDir = new File(getExternalModuleDir())

def getExternalModuleDir() {
    Properties properties = new Properties()
    properties.load(project.rootProject.file('local.properties').newDataInputStream())
    def externalModuleDir = properties.getProperty('module.dir', null)
    if (externalModuleDir == null)
        throw new GradleException("Module location not found. Define location with module.dir in the local.properties file!")

    return externalModuleDir 
}

But I get the following error:

Could not find property 'project' on settings 'AwesomeProject'.

I think this is because settings.gradle does not have access to the local.properties (or the settings.gradle is called before the local.properties, I really do not know for sure the exact flow).

I do something wrong? The flow that I want to achieve is correct? What is the correct way to do something like this?

Upvotes: 2

Views: 7381

Answers (2)

Anas Tariq
Anas Tariq

Reputation: 51

In addition to the answer above, here's Kotlin way to get the desired property from local.properties file.

Add your property in local.properties file

module.dir=C:\\Projects\\AnotherProject\\libraryModule

then in settings.gradle.kts, define a function.

fun getLocalProperty(key: String): String? {
    val props = Properties()
    props.load(File(rootDir.absolutePath + "/local.properties").inputStream())
    val property = props.getProperty(key, "")
    if (property.isNullOrEmpty()) {
       throw GradleException("No value found for the key: $key")
    }
    return property
}

and use it like

project(':ExternalModule').projectDir = new File(getLocalProperty("module.dir"))

Upvotes: 0

Ionut Negru
Ionut Negru

Reputation: 6314

I found an simple solution to my problem eventually. I don't know if it is the best solution or the mos elegant, but this is how I achieved what I wanted:

def getExternalModuleDir() {
    Properties properties = new Properties()
    properties.load(new File(rootDir.absolutePath + "/local.properties").newDataInputStream())
    def externalModuleDir = properties.getProperty('module.dir', null)
    if (externalModuleDir == null) {
        throw new GradleException(
                ""Module location not found. Define location with module.dir in the local.properties file!")
    }
    return externalModuleDir 
}

And in local.properties file I set my property module.dir:

module.dir=C:\\Projects\\AnotherProject\\libraryModule

The only restriction is that the local.properties file should be always located in the same folder as the settings.gradle file (which usually is).

Upvotes: 4

Related Questions