Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从其他gradle脚本调用方法_Android_Gradle_Android Gradle Plugin - Fatal编程技术网

Android 从其他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

我希望有方法在build.gradle之外生成版本号和版本。我已经创建了build-versioning.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()
我收到了:

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