Reputation: 368
I got stuck on a Problem with this script. I want to achieve 3 things:
My goal is to never care about versioning manually my builds. By just keep setting tags via git, this gradle script should automatically update the versionCode and versionNumber.
The Problem When I let gradle compile that script it fails with an Error on Line 77 and the Error just says 0.
ext.versionMajor = Integer.parseInt(v[0]);
I don´t get it, why does it fail there? Am I assigning the value wrong to the properties?
Im not a gradle pro, I would be really happy if someone has an idea what I am doing wrong.
Link to script 1 link to script 2
Here is the code of the build.gradle file in the app folder of my Adnroid Project.
apply plugin: 'com.android.application'
ext.versionMajor = 0
ext.versionMinor = 0
ext.versionPatch = 0
ext.versionClassifier = null
ext.isSnapshot = true
ext.minimumSdkVersion = 21
//fetch version tag
setVersionNumberByTag()
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.webdesign.crf.eins"
minSdkVersion minimumSdkVersion
targetSdkVersion 25
versionCode generateVersionCode()
versionName generateVersionName()
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.volley:volley:1.0.0'
testCompile 'junit:junit:4.12'
}
private Integer generateVersionCode() {
return minimumSdkVersion * 10000000 + versionMajor;
}
private String generateVersionName() {
String versionName = "${versionMajor}.${versionMinor}.${versionPatch}"
if (ext.versionClassifier == null) {
if (isSnapshot) {
versionClassifier = "SNAPSHOT"
}
}
if (ext.versionClassifier != null) {
versionName += "-" + versionClassifier
}
return versionName;
}
private String setVersionNumberByTag() {
/*
* Gets the version name from the latest Git tag
*/
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
String verByGit = stdout.toString().trim()
String[] v = new String[3];
v = verByGit.split(".");
ext.versionMajor = Integer.parseInt(v[0]);
ext.versionMinor = Integer.parseInt(v[1]);
ext.versionPatch = Integer.parseInt(v[2]);
}
Upvotes: 1
Views: 1120
Reputation: 368
found a solution
apply plugin: 'com.android.application'
ext.versionMajor = null
ext.versionMinor = 0
ext.versionPatch = 1
ext.versionClassifier = null
ext.isSnapshot = true
ext.minimumSdkVersion = 21
android {
//fetch version tag
setVersionNumberByTag()
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.webdesign.crf.eins"
minSdkVersion minimumSdkVersion
targetSdkVersion 25
versionCode generateVersionCode()
versionName generateVersionName()
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.volley:volley:1.0.0'
testCompile 'junit:junit:4.12'
}
private Integer generateVersionCode() {
return ext.minimumSdkVersion * 10000000 + ext.versionMajor * 10000 + ext.versionMinor * 100 + ext.versionPatch
}
private String generateVersionName() {
String versionName = "${versionMajor}.${versionMinor}.${versionPatch}"
if (ext.versionClassifier == null) {
if (isSnapshot) {
versionClassifier = "SNAPSHOT"
}
}
if (ext.versionClassifier != null) {
versionName += "-" + versionClassifier
}
return versionName;
}
private String setVersionNumberByTag() {
/*
* Gets the version name from the latest Git tag
*/
def stdout = new ByteArrayOutputStream()
exec {
commandLine 'git', 'describe', '--tags'
standardOutput = stdout
}
def String verByGit = stdout.toString().trim()
def (major, minor, patch) = verByGit.tokenize(".");
ext.versionMajor = Integer.parseInt(major);
ext.versionMinor = Integer.parseInt(minor);
ext.versionPatch = Integer.parseInt(patch);
}
In gradle files groovy is used. That means its not possible to use someString.split(".");
like normal in java. I found out, that def (major, minor, patch) = verByGit.tokenize(".");
did the trick.
Upvotes: 1