{"id":37306,"date":"2018-06-28T12:20:34","date_gmt":"2018-06-28T19:20:34","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=37306"},"modified":"2019-05-24T12:55:15","modified_gmt":"2019-05-24T19:55:15","slug":"advanced-android-versioncode","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/advanced-android-versioncode\/","title":{"rendered":"Use MSBuild to Define Advanced Android VersionCode"},"content":{"rendered":"<p>With the introduction of Xamarin.Android v7.2, two new MSBuild properties were also introduced in order to make maintaining Android VersionCode for Android apps even easier:<\/p>\n<ul>\n<li>AndroidVersionCodePattern<\/li>\n<li>AndroidVersionCodeProperties<\/li>\n<\/ul>\n<h2>Advanced Android VersionCode<\/h2>\n<p>Typically, to change the version number on an Android application you need to edit the <code>AndroidManifest.xml<\/code> file and update the <code>android:versionCode<\/code>. Alternatively, you could implement your own system for generating the code and updating the XML file. With these two properties in place, you will be able to define the <code>versionCode<\/code> within your MSBuild project. All this is built right into the build system.<\/p>\n<h3>AndroidVersionCodePattern<\/h3>\n<p>This property allows you to define the pattern desired for your <code>versionCode<\/code> and inputs in a similar way to <code>string.Format<\/code> work. You provide a number of <strong>{token}<\/strong> values which are replaced at build time. Out of the box we support of a number of default {tokens}:<\/p>\n<ul>\n<li>ABI \u2013 Inserts the targeted ABI for the app\n<ol>\n<li>armeabi<\/li>\n<li>armeabi-v7a<\/li>\n<li>x86<\/li>\n<li>arm64-v8a<\/li>\n<li>x86_64<\/li>\n<\/ol>\n<\/li>\n<li><strong>MinSDK<\/strong> \u2013 Inserts the minimum supported Sdk value from the AndroidManifest.xml or 11 if none is defined.<\/li>\n<li><strong>VersionCode<\/strong> \u2013 Uses the version code directly from Properties\\AndroidManifest.xml.<\/li>\n<\/ul>\n<p>By default the value of <code>$(AndroidVersionCodePattern)<\/code> will be set to <code>{abi}{versionCode:D6}<\/code>. So as an example, if you are splitting your <a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/android\/deploy-test\/building-apps\/abi-specific-apks#creating-the-version-code-for-the-apk\">APK&#8217;s by ABI<\/a> and your <code>versionCode<\/code> in the <code>AndroidManifest.xml<\/code> is currently 1, the following should result:<\/p>\n<ul>\n<li><code>2000001<\/code> for armeabi-v7a<\/li>\n<li><code>4000001<\/code> for armeabi-v8a<\/li>\n<\/ul>\n<p>This can be broken down like so:<\/p>\n<table class=\"table table-bordered; margin: 0px auto;\">\n<tbody>\n<tr>\n<th style=\"text-align: center;\">2<\/th>\n<th style=\"text-align: center;\">000001<\/th>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">{abi}<\/td>\n<td style=\"text-align: center;\">{versionCode:D8}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>Notice the {token} supports the same zero padding definition that <code>string.Format<\/code> supports. In this example, the <code>D6<\/code> means &#8220;left pad the result so that it is 6 digits long&#8221;. Another example might be <code>{abi}{minSDK:D2}{versionCode:D4}<\/code>. If the <strong>minSdk<\/strong> was API-9 and your <code>versionCode<\/code> in the <code>AndroidManifest.xml<\/code> is currently 21, you will see the following result.<\/p>\n<ul>\n<li><code>2090021<\/code> for armeabi-v7a<\/li>\n<li><code>4090021<\/code> for armeabi-v8a<\/li>\n<\/ul>\n<p>This can be broken down like so:<\/p>\n<table class=\"table table-bordered; margin: 0px auto;\">\n<tbody>\n<tr>\n<th style=\"text-align: center;\">2<\/th>\n<th style=\"text-align: center;\">09<\/th>\n<th style=\"text-align: center;\">0021<\/th>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">{abi}<\/td>\n<td style=\"text-align: center;\">{minSDK:D2}<\/td>\n<td style=\"text-align: center;\">{versionCode:D4}<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h3><\/h3>\n<h3>AndroidVersionCodeProperties<\/h3>\n<p>Support for allowing you to define your own {token} has also been added. This is done via the <code>$(AndroidVersionCodeProperties)<\/code>. This is a comma-separated <strong>token=value<\/strong> pair string which defines the new {token} and its value. The <strong>value<\/strong> can be hard-coded for any one of the many MSBuild properties which are available.<\/p>\n<p>For example: <code>screen=23;target=$(_SupportedApiLevel)<\/code>. This defines two new tokens <strong>{screen}<\/strong> and <strong>{target}<\/strong>. One of which is derived from a Xamarin.Android MSBuild property. With this in mind, you can now define a <code>$(AndroidVersionCodePattern)<\/code> of:<\/p>\n<pre><code>{abi}{minSDK:D2}{screen:D2}{target:D2}{versionCode:D4}<\/code><\/pre>\n<p>Which will make use of your custom {token} values in the final APK <code>versionCode<\/code><\/p>\n<h3>Getting VersionCode from your Assembly<\/h3>\n<p>Information for the <code>AndroidManifest.xml<\/code> can be useful if your project is setup to auto increment your assembly version for each build.<\/p>\n<p>Use the MSBuild Task <code>GetAssemblyIdentity<\/code> to retrieve the compiled assembly&#8217;s version information. Then use some MSBuild:Foo to split that into a usable format. Finally, create a set of {token}={value} items in the <code>$(AndroidVersionCodeProperties)<\/code> property.<\/p>\n<pre class=\"\"><code>&lt;Target Name=\"CalculateVersionCodeProperties\" AfterTargets=\"CoreCompile\"&gt;\r\n    &lt;GetAssemblyIdentity AssemblyFiles=\"$(OutputPath)$(AssemblyName).dll\"&gt;\r\n      &lt;Output TaskParameter=\"Assemblies\" ItemName=\"AssemblyVersionInfo\" \/&gt;\r\n    &lt;\/GetAssemblyIdentity&gt;\r\n    &lt;PropertyGroup&gt;\r\n      &lt;BuildNumber&gt;$([System.Version]::Parse(%(AssemblyVersionInfo.Version)).ToString(4))&lt;\/BuildNumber&gt;\r\n      &lt;Major&gt;$(BuildNumber.Split('.')[0])&lt;\/Major&gt;\r\n      &lt;Minor&gt;$(BuildNumber.Split('.')[1])&lt;\/Minor&gt;\r\n      &lt;Build&gt;$(BuildNumber.Split('.')[2])&lt;\/Build&gt;\r\n      &lt;Revision&gt;$(BuildNumber.Split('.')[3])&lt;\/Revision&gt;\r\n      &lt;Year&gt;$([System.DateTime]::Now.ToString ('yyyy'))&lt;\/Year&gt;\r\n      &lt;Month&gt;$([System.DateTime]::Now.ToString ('MM'))&lt;\/Month&gt;\r\n      &lt;Day&gt;$([System.DateTime]::Now.ToString ('dd'))&lt;\/Day&gt;\r\n    &lt;\/PropertyGroup&gt;\r\n    &lt;PropertyGroup&gt;\r\n     &lt;AndroidVersionCodeProperties&gt;year=$(Year);month=$(Month);day=$(Day);major=$(Major);minor=$(Minor);build=$(Build);revision=$(Revision)&lt;\/AndroidVersionCodeProperties&gt;\r\n    &lt;\/PropertyGroup&gt;\r\n  &lt;\/Target&gt;\r\n<\/code><\/pre>\n<p>With <code>$(AndroidVersionCodeProperties)<\/code> in place we can now define our <code>$(AndroidVersionCodePattern)<\/code>.<\/p>\n<pre class=\"\"><code>{abi}{major:D2}{minor:D2}{build:D2}{revision:D4}\r\n<\/code><\/pre>\n<p>&nbsp;<\/p>\n<h2>Wrapping Up<\/h2>\n<p>Once this is all in place, there is no need to update the Android VersionCode in the <code>AndroidManifest.xml<\/code> as it can all be done programmatically. Just remember to update the <code>versionName<\/code> when the<strong>major<\/strong> build version updates.<\/p>\n<p>To discuss this article, head on over to our <a href=\"https:\/\/forums.xamarin.com\/128723\/\">Xamarin Forums<\/a>!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the introduction of Xamarin.Android v7.2, two new MSBuild properties were also introduced in order to make maintaining Android VersionCode for Android apps even easier: AndroidVersionCodePattern AndroidVersionCodeProperties Advanced Android VersionCode Typically, to change the version number on an Android application you need to edit the AndroidManifest.xml file and update the android:versionCode. Alternatively, you could implement [&hellip;]<\/p>\n","protected":false},"author":4328,"featured_media":39167,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[313],"tags":[5],"class_list":["post-37306","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android"],"acf":[],"blog_post_summary":"<p>With the introduction of Xamarin.Android v7.2, two new MSBuild properties were also introduced in order to make maintaining Android VersionCode for Android apps even easier: AndroidVersionCodePattern AndroidVersionCodeProperties Advanced Android VersionCode Typically, to change the version number on an Android application you need to edit the AndroidManifest.xml file and update the android:versionCode. Alternatively, you could implement [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/37306","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/4328"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=37306"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/37306\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=37306"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=37306"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=37306"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}