Denis Loh
Denis Loh

Reputation: 2224

Gradle: set property in project dependency

I am looking for the solution of setting properties of project dependencies with gradle with configuration closures. I already had the solution but my computer crashed and I can't remember how I did this.

I have the following Android project structure:

/
-lib/
  - build.gradle
-app/
  - build.gradle
- build.gradle

In the lib:build.gradle I have a ext closure:

ext {
    username = null
    password = null
    serverUrl = 'example.com'
}

In the app:build.gradle I add the lib as a project dependency:

compile project(':lib')

In some way, I achieved to set the ext properties with a statement like

compile project(path: ':lib', configClosure: { ext.username='test' })

Unfortunately, this isn't the correct statement. But what was it?

Upvotes: 2

Views: 1933

Answers (1)

Stanislav
Stanislav

Reputation: 28106

First of all, you don't need to pass parameters with it's names. Just do it this way:

compile project(':lib', { ext.username='test' })

And the second is, you don't need to add a property in the lib:build.gradle if you want to set it from outside. So you have to delete this part of your build script:

ext {
    username = null
}

if you want to set username from outside. Otherwise the value from outside will be set with the null value.

And if you wich to have default value, then you have to make a gradle.properties file in your :lib project and set this values in this properties file as:

username=test

Upvotes: 2

Related Questions