Created
November 5, 2013 06:39
-
-
Save rdallman/7314868 to your computer and use it in GitHub Desktop.
Android Gradle sign and increment version for release
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
buildscript { | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
classpath 'com.android.tools.build:gradle:0.6.+' | |
} | |
} | |
apply plugin: 'android' | |
repositories { | |
mavenCentral() | |
} | |
dependencies { | |
//list your dependencies | |
} | |
android { | |
compileSdkVersion 19 | |
buildToolsVersion "19" | |
defaultConfig { | |
//only needed for release, see release Task | |
versionCode 0 | |
versionName "" | |
minSdkVersion 10 | |
targetSdkVersion 19 | |
} | |
sourceSets { | |
main { | |
//or whatever your structure is | |
manifest.srcFile 'AndroidManifest.xml' | |
java.srcDirs = ['src'] | |
resources.srcDirs = ['src'] | |
aidl.srcDirs = ['src'] | |
renderscript.srcDirs = ['src'] | |
res.srcDirs = ['res'] | |
assets.srcDirs = ['assets'] | |
} | |
// Move the tests to tests/java, tests/res, etc... | |
instrumentTest.setRoot('tests') | |
// Move the build types to build-types/<type> | |
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ... | |
// This moves them out of them default location under src/<type>/... which would | |
// conflict with src/ being used by the main source set. | |
// Adding new build types or product flavors should be accompanied | |
// by a similar customization. | |
debug.setRoot('build-types/debug') | |
release.setRoot('build-types/release') | |
} | |
signingConfigs { | |
release { | |
release { | |
storeFile file('/path/to/your/keystore') | |
//leave blank to fill in later | |
storePassword "" | |
keyAlias 'yourAlias' | |
//leave blank to fill in later | |
keyPassword "" | |
} | |
} | |
buildTypes { | |
release { | |
debuggable false | |
signingConfig signingConfigs.release | |
} | |
} | |
} | |
} | |
//signs keystore only for releases | |
task release << { | |
android.signingConfigs.release.storePassword = new String(System.console().readPassword("\nKeystore Password: ")) | |
android.signingConfigs.release.keyPassword = new String(System.console().readPassword("Key Password: ")) | |
} | |
//uses git tags to tag each release and keep track of version code and name | |
//e.g. 21v1.4.3 | |
task tag << { | |
String lastTag = 'git describe --tags'.execute().text.trim() | |
def vc = lastTag.split("v")[0].toInteger() | |
def vn = lastTag.split("v")[1] | |
def newVersion = new String(System.console().readLine("\nEnter a new version (last: ${vn}): ")) | |
"git tag ${++vc}v${newVersion}".execute() | |
android.defaultConfig.versionCode = vc | |
android.defaultConfig.versionName = newVersion | |
} | |
tasks.whenTaskAdded { assembleRelease -> | |
if (assembleRelease.name.equals("packageRelease")) { | |
assembleRelease.dependsOn "release" | |
} | |
//only tag if keystore passes | |
if (assembleRelease.name.equals("zipalignRelease")) { | |
assembleRelease.dependsOn "tag" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment