2016-08-15 681 views
1

我想拥有生成build.gradle之外的版本号和版本的方法。我创建集结versioning.gradle:从其他Gradle脚本调用方法

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() 

我m reciving:

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. 

与g相同etNewBuildCode。 如何解决这个问题?

回答

1

高清表明局部功能/变量,所以你必须以出口为主。这可以通过分机属性来完成。在这里可以找到更多关于它的信息:https://docs.gradle.org/current/userguide/writing_build_scripts.html#sec:local_variables

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() 
} 
+0

这就是它!谢谢 –

0

它建议立即进行删除这个样子:

// 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." 
}