Skip to content

Commit

Permalink
[Xamarin.Android.Build.Tasks] Implement a new process for defining ve…
Browse files Browse the repository at this point in the history
…rsionCode.

Context https://bugzilla.xamarin.com/show_bug.cgi?id=51620
Context https://bugzilla.xamarin.com/show_bug.cgi?id=51618
Context https://bugzilla.xamarin.com/show_bug.cgi?id=51145

Our current version system for multiple apk's for each Abi
is a bit broken [1]. If a user for example has a versionCode
set which is 123 the final version code for an x86_64 build
ends up as 327803.
This is completely transparent to the user and also does not
follow the guidance in the documentation at [1] and [2].

So we need a new system :) but as usual we have to support the
old system. So we are introducing a new system which is more
flexible. This will only apply when the `$(AndroidCreatePackagePerAbi)`
is set to `True`.

The new system has two new properties

	<AndroidVersionCodePattern/>
	<AndroidVersionCodeProperties/>

The first allows the developer to define the Pattern to be used
for the versonCode. The pattern will be made up of a format string
which will contain keys. These keys will be replaced with values
form one of the known keys or a custom user defined one.

We define a few known key values

- abi : The current target abi converted to an int where
	- 'armeabi' = 1,
	- 'armeabi-v7a' = 2,
	- 'x86' = 3,
	- 'arm64-v8a' = 4,
	- 'x86_64' = 5,

- minSDK : The minSDK value from the manifest or 11 if not present.

- versionCode : The versionCode from the manifest.

With these keys the user can define a pattern of

	{abi}{minSDK}{versionCode}

or if they way to include zero padding they can use

	{abi}{minSDK}{versionCode:D4}

similar to the left padding formats used in string.Format ().

Users can also use the `$(AndroidVersionCodeProperties)` property
to define new custom keys. This string will be in the form of a
semi-colon delimited key=value pairs. For example

	foo=12;bar=$(SomeBuildProperty)

when can then be used in the pattern.

	{abi}{foo}{bar}{versionCode}

Lets work through an example. The user defines a version code of '123'
in the manifest and enables `$(AndroidCreatePackagePerAbi)`. They define
a `$(AndroidVersionCodePattern)` of `{abi}{versionCode:D5}`.
This will result in the following version code being produced for the
'x86' build.

	300123

The first 3 is the `{abi}` value. The rest is the left zero padded
versionCode.
A slightly more complex pattern would be `{abi}{minSDK:D2}{versionCode:D4}`
which would produce

	3140123

if the minimumSdk value was set to API 14.

A more real life example mgiht be as follows. A user wants to use the `Build` value
from the AssemblyInfo.cs . They define the following target

```xml
<Target Name="_GetBuild" AfterTargets="Compile">
  <GetAssemblyIdentity AssemblyFiles="Foo.dll">
      <Output
          TaskParameter="Assemblies"
          ItemName="MasterVersion"/>
    </GetAssemblyIdentity>
    <PropertyGroup>
       <BuildVersion>$([System.Version]::Parse(%(MasterVersion.Version)).Build)</BuildVersion>
    </PropertyGroup>
</Target>
```
This extracts the build version from the built assembly. They can then define a
pattern of

	{abi}{minSDK}{build:D4}

and set the properties to

	build=$(BuildVersion)

Given similar properties from the previous example e.g abi=x86 and minSDk=14,
this will result in the follwing output (assuming the `Build` value was 3421).

	3143421

[1] https://developer.xamarin.com/guides/android/advanced_topics/build-abi-specific-apks/
[2] https://developer.android.com/google/play/publishing/multiple-apks.html#Rules
  • Loading branch information
dellis1972 committed Jul 27, 2017
1 parent 1ea5f07 commit ed90962
Show file tree
Hide file tree
Showing 5 changed files with 289 additions and 14 deletions.
83 changes: 74 additions & 9 deletions Documentation/build_process.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ dateupdated: 2017-06-22

The Xamarin.Android build process is responsible for gluing everything
together:
[generating `Resource.designer.cs`](/guides/android/advanced_topics/api_design#Resources),
[generating `Resource.designer.cs`](https://developer.xamarin.com/guides/android/advanced_topics/api_design#Resources),
supporting the `AndroidAsset`, `AndroidResource`, and other
[build actions](#Build_Actions), generating
[Android-callable wrappers](/guides/android/advanced_topics/java_integration_overview/android_callable_wrappers),
[Android-callable wrappers](https://developer.xamarin.com/guides/android/advanced_topics/java_integration_overview/android_callable_wrappers),
and generating a `.apk` for execution on Android devices.

<a name="App_Packaging" class="injected"></a>
Expand Down Expand Up @@ -247,7 +247,7 @@ when packaing Release applications.
Added in Xamarin.Android 6.1.

- **AndroidHttpClientHandlerType** &ndash; Allow setting the value of the
[`XA_HTTP_CLIENT_HANDLER_TYPE` environment variable](/guides/android/advanced_topics/environment/#XA_HTTP_CLIENT_HANDLER_TYPE).
[`XA_HTTP_CLIENT_HANDLER_TYPE` environment variable](https://developer.xamarin.com/guides/android/advanced_topics/environment/#XA_HTTP_CLIENT_HANDLER_TYPE).
This value will not override an explicitly specified
`XA_HTTP_CLIENT_HANDLER_TYPE` value. An
`XA_HTTP_CLIENT_HANDLER_TYPE` environment variable value specified
Expand Down Expand Up @@ -278,7 +278,7 @@ when packaing Release applications.
**Experimental**. Added in Xamarin.Android 7.1.

- **AndroidLinkMode** &ndash; Specifies which type of
[linking](/guides/android/advanced_topics/linking/) should be
[linking](https://developer.xamarin.com/guides/android/advanced_topics/linking/) should be
performed on assemblies contained within the Android package. Only
used in Android Application projects. The default value is
*SdkOnly*. Valid values are:
Expand Down Expand Up @@ -310,7 +310,7 @@ when packaing Release applications.

- **AndroidManifest** &ndash; Specifies a filename to use as the
template for the app's
[`AndroidManifest.xml`](/guides/android/advanced_topics/working_with_androidmanifest.xml/).
[`AndroidManifest.xml`](https://developer.xamarin.com/guides/android/advanced_topics/working_with_androidmanifest.xml/).
During the build, any other necessary values will be merged into to
produce the actual `AndroidManifest.xml`.
The `$(AndroidManifest)` must contain the package name in the `/manifest/@package` attribute.
Expand Down Expand Up @@ -501,6 +501,71 @@ when packaing Release applications.

Added in Xamarin.Android 7.1.

<a name="AndroidVersionCodePattern" class="injected"></a>
- **AndroidVersionCodePattern** &ndash; A string property which allows
the developer to customize the `versionCode` in the manifest.
See [Creating the Version Code for the APK](https://developer.xamarin.com/guides/android/advanced_topics/build-abi-specific-apks/#Creating_the_Version_Code_for_the_APK)
for information on deciding a `versionCode`.

Some examples, if `abi` is `armeabi` and `versionCode` in the manifest
is `123`

{abi}{versionCode}

will produce a versionCode of `1123` when `$(AndroidCreatePackagePerAbi)`
is True, otherwise will produce a value of 123.
If `abi` is `x86_64` and `versionCode` in the manifest
is `44`. This will produce `544` when `$(AndroidCreatePackagePerAbi)`
is True, otherwise will produce a value of `44`.

If we include a left padding format string

{abi}{versionCode:0000}

it would produde `50044` because we are left padding the `versionCode`
with `0`. Alternatively you can use the decimal padding such as

{abi}{versionCode:D4}

which does the same as the previous example.

Only '0' and 'Dx' padding format strings are supported since the value
MUST be an integer.

Pre defined key items

- **abi** &ndash; Inserts the targetted abi for the app
- 1 &ndash; `armeabi`
- 2 &ndash; `armeabi-v7a`
- 3 &ndash; `x86`
- 4 &ndash; `arm64-v8a`
- 5 &ndash; `x86_64`

- **minSDK** &ndash; Inserts the minimum supported Sdk
value from the `AndroidManifest.xml` or `11` if none is
defined.

- **versionCode** &ndash; Uses the version code direrctly from
`Properties\AndroidManifest.xml`.

You can define custom items using the [AndroidVersionCodeProperties](#AndroidVersionCodeProperties)
property.

Added in Xamarin.Android 7.2.

<a name="AndroidVersionCodeProperties" class="injected"></a>
- **AndroidVersionCodeProperties** &ndash; A string property which allows
the developer to define custom items to use with the [AndroidVersionCodePattern](#AndroidVersionCodePattern).
They are in the form of a `key=value` pair. All items in the `value` should
be integer values.

screen=23;target=$(_SupportedApiLevel)

As you can see you can make use of existing or custom MSBuild properties
in the string.

Added in Xamarin.Android 7.2.

## Binding Project Build Properties

The following MSBuild properties are used with
Expand Down Expand Up @@ -675,7 +740,7 @@ within the project and control how the file is processed.
## AndroidEnvironment

Files with a Build action of `AndroidEnvironment` are used
to [initialize environment variables and system properties during process startup](/guides/android/advanced_topics/environment/).
to [initialize environment variables and system properties during process startup](https://developer.xamarin.com/guides/android/advanced_topics/environment/).
The `AndroidEnvironment` Build action may be applied to
multiple files, and they will be evaluated in no particular order (so don't
specify the same environment variable or system property in multiple
Expand Down Expand Up @@ -748,7 +813,7 @@ example:

## AndroidNativeLibrary

[Native libraries](/guides/android/advanced_topics/cpu_architecture/#Android_Native_Library_Installation)
[Native libraries](https://developer.xamarin.com/guides/android/advanced_topics/cpu_architecture/#Android_Native_Library_Installation)
are added to the build by setting their Build action to
`AndroidNativeLibrary`.

Expand Down Expand Up @@ -787,7 +852,7 @@ Build action will result in a `XA0101` warning.
## LinkDescription

Files with a *LinkDescription* build action are used to
[control linker behavior](/guides/cross-platform/advanced/custom_linking/).
[control linker behavior](https://developer.xamarin.com/guides/cross-platform/advanced/custom_linking/).


<a name="ProguardConfiguration"></a>
Expand All @@ -797,7 +862,7 @@ Files with a *LinkDescription* build action are used to
Files with a *ProguardConfiguration* build action contain options which
are used to control `proguard` behavior. For more information about
this build action, see
[ProGuard](/guides/android/deployment,_testing,_and_metrics/proguard/).
[ProGuard](https://developer.xamarin.com/guides/android/deployment,_testing,_and_metrics/proguard/).

These files are ignored unless the `$(EnableProguard)` MSBuild property
is `True`.
Expand Down
26 changes: 24 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/Aapt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,19 @@ public class Aapt : AsyncTask

public bool ExplicitCrunch { get; set; }

// pattern to use for the version code. Used in CreatePackagePerAbi
// eg. {abi:00}{dd}{version}
// known keyworks
// {abi} the value for the current abi
// {version} the version code from the manifest.
public string VersionCodePattern { get; set; }

// Name=Value pair seperated by ';'
// e.g screen=21;abi=11
public string VersionCodeProperties { get; set; }

public string AndroidSdkPlatform { get; set; }

Dictionary<string,string> resource_name_case_map = new Dictionary<string,string> ();

bool ManifestIsUpToDate (string manifestFile)
Expand Down Expand Up @@ -190,6 +203,8 @@ public override bool Execute ()
Log.LogDebugMessage (" ExtraArgs: {0}", ExtraArgs);
Log.LogDebugMessage (" CreatePackagePerAbi: {0}", CreatePackagePerAbi);
Log.LogDebugMessage (" ResourceNameCaseMap: {0}", ResourceNameCaseMap);
Log.LogDebugMessage (" VersionCodePattern: {0}", VersionCodePattern);
Log.LogDebugMessage (" VersionCodeProperties: {0}", VersionCodeProperties);
if (CreatePackagePerAbi)
Log.LogDebugMessage (" SupportedAbis: {0}", SupportedAbis);

Expand Down Expand Up @@ -244,8 +259,15 @@ protected string GenerateCommandLineCommands (string ManifestFile, string curren
Directory.CreateDirectory (manifestDir);
manifestFile = Path.Combine (manifestDir, Path.GetFileName (ManifestFile));
ManifestDocument manifest = new ManifestDocument (ManifestFile, this.Log);
if (currentAbi != null)
manifest.SetAbi (currentAbi);
manifest.SdkVersion = AndroidSdkPlatform;
if (currentAbi != null) {
if (!string.IsNullOrEmpty (VersionCodePattern))
manifest.CalculateVersionCode (currentAbi, VersionCodePattern, VersionCodeProperties);
else
manifest.SetAbi (currentAbi);
} else if (!string.IsNullOrEmpty (VersionCodePattern)) {
manifest.CalculateVersionCode (null, VersionCodePattern, VersionCodeProperties);
}
manifest.ApplicationName = ApplicationName;
manifest.Save (manifestFile);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using NUnit.Framework;
using Xamarin.ProjectTools;
Expand Down Expand Up @@ -264,6 +264,144 @@ public void DirectBootAwareAttribute ()
}
}

static object [] VersionCodeTestSource = new object [] {
new object[] {
/* seperateApk */ false,
/* abis */ "armeabi-v7a",
/* versionCode */ "123",
/* pattern */ null,
/* props */ null,
/* shouldBuild */ true,
/* expected */ "123",
},
new object[] {
/* seperateApk */ false,
/* abis */ "armeabi-v7a",
/* versionCode */ "123",
/* pattern */ "{abi}{versionCode}",
/* props */ null,
/* shouldBuild */ true,
/* expected */ "123",
},
new object[] {
/* seperateApk */ false,
/* abis */ "armeabi-v7a",
/* versionCode */ "1",
/* pattern */ "{abi}{versionCode}",
/* props */ "versionCode=123",
/* shouldBuild */ true,
/* expected */ "123",
},
new object[] {
/* seperateApk */ false,
/* abis */ "armeabi-v7a;x86",
/* versionCode */ "123",
/* pattern */ "{abi}{versionCode}",
/* props */ null,
/* shouldBuild */ true,
/* expected */ "123",
},
new object[] {
/* seperateApk */ true,
/* abis */ "armeabi-v7a;x86",
/* versionCode */ "123",
/* pattern */ null,
/* props */ null,
/* shouldBuild */ true,
/* expected */ "131195;196731",
},
new object[] {
/* seperateApk */ true,
/* abis */ "armeabi-v7a;x86",
/* versionCode */ "123",
/* pattern */ "{abi}{versionCode}",
/* props */ null,
/* shouldBuild */ true,
/* expected */ "2123;3123",
},
new object[] {
/* seperateApk */ true,
/* abis */ "armeabi-v7a;x86",
/* versionCode */ "12",
/* pattern */ "{abi}{minSDK:00}{versionCode:000}",
/* props */ null,
/* shouldBuild */ true,
/* expected */ "211012;311012",
},
new object[] {
/* seperateApk */ true,
/* abis */ "armeabi-v7a;x86",
/* versionCode */ "12",
/* pattern */ "{abi}{minSDK:00}{screen}{versionCode:000}",
/* props */ "screen=24",
/* shouldBuild */ true,
/* expected */ "21124012;31124012",
},
new object[] {
/* seperateApk */ true,
/* abis */ "armeabi-v7a;x86",
/* versionCode */ "12",
/* pattern */ "{abi}{minSDK:00}{screen}{foo:0}{versionCode:000}",
/* props */ "screen=24;foo=$(Foo)",
/* shouldBuild */ true,
/* expected */ "211241012;311241012",
},
new object[] {
/* seperateApk */ true,
/* abis */ "armeabi-v7a;x86",
/* versionCode */ "12",
/* pattern */ "{abi}{minSDK:00}{screen}{foo:00}{versionCode:000}",
/* props */ "screen=24;foo=$(Foo)",
/* shouldBuild */ false,
/* expected */ "2112401012;3112401012",
},
};

[Test]
[TestCaseSource("VersionCodeTestSource")]
public void VersionCodeTests (bool seperateApk, string abis, string versionCode, string versionCodePattern, string versionCodeProperties, bool shouldBuild, string expectedVersionCode)
{
var proj = new XamarinAndroidApplicationProject () {
IsRelease = true,
};
proj.SetProperty ("Foo", "1");
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidCreatePackagePerAbi, seperateApk);
if (!string.IsNullOrEmpty (abis))
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidSupportedAbis, abis);
if (!string.IsNullOrEmpty (versionCodePattern))
proj.SetProperty (proj.ReleaseProperties, "AndroidVersionCodePattern", versionCodePattern);
else
proj.RemoveProperty (proj.ReleaseProperties, "AndroidVersionCodePattern");
if (!string.IsNullOrEmpty (versionCodeProperties))
proj.SetProperty (proj.ReleaseProperties, "AndroidVersionCodeProperties", versionCodeProperties);
else
proj.RemoveProperty (proj.ReleaseProperties, "AndroidVersionCodeProperties");
proj.AndroidManifest = proj.AndroidManifest.Replace ("android:versionCode=\"1\"", $"android:versionCode=\"{versionCode}\"");
using (var builder = CreateApkBuilder (Path.Combine ("temp", "VersionCodeTests"), false, false)) {
builder.ThrowOnBuildFailure = false;
Assert.AreEqual (shouldBuild, builder.Build (proj), shouldBuild ? "Build should have succeeded." : "Build should have failed.");
if (!shouldBuild)
return;
var abiItems = seperateApk ? abis.Split (';') : new string[1];
var expectedItems = expectedVersionCode.Split (';');
XNamespace aNS = "http://schemas.android.com/apk/res/android";
Assert.AreEqual (abiItems.Length, expectedItems.Length, "abis parameter should have matching elements for expected");
for (int i = 0; i < abiItems.Length; i++) {
var path = seperateApk ? Path.Combine ("android", abiItems[i], "AndroidManifest.xml") : Path.Combine ("android", "manifest", "AndroidManifest.xml");
var manifest = builder.Output.GetIntermediaryAsText (Root, path);
var doc = XDocument.Parse (manifest);
var nsResolver = new XmlNamespaceManager (new NameTable ());
nsResolver.AddNamespace ("android", "http://schemas.android.com/apk/res/android");
var m = doc.XPathSelectElement ("/manifest") as XElement;
Assert.IsNotNull (m, "no manifest element found");
var vc = m.Attribute (aNS + "versionCode");
Assert.IsNotNull (vc, "no versionCode attribute found");
StringAssert.AreEqualIgnoringCase (expectedItems[i], vc.Value,
$"Version Code is incorrect. Found {vc.Value} expect {expectedItems[i]}");
}
}
}

[Test]
public void ManifestPlaceholders ()
{
Expand Down
Loading

0 comments on commit ed90962

Please sign in to comment.