2016-08-15 174 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." 
}