Skip to content

Commit

Permalink
Merge pull request #227 from xamarin/dev/jestedfa/uses-sdk
Browse files Browse the repository at this point in the history
Only conditionally include <uses-sdk /> in the AndroidManifest.xml wh…
  • Loading branch information
jstedfast authored Feb 8, 2024
2 parents ed102fc + 8b13954 commit a698a33
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
13 changes: 10 additions & 3 deletions src/Xamarin.Android.Tools.AndroidSdk/AndroidAppManifest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Linq;
using System.Xml;
using System.Collections.Generic;
Expand Down Expand Up @@ -45,7 +45,7 @@ public class AndroidAppManifest
if (manifest.Element ("uses-sdk") is XElement uses)
usesSdk = uses;
else
manifest.Add (usesSdk = new XElement ("uses-sdk"));
usesSdk = new XElement ("uses-sdk");
}

public static string CanonicalizePackageName (string packageNameOrAssemblyName)
Expand All @@ -71,7 +71,6 @@ public static AndroidAppManifest Create (string packageName, string appLabel, An
return new AndroidAppManifest (versions, XDocument.Parse (
@"<?xml version=""1.0"" encoding=""utf-8""?>
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"">
<uses-sdk />
<application android:label="""">
</application>
</manifest>")) {
Expand Down Expand Up @@ -102,6 +101,14 @@ public static AndroidAppManifest Load (XDocument doc, AndroidVersions versions)

public void Write (XmlWriter writer)
{
// Make sure that if the <uses-sdk /> XML element does not have any attributes (i.e. minSdkVersion
// and targetSdkVersion), do NOT write it into the output. This is to avoid issues like
// https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1874249/
if (usesSdk.HasAttributes && usesSdk.Parent == null)
manifest.Add (usesSdk);
else if (!usesSdk.HasAttributes && usesSdk.Parent != null)
usesSdk.Remove ();

doc.Save (writer);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
Expand Down Expand Up @@ -172,5 +172,45 @@ public void CanGetAppTheme ()

Assert.AreEqual ("@android:style/Theme.Material.Light", manifest.ApplicationTheme);
}

[Test]
public void CanAddAndRemoveUsesSdk ()
{
XNamespace aNS = "http://schemas.android.com/apk/res/android";
var versions = new AndroidVersions (new AndroidVersion [0]);
var doc = XDocument.Parse (@"
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"" package=""com.xamarin.Foo"">
<uses-sdk android:minSdkVersion=""8"" android:targetSdkVersion=""12"" />
<application android:label=""Foo"" android:icon=""@drawable/ic_icon"" android:theme=""@android:style/Theme.Material.Light"">
</application>
</manifest>");
var manifest = AndroidAppManifest.Load (doc, versions);

manifest.MinSdkVersion = null;
manifest.TargetSdkVersion = null;

var sb = new StringBuilder ();
using (var writer = XmlWriter.Create (sb)) {
manifest.Write (writer);
}

var newDoc = XDocument.Parse (sb.ToString ());
var usesSdk = newDoc.Element ("manifest").Element ("uses-sdk");
Assert.IsNull (usesSdk, "uses-sdk should not exist");

manifest.MinSdkVersion = 8;
manifest.TargetSdkVersion = 12;

sb = new StringBuilder ();
using (var writer = XmlWriter.Create (sb)) {
manifest.Write (writer);
}

newDoc = XDocument.Parse (sb.ToString ());
usesSdk = newDoc.Element ("manifest").Element ("uses-sdk");
Assert.IsNotNull (usesSdk, "uses-sdk should exist");
Assert.AreEqual ("8", usesSdk.Attribute (aNS + "minSdkVersion").Value);
Assert.AreEqual ("12", usesSdk.Attribute (aNS + "targetSdkVersion").Value);
}
}
}

0 comments on commit a698a33

Please sign in to comment.