Sergey Shustikov
Sergey Shustikov

Reputation: 15831

Android Gradle detect is a release build or not in runtime?

I need to create auto-increment version mechanism via Gradle. For this I use this manual and create two files

version.properties

#Fri May 20 11:04:03 EEST 2016
VERSION_BUILD=3

build.gradle at app level:

buildTypes {

        debug{
            versionNameSuffix 'debug';
        }
        release {
            def versionPropsFile = file('version.properties')

            if (versionPropsFile.canRead()) {
                def Properties versionProps = new Properties()
                versionProps.load(new FileInputStream(versionPropsFile))
                def versionBuild = versionProps['VERSION_BUILD'].toInteger() + 1
                versionProps['VERSION_BUILD'] = versionBuild.toString()
                versionProps.store(versionPropsFile.newWriter(), null)

                versionNameSuffix = versionBuild;

            } else {
                throw new GradleException("Could not read version.properties!")
            }
            debuggable false
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.release
        }
}

The my problem is : VERSION_BUILD increments each time when I build project. Not a Run, just a build. And don't matter I Run project or create a signed APK.

The my goal - increment version only when I generate signed APK. How I can do that?

Upvotes: 0

Views: 1068

Answers (2)

Sergey Shustikov
Sergey Shustikov

Reputation: 15831

Finally, I found solution.

List<String> runTasks = gradle.startParameter.getTaskNames();

for (String item : runTasks) {
    if (item.contains("assemble") && item.contains("Release")) {
           // this is a release task
    }
}

Upvotes: 5

Ashish Rawat
Ashish Rawat

Reputation: 5839

From your gradle file,everytime you run a rel build you sign the app. So Why not use versionCode instead of VERSION_BUILD, that way it will only happen when you build.

Upvotes: 0

Related Questions