Skip to content
This repository was archived by the owner on Jul 13, 2026. It is now read-only.

Commit a698a33

Browse files
authored
Merge pull request #227 from xamarin/dev/jestedfa/uses-sdk
Only conditionally include <uses-sdk /> in the AndroidManifest.xml wh…
2 parents ed102fc + 8b13954 commit a698a33

2 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎src/Xamarin.Android.Tools.AndroidSdk/AndroidAppManifest.cs‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usingSystem;
1+
usingSystem;
22
usingSystem.Linq;
33
usingSystem.Xml;
44
usingSystem.Collections.Generic;
@@ -45,7 +45,7 @@ public class AndroidAppManifest
4545
if(manifest.Element("uses-sdk")isXElementuses)
4646
usesSdk=uses;
4747
else
48-
manifest.Add(usesSdk=newXElement("uses-sdk"));
48+
usesSdk=newXElement("uses-sdk");
4949
}
5050

5151
publicstaticstringCanonicalizePackageName(stringpackageNameOrAssemblyName)
@@ -71,7 +71,6 @@ public static AndroidAppManifest Create (string packageName, string appLabel, An
7171
returnnewAndroidAppManifest(versions,XDocument.Parse(
7272
@"<?xml version=""1.0"" encoding=""utf-8""?>
7373
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"">
74-
<uses-sdk />
7574
<application android:label="""">
7675
</application>
7776
</manifest>")){
@@ -102,6 +101,14 @@ public static AndroidAppManifest Load (XDocument doc, AndroidVersions versions)
102101

103102
publicvoidWrite(XmlWriterwriter)
104103
{
104+
// Make sure that if the <uses-sdk /> XML element does not have any attributes (i.e. minSdkVersion
105+
// and targetSdkVersion), do NOT write it into the output. This is to avoid issues like
106+
// https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1874249/
107+
if(usesSdk.HasAttributes&&usesSdk.Parent==null)
108+
manifest.Add(usesSdk);
109+
elseif(!usesSdk.HasAttributes&&usesSdk.Parent!=null)
110+
usesSdk.Remove();
111+
105112
doc.Save(writer);
106113
}
107114

‎tests/Xamarin.Android.Tools.AndroidSdk-Tests/AndroidAppManifestTests.cs‎

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
usingSystem;
1+
usingSystem;
22
usingSystem.Collections.Generic;
33
usingSystem.Diagnostics;
44
usingSystem.IO;
@@ -172,5 +172,45 @@ public void CanGetAppTheme ()
172172

173173
Assert.AreEqual("@android:style/Theme.Material.Light",manifest.ApplicationTheme);
174174
}
175+
176+
[Test]
177+
publicvoidCanAddAndRemoveUsesSdk()
178+
{
179+
XNamespaceaNS="http://schemas.android.com/apk/res/android";
180+
varversions=newAndroidVersions(newAndroidVersion[0]);
181+
vardoc=XDocument.Parse(@"
182+
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"" package=""com.xamarin.Foo"">
183+
<uses-sdk android:minSdkVersion=""8"" android:targetSdkVersion=""12"" />
184+
<application android:label=""Foo"" android:icon=""@drawable/ic_icon"" android:theme=""@android:style/Theme.Material.Light"">
185+
</application>
186+
</manifest>");
187+
varmanifest=AndroidAppManifest.Load(doc,versions);
188+
189+
manifest.MinSdkVersion=null;
190+
manifest.TargetSdkVersion=null;
191+
192+
varsb=newStringBuilder();
193+
using(varwriter=XmlWriter.Create(sb)){
194+
manifest.Write(writer);
195+
}
196+
197+
varnewDoc=XDocument.Parse(sb.ToString());
198+
varusesSdk=newDoc.Element("manifest").Element("uses-sdk");
199+
Assert.IsNull(usesSdk,"uses-sdk should not exist");
200+
201+
manifest.MinSdkVersion=8;
202+
manifest.TargetSdkVersion=12;
203+
204+
sb=newStringBuilder();
205+
using(varwriter=XmlWriter.Create(sb)){
206+
manifest.Write(writer);
207+
}
208+
209+
newDoc=XDocument.Parse(sb.ToString());
210+
usesSdk=newDoc.Element("manifest").Element("uses-sdk");
211+
Assert.IsNotNull(usesSdk,"uses-sdk should exist");
212+
Assert.AreEqual("8",usesSdk.Attribute(aNS+"minSdkVersion").Value);
213+
Assert.AreEqual("12",usesSdk.Attribute(aNS+"targetSdkVersion").Value);
214+
}
175215
}
176216
}

0 commit comments

Comments
 (0)