• 6
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Set Android app version using 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 Studio says

   '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.

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

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

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

VERSION_NAME=1.2.1
VERSION_CODE=26

Then in your build.gradle you can use:

versionName project.VERSION_NAME
versionCode Integer.parseInt(project.VERSION_CODE)
  • 49
Reply Report
    • Note: This worked great till updating to use newer build tools! I noticed afterwards, that my version code was unset and Integer.parseInt is marked in Android Studio as unknown. Now this is needed: project.VERSION_CODE.toInteger()

EDITED

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

See here for 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.

  • 29
Reply Report
    • Thank you for response. I also need to use versionCode and versionName values in other places of build.gradle. If I hardcode it like this I won't be able to do that. Therefore I use methods.
      • 1
    • The link does mention Note: Do not use function names that could conflict with existing getters in the given scope. For instance instance defaultConfig { ...} calling getVersionName() will automatically use the getter of defaultConfig.getVersionName() instead of the custom method. So try a different method name.
    • I have a function named getMinSdkVersion() and it does not work, if I rename it to getMinSdk() it works... does that means the previous function name is predefined somewhere?

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

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}"
    }
    ....
}
  • 12
Reply Report