问题 在build.gradle中定义全局变量和函数


有没有办法在中定义全局变量 build.gradle 并让他们从任何地方访问。

我的意思是这样的

def variable = new Variable()

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

因为那样它告诉我它 cannot find property。 我也想对这些方法做同样的事情。 我的意思是这样的

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

11436
2017-12-29 15:12


起源

到处都是源代码中的无处不在? - Roman Makhlin
ext 命名空间是你需要的 - RaGe


答案:


使用额外的属性。

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

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

task(runmethod) << { PrintAllProps() }

运行 runmethod 打印:

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

了解更多 Gradle额外属性在这里。

你应该能够从函数调用函数而不做任何特殊的事情:

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

结果是:

More Props: propAValue, propAValue, propAValue

10
2017-12-29 16:12