Skip to content

Commit 1bdcf9f

Browse files
authored
[Xamarin.Android.Build.Tasks] Fix Android Version Code for Release builds (#7795)
* [Xamarin.Android.Build.Tasks] Fix Android Version Code for Release builds Fixes dotnet/maui#11139 Users trying to use `maui` and the new `ApplicationVersion` in conjunction with `AndroidCreatePackagePerAbi` find that the version is NOT used in the final set of apks. This is because when we use `AndroidCreatePackagePerAbi` we are totally ignoring the `ApplicationVersion` number. Instead we pick up the one from the `AndroidManifest.xml` `android:versionCode`. For maui users This is not obvious and is counter intuitive. So lets use the `ApplicationVersion` when using `AndroidCreatePackagePerAbi`. All the old code will remain in place, if the `ApplicationVersion` is not set we will still fall back to `android:versionCode` and if that is not set default to `1`.
1 parent 9113e2b commit 1bdcf9f

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/ManifestTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,46 @@ public void VersionCodeTests (bool seperateApk, string abis, string versionCode,
522522
}
523523
}
524524

525+
[Test]
526+
[TestCase ("1", false, "manifest=1")]
527+
[TestCase ("1", true, "x86_64=500001;arm64-v8a=400001")]
528+
[TestCase ("2", false, "manifest=2")]
529+
[TestCase ("2", true, "x86_64=500002;arm64-v8a=400002")]
530+
[TestCase ("999", false, "manifest=999")]
531+
[TestCase ("999", true, "x86_64=500999;arm64-v8a=400999")]
532+
public void ApplicationVersionTests (string applicationVersion, bool seperateApk, string expected)
533+
{
534+
var proj = new XamarinAndroidApplicationProject () {
535+
IsRelease = true,
536+
MinSdkVersion = null,
537+
};
538+
proj.SetProperty (proj.ReleaseProperties, "ApplicationVersion", applicationVersion);
539+
proj.SetProperty (proj.ReleaseProperties, "ApplicationDisplayVersion", applicationVersion);
540+
proj.SetProperty (proj.ReleaseProperties, KnownProperties.AndroidCreatePackagePerAbi, seperateApk);
541+
proj.AndroidManifest = proj.AndroidManifest
542+
.Replace ("android:versionCode=\"1\"", string.Empty)
543+
.Replace ("android:versionName=\"1.0\"", string.Empty);
544+
using (var builder = CreateApkBuilder ()) {
545+
Assert.True (builder.Build (proj), "Build should have succeeded.");
546+
XNamespace aNS = "http://schemas.android.com/apk/res/android";
547+
548+
var expectedItems = expected.Split (';');
549+
foreach (var item in expectedItems) {
550+
var items = item.Split ('=');
551+
var path = Path.Combine ("android", items [0], "AndroidManifest.xml");
552+
var manifest = builder.Output.GetIntermediaryAsText (Root, path);
553+
var doc = XDocument.Parse (manifest);
554+
var m = doc.XPathSelectElement ("/manifest") as XElement;
555+
Assert.IsNotNull (m, "no manifest element found");
556+
var vc = m.Attribute (aNS + "versionCode");
557+
Assert.IsNotNull (vc, "no versionCode attribute found");
558+
StringAssert.AreEqualIgnoringCase (items [1], vc.Value,
559+
$"Version Code is incorrect. Found {vc.Value} expect {items [1]}");
560+
}
561+
562+
}
563+
}
564+
525565
[Test]
526566
public void ManifestPlaceholders ([Values ("legacy", "manifestmerger.jar")] string manifestMerger)
527567
{

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
599599
<_AndroidPackage>$(ApplicationId)</_AndroidPackage>
600600
<_ApplicationLabel>$(ApplicationTitle)</_ApplicationLabel>
601601
<_AndroidVersionName>$(ApplicationDisplayVersion)</_AndroidVersionName>
602-
<_AndroidVersionCode Condition=" '$(AndroidCreatePackagePerAbi)' != 'true' ">$(ApplicationVersion)</_AndroidVersionCode>
602+
<_AndroidVersionCode>$(ApplicationVersion)</_AndroidVersionCode>
603603
</PropertyGroup>
604604
<AndroidError Code="XA1018"
605605
ResourceName="XA1018"

0 commit comments

Comments
 (0)