Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
}