📜 ⬆️ ⬇️

Generating a version of the android application from the subversion revision and git

When users encounter problems, you always want to know exactly which version of software they use. When using a version control system and automatic version numbering of software, such information can be provided to users, and if necessary, simply ask to dictate a string.

About how you can number your android project written here and here . In both articles, an example of getting a version of a project using 'svn info' is considered, and in the first article the author complains about the absence of SvnAnt , and in the second article the author notices a problem with the use of 'svn info'. The problem is that 'svn info' gives inaccurate information about the revision of the working copy.

The following is an example of solving this problem in a rather simple way.
UPD: git script added.

Problem


The bottom line is that the 'svn info' command issues the last commit revision of elements of the specified directory.
For example:
$ svn info Revision: 32 

Wherein:
 $ svn info ./src/ru/bsrgin/myproject/MyActivity.java Revision: 45 $ svn -r 32 -v log Changed paths: D /some-folder $ svn -r 45 -v log Changed paths: M /src/ru/bsrgin/myproject/MyActivity.java 

The authors of both mentioned articles interpret the output of the utility as a regular property file.
Those. the presence of lines of the Revision: 32 type allows them to interpret the data as parameters in the Build And script. Accordingly, the task is much more complicated if you run 'svn -R info' and look for the latest revision in the output file.
')
The query 'svn -r HEAD info' gives the revision number on the server, and not in the working copy, which is also incorrect, because the main condition is not met - generation of the current software version. Arguments BASE, COMMITTED and PREV also do not give an answer to the question - from the files of which version is the project assembled?

Decision


I was ready to abandon the method of obtaining the software version described in the article, but I remembered in time about another utility 'svnversion'. The output format of this utility is:
 4123:4168 mixed revision working copy 4168M modified working copy 4123S switched working copy 4123P partial working copy, from a sparse checkout 4123:4168MS mixed revision, modified, switched working copy 

Those. If I run 'svnversion' in my working copy, the following result will appear:
 $ svnversion 32:46 

And if you also modify a file, then:
 $ svnversion 32:46M 

As a matter of fact, the utility provides useful information that I would like to include in the application version, but the output format does not fit. I had to deal with the syntax of the Build Ant scripts ...

Below are instructions on how to add a version of a working copy of subversion or git to your project.

Sequencing


Create the svn-revision.build.xml file in the project root. Paste the following content into it:
 <project default="svn-revision"> <target name="svn-revision"> <!--  `svnversion -n`  ,      .  : 4123:4168 mixed revision working copy 4168M modified working copy 4123S switched working copy 4123P partial working copy, from a sparse checkout 4123:4168MS mixed revision, modified, switched working copy --> <exec executable="svnversion" output="svnversion.output"> <arg line="-n"/> </exec> <loadresource property="svnversion.Revision"> <file file="svnversion.output"/> </loadresource> <echo>Revision: ${svnversion.Revision}</echo> <!--     Manifest       VersionName --> <replaceregexp file="AndroidManifest.xml" match='android:versionName="([^".]+\.[^".]+)(\.[^"]*)?"' replace='android:versionName="\1.${svnversion.Revision}"' /> <!--    --> <delete file="svnversion.output"/> </target> </project> 

The implication is that AndroidManifest.xml is in the same directory as svn-revision.build.xml . If this is not the case, modify line 22. It also implies that the application version looks like 1.2 or 1.2.3 . If not, modify line 23.

Next, create the .externalToolBuilders / AddSvnRevisionToVersion.launch file and add the following lines to it.
 <?xml version="1.0" encoding="UTF-8" standalone="no"?> <launchConfiguration type="org.eclipse.ant.AntBuilderLaunchConfigurationType"> <booleanAttribute key="org.eclipse.ant.ui.ATTR_TARGETS_UPDATED" value="true"/> <booleanAttribute key="org.eclipse.ant.ui.DEFAULT_VM_INSTALL" value="false"/> <booleanAttribute key="org.eclipse.debug.core.ATTR_REFRESH_RECURSIVE" value="false"/> <stringAttribute key="org.eclipse.debug.core.ATTR_REFRESH_SCOPE" value="${project}"/> <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_PATHS"> <listEntry value="${project_loc}/svn-revision.build.xml"/> </listAttribute> <listAttribute key="org.eclipse.debug.core.MAPPED_RESOURCE_TYPES"> <listEntry value="1"/> </listAttribute> <booleanAttribute key="org.eclipse.debug.core.capture_output" value="false"/> <booleanAttribute key="org.eclipse.debug.ui.ATTR_CONSOLE_OUTPUT_ON" value="false"/> <booleanAttribute key="org.eclipse.debug.ui.ATTR_LAUNCH_IN_BACKGROUND" value="false"/> <stringAttribute key="org.eclipse.jdt.launching.CLASSPATH_PROVIDER" value="org.eclipse.ant.ui.AntClasspathProvider"/> <booleanAttribute key="org.eclipse.jdt.launching.DEFAULT_CLASSPATH" value="true"/> <stringAttribute key="org.eclipse.jdt.launching.PROJECT_ATTR" value="ANDROID-APP"/> <stringAttribute key="org.eclipse.ui.externaltools.ATTR_LOCATION" value="${project_loc}/svn-revision.build.xml"/> <stringAttribute key="org.eclipse.ui.externaltools.ATTR_RUN_BUILD_KINDS" value="full,incremental,"/> <booleanAttribute key="org.eclipse.ui.externaltools.ATTR_TRIGGERS_CONFIGURED" value="true"/> </launchConfiguration> 

Instead of ANDROID-APP, insert the name of your project (look at the value of the tag <name> in the .project file). Next, we modify the .project file — after the <buildCommand> tag, we add the following script:
 <buildCommand> <name>org.eclipse.ui.externaltools.ExternalToolBuilder</name> <triggers>full,incremental,</triggers> <arguments> <dictionary> <key>LaunchConfigHandle</key> <value><project>/.externalToolBuilders/AddSvnRevisionToVersion.launch</value> </dictionary> </arguments> </buildCommand> 

As a result, the first task will be added a process that modifies the AndroidManifest.xml file. The file will be updated whenever a build is launched, and the process will also be launched whenever a release package is built. If the release package of the package is automated or you are not using Eclipse, then you need to configure your builder to run svn-revision.build.xml .

Git


To get the same result when using SCV git, replace the svn-revision.build.xml file with the git-revision.build.xml below.
 <project default="git-revision"> <target name="git-revision"> <!--  'git describe'  ,      . . http://habrahabr.ru/blogs/android_development/132017/#comment_4385225 --> <exec executable="git" output="gitdescribe.output"> <arg line="describe --always --dirty=+ --abbrev=5"/> </exec> <loadresource property="git.Revision"> <file file="gitdescribe.output"/> </loadresource> <echo>Revision: ${git.Revision}</echo> <!--     Manifest       VersionName --> <replaceregexp file="AndroidManifest.xml" match='android:versionName="([^".]+\.[^".]+)(\.[^"]*)?"' replace='android:versionName="\1.${git.Revision}"' /> <!--    --> <delete file="gitdescribe.output"/> </target> </project> 

Accordingly, do not forget to fix the files AddSvnRevisionToVersion.launch and .project .

Get versionName programmatically


Now, in order to get the software version, you can use this method:
 public static getApplicationVersion() { try { return getInstance().getApplicationContext().getPackageManager() .getPackageInfo(getPackageName(), 0).versionName; } catch (NameNotFoundException e) { return "App not installed!"; } } 

As a result, in my project I got the line 1.0.32: 46M (subversion) and 1.0.58c57 + (git).

Source: https://habr.com/ru/post/132017/


All Articles