使用 Gradle 设置 Android 应用版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21329849/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:22:22  来源:igfitidea点击:

Set Android app version using Gradle

androidandroid-studiogradleandroid-gradle-pluginbuild.gradle

提问by Slava

I'm trying to use Gradle to set app's name. Please, look at this snippet of build.gradle:

我正在尝试使用 Gradle 来设置应用程序的名称。请看一下 build.gradle 的这个片段:

android {
    ...
    defaultConfig {
        ...
        versionCode getVersionCode()
        versionName getVersionName()
        ...
    }
    ...
}

...

int getVersionCode() {
    return 1
}

def getVersionName() {
    return "1.0"
}

Android Studio says

Android Studio 说

   'versionCode' cannot be applied to 'java.lang.Integer'
   'versionName' cannot be applied to 'java.lang.String'

and when I install the app on a device it has no versionCode and versionName at all.

当我在设备上安装该应用程序时,它根本没有 versionCode 和 versionName。

The problem is clear to me but I don't know how to solve it.
Please advice.

问题对我来说很清楚,但我不知道如何解决。
请指教。

采纳答案by keithmaxx

EDITED

已编辑

In order to define your app version dynamically, specify a custom method with def and call it, as such:

为了动态定义您的应用程序版本,请使用 def 指定一个自定义方法并调用它,如下所示:

def computeVersionName() {
    return "2.0"
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        versionCode 12
        versionName computeVersionName()
        minSdkVersion 16
        targetSdkVersion 16
    }
}

See herefor more.

请参阅此处了解更多信息。

Make sure not to use function names that could conflict with existing getters in the given scope. For instance, defaultConfig { ... }calling getVersionName()will automatically use the getter defaultConfig.getVersionName()instead of the custom method.

确保不要使用可能与给定范围内的现有 getter 冲突的函数名称。例如,defaultConfig { ... }调用getVersionName()将自动使用 getterdefaultConfig.getVersionName()而不是自定义方法。

回答by Gabriele Mariotti

It doesn't resolve your issue, but it can be a different solution.

它不能解决您的问题,但可以是不同的解决方案。

You can use gradle.properties inside your root project and define:

您可以在根项目中使用 gradle.properties 并定义:

VERSION_NAME=1.2.1
VERSION_CODE=26

Then in your build.gradle you can use:

然后在你的 build.gradle 中你可以使用:

versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)

回答by Phileo99

Here is a build.gradle that I am using based on ideas from JakeWharton:

这是我根据JakeWharton 的想法使用的build.gradle

apply plugin: 'com.android.application'
def versionMajor = 1
def versionMinor = 2
def versionPatch = 0

def gitVersion() {
    def counter = 0
    def process = "git rev-list master --first-parent --count".execute()
    return process.text.toInteger()
}

repositories {
    mavenCentral()
}

android {
    compileSdkVersion 19
    buildToolsVersion '19.1.0'


    defaultConfig {
        applicationId 'my.project.com'
        minSdkVersion 14
        targetSdkVersion 19
        versionCode gitVersion()
        versionName "${versionMajor}.${versionMinor}.${versionPatch}"
    }
    ....
}

回答by metch

If you use a gradle.propertiesfile to store shared build versions, make sur to use the toInteger()function for the version code, compiled/min and target sd versions.

如果您使用gradle.properties文件来存储共享构建版本,请使用该toInteger()函数来存储版本代码、已编译/最小和目标 sd 版本。

See this post for more information: https://medium.com/@ali.muzaffar/gradle-configure-variables-for-all-android-project-modules-in-one-place-5a6e56cd384e#.yl1x0ziqe

有关更多信息,请参阅此帖子:https: //medium.com/@ali.muzaffar/gradle-configure-variables-for-all-android-project-modules-in-one-place-5a6e56cd384e#.yl1x0ziqe