Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 967
Slava Avatar asked Jan 24 '14 10:01

Slava


People also ask

What is versionCode and versionName in Android?

The version code is an incremental integer value that represents the version of the application code. The version name is a string value that represents the “friendly” version name displayed to the users.

What is version code in build gradle?

versionCode — A positive integer used as an internal version number. This number is used only to determine whether one version is more recent than another, with higher numbers indicating more recent versions. This is not the version number shown to users; that number is set by the versionName setting, below.


1 Answers

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) 
like image 188
Gabriele Mariotti Avatar answered Oct 03 '22 05:10

Gabriele Mariotti