I'm trying to emulate Maven release plugin in Android by using a customized version of gradle-release plugin: https://github.com/townsfolk/gradle-release
The interesting steps are:
- Check uncommitted changes
- Step version code and remove -SNAPSHOT suffix from version name
- Build
- Step version name and add -SNAPSHOT suffix for next development version
However the generated APK always has the previous versions (i.e. 1.0.0-SNAPSHOT instead of 1.0.0).
Version numbers are stored and correctly updated in gradle.properties, so I'm assuming that I need to update the versions in the data model as well for the changes to take effect.
My android plugin config:
defaultConfig {
versionCode versionCode as int // taken from gradle.properties
versionName versionName // taken from gradle.properties
minSdkVersion 10
targetSdkVersion 19
}
Things I tried:
preBuild << {
android.applicationVariants.each { variant ->
variant.versionName = versionName
}
}
But there's no versionName in a variant.
preBuild << {
android.buildTypes.each { type ->
type.versionName = versionName
}
}
But there's no versionName in a type.
preBuild << {
android.productFlavors.each { flavor ->
flavor.versionName = versionName
}
}
But there are no flavors in my app (plain debug and release build types only).
My alternative is to write a bash/bat script to step the versions before invoking Gradle, which pretty much defeats the purpose of using Groovy to improve build customization.
How can I update versions dynamically in the Android Gradle plugin in the execution phase?
I needed to append current git commit count of code revision to the version name. Its real handy in many situation. I ended up with below simple gradle file
Similar to gitCommitCount, You can generate variables of your own to customise version name. As i am just executing a terminal command to store its result in a variable.
This doesn't directly address your question of how to completely change the versionName, but this is what I use to append a suffix for my buildTypes:
I was facing similar need of having separate build logic for release and non-release builds. Apart from different versioning, I had to use a different set of dependencies, even different repositories.
None of the available plugins had all of the features that I needed, so I developed my own solution, based on simple approach - command line argument.
You can pass a command line parameter when invoking gradle build script like this:
or in my case
Gradle will parse it, and it would automagically be available as a property of the project object. You could then use it like this:
I extracted this logic into a separate plugin, and I've been successfully using it across different projects.
Although this doesn't answer your question directly, I hope I gave you another angle to think about the problem and another possible solution.
I just used Javanator's answer and modified it a bit so that commit count not only helps in changing the name but also makes sure that version code also remains unique. Here is a sample of what I did (Maybe a couple of things can be optimized, but nevertheless does the job for me) :
Edit : The last bit could also be like
That's what
buildTypes
are for. What you're describing is arelease
build, IMO.Here's an example: when executing
assembleDebug
it will give you a snapshot build, and executingassembleRelease
will give you a clean build without any suffix and incremented version number. The next debug build will also use the incremented number.The following is a fully functional build when the files are created in a folder. It should also work with flavors, but that's just a side product :). Gradle 2.2.1, Android plugin 1.1.3
build.gradle
src/main/AndroidManifest.xml
auto-version.gradle
Execute
gradle assembleDebug
to build normally,gradle assembleRelease
to increment and build, andgradle incrementVersion
to just increment. Note: be careful withgradle assemble
because the order ofassembleDebug
andassembleRelease
will yield different results.Check the generated files in the
build
directory to see if the values are to your liking.Manual execution (from comments)
It is possible you have multiple flavors in which case the version is incremented multiple times because multiple variants match the release build type. The original quesion was for no flavors. If you want to have more control when the version number is incremented just remove the
afterEvaluate
block and call theincrementVersion
task whenever you want:(The above manual execution is an untested idea.)
Check uncommitted changes
The "Check uncommitted changes" are not covered in this answer, that's another game. You could hook on to
tasks.preBuild.doFirst { /*fail here if uncommited changes*/ }
if I understand correctly. But that highly depends on your version control. Ask another question for more!