设置使用摇篮Android应用程序版本 [英] Set Android app version using Gradle

查看:217
本文介绍了设置使用摇篮Android应用程序版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用摇篮设置应用程序的名称。请看看这个片断build.gradle的:

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

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

...

int getVersionCode() {
    return 1
}

def getVersionName() {
    return "1.0"
}

Android的工作室说

Android Studio says

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

当我在设备上安装应用程序有没有版本code和VERSIONNAME都没有。

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

现在的问题是清楚的给我,但我不知道如何解决它。
请指点。

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

推荐答案

EDITED

为了动态地定义你的应用程序版本,指定一个自定义的方法DEF并调用它,因为这样的:

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

def computeVersionName() {
    return "2.0"
}

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

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

请参阅<一href="http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Basic-Build-Customization">here更多。

请务必不要使用函数名,可以在给定的范围内现有的干将冲突。例如, defaultConfig {...} 调用 getVersionName()将自动使用吸气 defaultConfig.getVersionName(),而不是自定义的方法。

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.

这篇关于设置使用摇篮Android应用程序版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆