 <p>How I solved it:</p> <ul> <li><p>I created a file version.properties in the root of my android project and added the versioncode in it.</p></li> <li><p>Then I created method called getCurrentVersionCode() in my build.gradle file that reads the current versioncode.</p></li> </ul> <p>Code:</p> <pre><code>def getCurrentVersionCode() { def versionPropsFile = file('version.properties') def Properties versionProps = new Properties() versionProps.load(new FileInputStream(versionPropsFile)) return versionProps['version_code'].toInteger() } </code></pre> <p>I also created a method to generate a new code:</p> <p>Code:</p> <pre><code>// http://stackoverflow.com/questions/30523393/auto-increment-versioncode-only-on-releases // def setUpdatedVersionCode() { def versionPropsFile = file('version.properties') def Properties versionProps = new Properties() def code = getCurrentVersionCode() + 1 versionProps['version_code'] = code.toString() versionProps['version_name'] = generateVersionName() versionProps.store(versionPropsFile.newWriter(), null) } </code></pre> <p>and then I created a task that triggers on a QA build.</p> <p>Code:</p> <pre><code>task('increaseVersionCode') &lt;&lt; { setUpdatedVersionCode() } tasks.whenTaskAdded { task -&gt; if (task.name == 'assembleQaRelease') { task.dependsOn 'increaseVersionCode' } } </code></pre> <p>That way it saves the versioncode in a separate file so I don't need to edit my build.gradle.</p>