lapots
lapots

Reputation: 13395

define global variables and functions in build.gradle

Is there a way to define global variables in build.gradle and make them accessible from everywhere.

I mean something like this

def variable = new Variable()

def method(Project proj) {
    def value = variable.value
}

Because that way it tells me that it cannot find property. Also I'd like to do the same for the methods. I mean something like this

def methodA() {}
def methodB() { methodA() }

Upvotes: 25

Views: 25930

Answers (1)

RaGe
RaGe

Reputation: 23637

Use extra properties.

ext.propA = 'propAValue'
ext.propB = propA
println "$propA, $propB"

def PrintAllProps(){
  def propC = propA
  println "$propA, $propB, $propC"
}

task(runmethod) << { PrintAllProps() }

Running runmethod prints:

gradle runmethod
propAValue, propAValue
:runmethod
propAValue, propAValue, propAValue

Read more about Gradle Extra Properties here.

You should be able to call functions from functions without doing anything special:

def PrintMoreProps(){
  print 'More Props: '
  PrintAllProps()
}

results in:

More Props: propAValue, propAValue, propAValue

Upvotes: 30

Related Questions