Skip to content

Commit fbddc21

Browse files
Add min SDK version support to Aapt2Link and test
Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
1 parent dfd6855 commit fbddc21

2 files changed

Lines changed: 58 additions & 0 deletions

File tree

src/Xamarin.Android.Build.Tasks/Tasks/Aapt2Link.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,16 @@ string [] GenerateCommandLineCommands (string ManifestFile, string currentAbi, s
178178
cmd.Add ("-v");
179179
cmd.Add ($"--manifest");
180180
cmd.Add (GetFullPath (manifestFile));
181+
182+
//NOTE: if this is blank, we can omit --min-sdk-version in this call
183+
if (AndroidManifestFile is { ItemSpec.Length: > 0 }) {
184+
var doc = AndroidAppManifest.Load (AndroidManifestFile.ItemSpec, MonoAndroidHelper.SupportedVersions);
185+
if (doc.MinSdkVersion.HasValue) {
186+
cmd.Add ("--min-sdk-version");
187+
cmd.Add (doc.MinSdkVersion.Value.ToString ());
188+
}
189+
}
190+
181191
if (!string.IsNullOrEmpty (JavaDesignerOutputDirectory)) {
182192
var designerDirectory = Path.IsPathRooted (JavaDesignerOutputDirectory) ? JavaDesignerOutputDirectory : Path.Combine (WorkingDirectory, JavaDesignerOutputDirectory);
183193
Directory.CreateDirectory (designerDirectory);

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,5 +569,53 @@ public void CheckForInvalidVersionCode (string versionCode, string versionCodePa
569569
Assert.AreEqual (manifestFile, errors [0].File, $"Error File should have been {manifestFile}");
570570
Directory.Delete (Path.Combine (Root, path), recursive: true);
571571
}
572+
573+
[Test]
574+
public void Aapt2LinkPassesMinSdkVersion ()
575+
{
576+
var path = Path.Combine (Root, "temp", TestName);
577+
Directory.CreateDirectory (path);
578+
var resPath = Path.Combine (path, "res");
579+
Directory.CreateDirectory (resPath);
580+
var engine = new MockBuildEngine (TestContext.Out);
581+
var manifestFile = Path.Combine (path, "AndroidManifest.xml");
582+
File.WriteAllText (manifestFile, @"<manifest xmlns:android='http://schemas.android.com/apk/res/android' package='Foo.Foo'>
583+
<uses-sdk android:minSdkVersion='26' android:targetSdkVersion='31' />
584+
<application android:label='Test' />
585+
</manifest>");
586+
587+
CallAapt2Compile (engine, resPath, path, path);
588+
589+
int platform = AndroidSdkResolver.GetMaxInstalledPlatform ();
590+
var task = new Aapt2Link {
591+
BuildEngine = engine,
592+
ToolPath = GetPathToAapt2 (),
593+
ResourceDirectories = new ITaskItem [] { new TaskItem (resPath) },
594+
ManifestFiles = new ITaskItem [] { new TaskItem (manifestFile) },
595+
AndroidManifestFile = new TaskItem (manifestFile),
596+
CompiledResourceFlatArchive = new TaskItem (Path.Combine (path, "compiled.flata")),
597+
OutputFile = Path.Combine (path, "resources.apk"),
598+
AssemblyIdentityMapFile = Path.Combine (path, "foo.map"),
599+
JavaPlatformJarPath = Path.Combine (AndroidSdkPath, "platforms", $"android-{platform}", "android.jar"),
600+
JavaDesignerOutputDirectory = Path.Combine (path, "java")
601+
};
602+
603+
// Override GenerateCommandLineCommands to capture the command line
604+
var commandLine = (string[])typeof(Aapt2Link)
605+
.GetMethod("GenerateCommandLineCommands", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
606+
.Invoke(task, new object[] { manifestFile, null, Path.Combine (path, "resources.apk") });
607+
608+
// Verify that --min-sdk-version 26 is present in the command line
609+
bool foundMinSdkVersion = false;
610+
for (int i = 0; i < commandLine.Length - 1; i++) {
611+
if (commandLine[i] == "--min-sdk-version" && commandLine[i + 1] == "26") {
612+
foundMinSdkVersion = true;
613+
break;
614+
}
615+
}
616+
617+
Assert.IsTrue (foundMinSdkVersion, $"--min-sdk-version 26 should be present in aapt2 command line. Command: {string.Join(" ", commandLine)}");
618+
Directory.Delete (Path.Combine (Root, path), recursive: true);
619+
}
572620
}
573621
}

0 commit comments

Comments
 (0)