本文介绍了如何从Gradle实验性插件中的任务访问versionCode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从gradle任务中访问defaultConfig中的versionCode和versionName。我正在使用实验gradle插件。我尝试了model.android.defaultConfig.versionCode,但它无法识别android ...

I would like to access versionCode and versionName in defaultConfig from a gradle task. I am using the experimental gradle plugin. I tried model.android.defaultConfig.versionCode but it doesn't recognise android...

apply plugin: 'com.android.model.application'

apply from: "../config.gradle"
apply from: "../gitversion.gradle"
def config = ext.configuration

model {
    android {
        compileSdkVersion = config.compileSdkVersion
        buildToolsVersion = config.buildToolsVersion

        defaultConfig {
            applicationId = 'eu.tss.apitest'
            minSdkVersion.apiLevel = config.minimumSdkVersion
            targetSdkVersion.apiLevel = config.targetSdkVersion
            defaultConfig {

            versionCode = 1
            versionName = '1.0'
            ext.printVersions(versionCode,versionName)
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles.add(file('proguard-android.txt'))
            }
        }

    }
   android.lintOptions {

        abortOnError false
    }
}

dependencies {
    println rootProject.getName()
    compile 'com.android.support:appcompat-v7:25.0.0'
    compile project(':tssapiandroid')
    compile project(path: ':tssuielements')
}

task printVersions{

    //I would like to access versionCode here:)
}


推荐答案

创建方法在你的 build.gradle

def getVersionCode = { ->
    // calculate versionCode code or use hardcoded value
    return 1
}

defaultConfig 中使用此方法:
$ b

Use this method in defaultConfig:

android {
    defaultConfig {
        versionCode getVersionCode()
    }
}

在您的自定义任务中使用相同的方法:

Use the same method in your custom task:

task printVersions{
    print "Version code is " + getVersionCode()
}

替代解决方案是访问<$ c直接使用$ c> versionCode 属性:

Alternative solution is to access versionCode property directly:

task printVersions{
    print "Version code is $project.android.defaultConfig.versionCode"
}

这篇关于如何从Gradle实验性插件中的任务访问versionCode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 18:58