Skip to content

Commit 77c5b44

Browse files
[xabt] <Aapt2Link/> passes --min-sdk-version switch (#10194)
Fixes: #6739 The `Aapt2Link` task was not passing the `--min-sdk-version` parameter to aapt2, causing build failures when using adaptive icons in `Resources\mipmap-anydpi` folders without the `-v26` suffix, even when the app's minimum SDK version was correctly set to 26 or higher. Users reported getting this error: APT2000: <adaptive-icon> elements require a sdk version of at least 26. This occurred when: * App has `<SupportedOSPlatformVersion>26</SupportedOSPlatformVersion>` (or higher) * AndroidManifest.xml correctly shows `<uses-sdk android:minSdkVersion="26" .../>` * Adaptive icons are placed in `Resources\mipmap-anydpi` (without `-v26` suffix) The workaround was to manually add: <AndroidAapt2LinkExtraArgs>--min-sdk-version $(SupportedOSPlatformVersion)</AndroidAapt2LinkExtraArgs> We could not reproduce this any longer, perhaps fixed in newer versions of `aapt2`? However, it seems good to pass this switch along to `aapt2`, and added a new test for this scenario. Co-authored-by: Jonathan Peppers <[email protected]>
1 parent 189af22 commit 77c5b44

File tree

4 files changed

+44
-14
lines changed

4 files changed

+44
-14
lines changed

.github/copilot-instructions.md

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@
1919

2020
## Critical Rules
2121

22+
Reference official Android documentation where helpful:
23+
* [Android Developer Guide](https://developer.android.com/develop)
24+
* [Android API Reference](https://developer.android.com/reference)
25+
* [Android `aapt2` Documentation](https://developer.android.com/tools/aapt2)
26+
2227
**Only modify the main English `*.resx` files** (e.g., `Resources.resx`)
2328

2429
**Never modify non-English localization files:** `*.lcl` files in `Localize/loc/` or non-English `*.resx` files are auto-generated.
@@ -149,19 +154,6 @@ try {
149154
- **MSBuild Errors:** `XA####` (errors), `XA####` (warnings), `APT####` (Android tools)
150155
- **Logging:** Use `Log.LogError`, `Log.LogWarning` with error codes and context
151156

152-
## Commit Format
153-
```
154-
[Component] Summary
155-
```
156-
Components: `[Mono.Android]`, `[Build.Tasks]`, `build`, `ci`, `docs`, `tests`
157-
158-
```
159-
Bump to org/repo/branch@commit
160-
Bump to [Dependency Name] [Dependency Version]
161-
```
162-
163-
Required sections: **Changes**, **Fixes** (#issue-numbers), **Context**
164-
165157
## Troubleshooting
166158
- **Build:** Clean `bin/`+`obj/`, check Android SDK/NDK, `make clean`
167159
- **MSBuild:** Test in isolation, validate inputs

src/Xamarin.Android.Build.Tasks/MSBuild/Xamarin/Android/Xamarin.Android.Aapt2.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ Copyright (C) 2011-2012 Xamarin. All rights reserved.
166166
JavaPlatformJarPath="$(JavaPlatformJarPath)"
167167
JavaDesignerOutputDirectory="$(ResgenTemporaryDirectory)"
168168
CompiledResourceFlatFiles="@(_CompiledFlatFiles)"
169+
AndroidManifestFile="$(_AndroidManifestAbs)"
169170
ManifestFiles="$(ResgenTemporaryDirectory)\AndroidManifest.xml"
170171
AdditionalAndroidResourcePaths="@(_LibraryResourceDirectories)"
171172
YieldDuringToolExecution="$(YieldDuringToolExecution)"

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,15 @@ string [] GenerateCommandLineCommands (string ManifestFile, string? currentAbi,
321321
cmd.Add ("-o");
322322
cmd.Add (GetFullPath (currentResourceOutputFile));
323323

324+
// Add min SDK version from AndroidManifestFile if available
325+
if (AndroidManifestFile is { ItemSpec.Length: > 0 }) {
326+
var doc = AndroidAppManifest.Load (AndroidManifestFile.ItemSpec, MonoAndroidHelper.SupportedVersions);
327+
if (doc.MinSdkVersion.HasValue) {
328+
cmd.Add ("--min-sdk-version");
329+
cmd.Add (doc.MinSdkVersion.Value.ToString ());
330+
}
331+
}
332+
324333
return cmd.ToArray ();
325334
}
326335

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
namespace Xamarin.Android.Build.Tests {
1313
[TestFixture]
1414
[Parallelizable (ParallelScope.Self)]
15-
public class AndroidResourceTests : BaseTest {
15+
public class AndroidResourceTests : BaseTest
16+
{
1617
[Test]
1718
public void RTxtParserTest ()
1819
{
@@ -154,5 +155,32 @@ public void UserLayout ()
154155
Assert.True (mainText.Contains ("FixedWidth"), "'FixedWidth' was converted to 'fixedwidth'");
155156
Directory.Delete (path, recursive: true);
156157
}
158+
159+
[Test]
160+
public void AdaptiveIcon ()
161+
{
162+
var proj = new XamarinAndroidApplicationProject {
163+
SupportedOSPlatformVersion = "26",
164+
AndroidResources = {
165+
new AndroidItem.AndroidResource ("Resources\\mipmap-anydpi-v26\\adaptiveicon.xml") {
166+
TextContent = () => """
167+
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
168+
<background android:drawable="@mipmap/AdaptiveIcon_background" />
169+
<foreground android:drawable="@mipmap/AdaptiveIcon_foreground" />
170+
</adaptive-icon>
171+
""",
172+
},
173+
new AndroidItem.AndroidResource ("Resources\\mipmap-mdpi\\AdaptiveIcon_background.png") {
174+
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi,
175+
},
176+
new AndroidItem.AndroidResource ("Resources\\mipmap-mdpi\\AdaptiveIcon_foreground.png") {
177+
BinaryContent = () => XamarinAndroidCommonProject.icon_binary_mdpi,
178+
},
179+
}
180+
};
181+
182+
using var b = CreateApkBuilder ();
183+
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
184+
}
157185
}
158186
}

0 commit comments

Comments
 (0)