Automatic version with the number of your Android application using Git and Eclipse

I believe that computers are the best things to do repetitive tasks. Of course I don’t, I either forget or (basically) do nothing in a consistent way - this is not what I want.

One of the things I’m most likely to do is to forget about the version in the manifest when I publish the new version of the Android app. I used to work with customized build systems that have the function of the automatic version of numbering, and I used it (or maybe I was lazy).

I found this https://stackoverflow.com/a/166148/ very useful in developing the solution, but it took me a while to set it up so that it worked exactly the way I want. Earlier attempts have ever been a reason to continue building that was painful. So I decided to post my decision here in the hope that someone would find it useful.

+12
git android versioning
Feb 15 '13 at 12:51
source share
4 answers

This solution was developed on Linux. I'm sure experienced Windows and Mac developers can adapt them to their platforms, but I'm not such a developer. Linux is where my skill set lives.

Git has a nice feature in the git describe --dirty . It bounces back the commit log and finds the tag, and then builds the version string. If this is a “production build" where the last commit was flagged and all files were checked, then this is your version string. If this is a development assembly, then the last tag is added with the number of additional commits and a shortened hash code. The --dirty flag is just a cherry on the icing on the cake: it adds the word dirty if there are any modified files that have not yet been committed. This is perfect for your android:versionName in the manifest file.

android:versionCode requires a number. This is needed for watches for releases, but not for building development, and since each version will have a tag with the version, I just count them. I always mark my versions in the form v<major>.<minor>[.<patch>] , where <major> , <minor> and <patch> are just numbers. Therefore, tags that start with the lowercase “v” are counted, followed by a digit, all of which is really necessary here.

After completing the template manifest file, I found that the best way is to simply use the AndroidManifest.xml file in the project database, edited using the sed stream editor, and put the result in bin / AndroidManifest.xml.

So, I developed the script below, placing it in the scripts folder at the same level as my projects (so that they all can share the same script), and then set up my own constructor in Eclipse.

There is a script that I called version.sh :

 #/bin/bash echo "Auto Version: `pwd`" CODE=`git tag | grep -c ^v[0-9]` NAME=`git describe --dirty | sed -e 's/^v//'` COMMITS=`echo ${NAME} | sed -e 's/[0-9\.]*//'` if [ "x${COMMITS}x" = "xx" ] ; then VERSION="${NAME}" else BRANCH=" (`git branch | grep "^\*" | sed -e 's/^..//'`)" VERSION="${NAME}${BRANCH}" fi echo " Code: ${CODE}" echo " Ver: ${VERSION}" cat AndroidManifest.xml | \ sed -e "s/android:versionCode=\"[0-9][0-9]*\"/android:versionCode=\"${CODE}\"/" \ -e "s/android:versionName=\".*\"/android:versionName=\"${VERSION}\"/" \ > bin/AndroidManifest.xml exit 0 

To configure the constructor, follow these steps:

one). Right-click the project base and select Properties and then Builders.

2). Click the "Create" button and select "Program."

3). Name your version something like "<project> Auto Version". This line should be unique for all projects.

four). Set up the Home tab as follows:

4a). In the Location section, use File System Overview and navigate and select the script file.

4b). In the "Working Directory" section, use "Workspace Overview" to select a project.

5). Leave the "Update resources after completion" checkbox unchecked on the "Update" tab.

6). Do not set any variables on the Environment tab.

7). On the Build Options tab:

7a). Make sure "During manual builds" is checked and

7b). Also marked "During Auto Build".

7c). Now I have the rest. I don’t even give him a console. The eagle watching him may have noticed that the script is displaying some information, but now it works for me, I just want everything to be quiet, without bothering me.

8). Fine tune your build options, and then place your constructor between Android Precompilation and Java Builder.

Go back to creating secure applications, knowing that they are correctly versioned, and check the information about your application. Isn't that a version number .:-)

Steve

+13
Feb 15 '13 at 12:51
source share

Idea (using ant and git executables)

ok, here's a new way to do this:

  • to calculate version.code :

    git rev-list master --first-parent --count

    this follows the version guide . Since it effectively finds the number of commits from the initial commit (inclusive), which always increases from the previous version.

  • to calculate version.name :

    git describe --tags --dirty --abbrev=7

Implementation:

build.xml usually imports a custom ant script called custom_rules.xml

so the contents of the script is:

 <?xml version="1.0" encoding="UTF-8"?> <project name="application-custom"> <macrodef name="git" taskname="@{taskname}"> <attribute name="command" /> <attribute name="dir" default="" /> <attribute name="property" default="" /> <attribute name="taskname" default="" /> <attribute name="failonerror" default="on" /> <element name="args" optional="true" /> <sequential> <exec executable="git" dir="@{dir}" outputproperty="@{property}" failifexecutionfails="@{failonerror}" failonerror="@{failonerror}"> <arg value="@{command}" /> <args/> </exec> </sequential> </macrodef> <target name="-pre-build"> <git command="rev-list" property="versioning.code" taskname="versioning"> <args> <arg value="master" /> <arg value="--first-parent" /> <arg value="--count" /> </args> </git> <git command="describe" property="versioning.name" taskname="versioning"> <args> <arg value="--tags" /> <arg value="--dirty" /> <arg value="--abbrev=7" /> </args> </git> <echo level="info" taskname="versioning">${versioning.code}, ${versioning.name}</echo> <replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="${versioning.code}"' /> <replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="${versioning.name}"' /> </target> <target name="-post-build" > <replaceregexp file="AndroidManifest.xml" match='android:versionCode=".*"' replace='android:versionCode="0"' /> <replaceregexp file="AndroidManifest.xml" match='android:versionName=".*"' replace='android:versionName="0"' /> </target> 

just would do.

In the nut shell, it simply replaces android.versionCode and android.versionName with the current code and the name of the current version stored in git.

Warnings

  • The code and name of the initial version are set to 0 after the build is complete. If you need it, replace the zero in the -post-build target, or (although I doubt very much that you will require it) you can configure it and put it in some property (file or built-in, of your choice).
  • If the assembly failed or was interrupted, the version will remain as it is. (although I doubt very much that this is a concern, just return the file!)

Enjoy.

Refs

Important Editing

  • prevent the use of HEAD to calculate assembly numbers; causes a downgrade problem when building during development, and then when installing a stable version (or when executing a beta version on the main release). using master instead (or the branch that is used for production assemblies).

PS: relevant for AS users: Describe the automatic execution of an Android project with git using Android Studio / Gradle

+12
Dec 14 '13 at 13:56 on
source share

Using Android Studio (Gradle): Check out this blog post: http://blog.android-develop.com/2014/09/automatic-versioning-and-increment.html

Here is the blog post implementation:

  android { defaultConfig { ... // Fetch the version according to git latest tag and "how far are we from last tag" def longVersionName = "git -C ${rootDir} describe --tags --long".execute().text.trim() def (fullVersionTag, versionBuild, gitSha) = longVersionName.tokenize('-') def(versionMajor, versionMinor, versionPatch) = fullVersionTag.tokenize('.') // Set the version name versionName "$versionMajor.$versionMinor.$versionPatch($versionBuild)" // Turn the version name into a version code versionCode versionMajor.toInteger() * 100000 + versionMinor.toInteger() * 10000 + versionPatch.toInteger() * 1000 + versionBuild.toInteger() // Friendly print the version output to the Gradle console printf("\n--------" + "VERSION DATA--------" + "\n" + "- CODE: " + versionCode + "\n" + "- NAME: " + versionName + "\n----------------------------\n") ... } } 
+6
Sep 07 '14 at 6:10
source share
 ' for windows ' preBuildMy.cmd include repVer.vbs ' repVer.vbs : Dim objFileSystem, objOutputFile Dim objInputFile Dim sText,gitFile FileName = "./bin/AndroidManifest.xml" ' gitFile = ".\..\.git\refs\heads\master" gitFile = ".\..\.git\refs\remotes\origin\master" Set objFileSystem = CreateObject("Scripting.fileSystemObject") set objInputFile= objFileSystem.OpenTextFile(FileName) sText= objInputFile.ReadAll set objOutputFile = objFileSystem.CreateTextFile(FileName , TRUE) set objInputFile= objFileSystem.OpenTextFile(gitFile) refText= objInputFile.ReadAll sText = Replace(sText,"v1.0","v 1.0 " & Now & " ref=" & mid(refText,1,7)) objOutputFile.WriteLine(sText) objOutputFile.Close Set objFileSystem = Nothing WScript.Quit(0) 
0
Mar 03 '13 at 14:20
source share



All Articles