2016-08-15 14 views
1

build.gradleの外にビルド番号とバージョンを生成するメソッドが必要です。 defaultConfigに他のgradleスクリプトからメソッドを呼び出す

apply from: '../../signing.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にそれを適用しています:

versionName getVersionNameFromTag() 

私は」私はビルドversioning.gradleを作成しました受け取る:

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

をDEFは、ローカル関数/変数を示します。これは、extプロパティを介して行うことができます。その詳細はこちら: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

それは次のようになりshoul:あなたがそれらをエクスポートする必要がありますので

// 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." 
} 
関連する問題