Android 从其他gradle脚本调用方法
我希望有方法在build.gradle之外生成版本号和版本。我已经创建了build-versioning.gradle:Android 从其他gradle脚本调用方法,android,gradle,android-gradle-plugin,Android,Gradle,Android Gradle Plugin,我希望有方法在build.gradle之外生成版本号和版本。我已经创建了build-versioning.gradle: def getNewBuildCode = { -> def stdout = new ByteArrayOutputStream() exec { commandLine 'git', 'rev-list', 'HEAD', '--count' standardOutput = stdout } def
def getNewBuildCode = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', 'HEAD', '--count'
standardOutput = stdout
}
def version = stdout.toString().trim().toInteger()
println("versionCode: $version")
return version
}
def getVersionNameFromTag = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
return stdout.toString().trim()
}
然后我将其应用于build.gradle中:
apply from: '../../signing.gradle'
并在defaultConfig中使用:
versionName getVersionNameFromTag()
我收到了:
Error:Could not find method getVersionNameFromTag() for arguments [] on ProductFlavor_Decorated{name=main, dimension=null, minSdkVersion=DefaultApiVersion{mApiLevel=17, mCodename='null'}, targetSdkVersion=DefaultApiVersion{mApiLevel=24, mCodename='null'}, renderscriptTargetApi=null, renderscriptSupportModeEnabled=null, renderscriptSupportModeBlasEnabled=null, renderscriptNdkModeEnabled=null, versionCode=157, versionName=null, applicationId=ae.propertyfinder, testApplicationId=null, testInstrumentationRunner=android.support.test.runner.AndroidJUnitRunner, testInstrumentationRunnerArguments={}, testHandleProfiling=null, testFunctionalTest=null, signingConfig=null, resConfig=null, mBuildConfigFields={}, mResValues={}, mProguardFiles=[], mConsumerProguardFiles=[], mManifestPlaceholders={}} of type com.android.build.gradle.internal.dsl.ProductFlavor.
GetNewBuilderCode也是如此。
如何解决这个问题?应该是这样的:
// build.gradle file
task build(type: GradleBuild) {
buildFile = 'other.gradle'
tasks = ['hello from main file']
}
// other.gradle file
task hello << {
println "method from another file here."
}
//build.gradle文件
任务生成(类型:GradleBuild){
构建文件='other.gradle'
tasks=['hello from main file']
}
//其他.gradle文件
任务hellodef表示本地函数/变量,因此必须导出它们。这可以通过ext属性完成。请在此处查找更多信息:
就是这样!谢谢
ext.getNewBuildCode = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'rev-list', 'HEAD', '--count'
standardOutput = stdout
}
def version = stdout.toString().trim().toInteger()
println("versionCode: $version")
return version
}
ext.getVersionNameFromTag = { ->
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
return stdout.toString().trim()
}