Giacomo M
Giacomo M

Reputation: 4711

Change versionCode of an App created in a gradle script

in an Android app I did not develop, but I have to edit, I found this gradle script:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.ajoberstar:grgit:1.5.0'
    }
}

import org.ajoberstar.grgit.Grgit

ext {
    git = Grgit.open(currentDir: projectDir)
    gitVersionName = git.describe()
    gitVersionCode = git.tag.list().size()
    gitVersionCodeTime = git.head().time
}

task printVersion() {
    println("Version Name: $gitVersionName")
    println("Version Code: $gitVersionCode")
    println("Version Code Time: $gitVersionCodeTime")
}

The "gitVersionCode" variable is used as "versionCode" of the app in the build.gradle file.
This is the build.gradle file:

// gradle script
apply from: "$project.rootDir/tools/script-git-version.gradle"
//...
defaultConfig {
    applicationId "..."
    minSdkVersion 17
    targetSdkVersion 25
    versionCode gitVersionCode
    versionName gitVersionName
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

When I generate signed APK and try to add to beta version, I get the google error that "Version code already exists", but I do not know how to edit the version code of the app.

My questions are:

Upvotes: 1

Views: 810

Answers (2)

Nelson Wright
Nelson Wright

Reputation: 506

It appears to be using this library to query git, in order to generate the version name and version code. In terms of the version code, it looks like it's setting it depending on the number of git tags it finds. So, to bump the version code, do a git tag on your release commit.

If that doesn't suit, you could just do it manually and replace

versionCode gitVersionCode

with

versionCode 99

replacing "99" with whatever your version code should be.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006614

Git is a version control system. Your project appears to be stored in a Git repository.

Grgit is a Groovy library that works with Git. Your Gradle script is loading this library.

git is an instance of Grgit. git.tag.list() returns a list of the Git tags used in this repository. git.tag.list().size() returns the number of tags in that list.

So, this Gradle script is setting the versionCode to be the number of Git tags used in the project's Git repository.

how to edit the version code of the app?

Either:

  • Create another Git tag, such as by tagging the version that you are trying to release, or

  • Change versionCode gitVersionCode to stop using gitVersionCode and use some other numbering system

Upvotes: 1

Related Questions