Red M
Red M

Reputation: 2789

Using a default value between two different build gradle files

I have the following scenario in my Android project:

Project1 --> Build.gradle (1) Project2--> Build.gradle (2)

Example: Define the following variable:

//first Gradle file
def getProductFlavor() {
//Logic here
Gradle gradle = getGradle()
String requestingTask = gradle.getStartParameter().getTaskRequests().toString()
return requestingTask
}

Instead of defining getCurrentTime() in the second Gradle file, 
I call the getCurrentTime() from the first Gradle file.

Maybe my example is wrong and the default value needs to be implementing in another gradle file like the script gradle or somewhere else, but the intent of the example was to clarify what I'm trying to achieve.

The two projects are independents but both belong to the same android project. I want to use ONE def value in both of these gradle files.

I'm a gradle newbie by the way. Never mind if I'm asking this question the wrong way.

Upvotes: 1

Views: 238

Answers (1)

AdamSkywalker
AdamSkywalker

Reputation: 11609

Feels like a scenario for external script:

main-project
....| sub-project1
........| src
........| build.gradle
....| sub-project2
........| src
........| build.gradle
....| common.gradle

build.gradle

apply from: '../common.gradle'

def flavor = getProductFlavor()

common.gradle

def getProductFlavor() {
    Gradle gradle = getGradle()
    String requestingTask = gradle.getStartParameter().getTaskRequests().toString()
    return requestingTask
}

ext {
    getProductFlavor = this.&getProductFlavor
}

Upvotes: 3

Related Questions