android - 使用 Gradle 设置 Android 应用版本

标签 android android-studio gradle android-gradle-plugin build.gradle

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

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

...

int getVersionCode() {
    return 1
}

def getVersionName() {
    return "1.0"
}

Android Studio 说

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

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

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

最佳答案

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

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

VERSION_NAME=1.2.1
VERSION_CODE=26

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

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

关于android - 使用 Gradle 设置 Android 应用版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21329849/

相关文章:

java - 在文件的最后以数字增量保存文件

android - 为什么在 Android Studio 中编程时我的光标会随机消失?

android - 将库上传到 Bintray,但使用时无法解析

android - 从后台删除 fragment 并显示 Activity

Android Google DriveApi 在不同设备上为同一标题返回不同的 DriveID

java - 我无法让 ListView 显示文件夹中的文件名

android - Android Gradle 中每种风格的代码路径

testing - 使用 Gradle 进行集成测试时运行服务器

android - 如何使用 xml 设置 EditText 的边距?

安卓图库查看更新图片