Skip to content

Commit eca0e84

Browse files
authored
Merge pull request #805 from jonpryor/jonp-cp-58029
[Xamarin.Android.Build.Tasks] AOT+LLVM needs minSdkVersion (#795)
2 parents c067436 + 16ddadb commit eca0e84

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public class Aot : AsyncTask
4444
[Required]
4545
public string SdkBinDirectory { get; set; }
4646

47+
[Required]
48+
public ITaskItem ManifestFile { get; set; }
49+
4750
[Required]
4851
public ITaskItem[] ResolvedAssemblies { get; set; }
4952

@@ -168,9 +171,22 @@ static bool ValidateAotConfiguration (TaskLoggingHelper log, AndroidTargetArch a
168171
return true;
169172
}
170173

171-
static int GetNdkApiLevel(string androidNdkPath, string androidApiLevel, AndroidTargetArch arch)
174+
int GetNdkApiLevel(string androidNdkPath, string androidApiLevel, AndroidTargetArch arch)
172175
{
173-
int level = int.Parse(androidApiLevel);
176+
var manifest = AndroidAppManifest.Load (ManifestFile.ItemSpec);
177+
178+
int level;
179+
if (manifest.MinSdkVersion.HasValue) {
180+
level = manifest.MinSdkVersion.Value;
181+
}
182+
else if (int.TryParse (androidApiLevel, out level)) {
183+
// level already set
184+
}
185+
else {
186+
// Probably not ideal!
187+
level = AndroidVersion.MaxApiLevel;
188+
}
189+
174190
// Some Android API levels do not exist on the NDK level. Workaround this my mapping them to the
175191
// most appropriate API level that does exist.
176192
if (level == 6 || level == 7) level = 5;

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.IO;
33
using System.Linq;
44
using System.Text;
5+
using System.Text.RegularExpressions;
56
using System.Xml.Linq;
67
using Microsoft.Build.Framework;
78
using NUnit.Framework;
@@ -209,12 +210,29 @@ public void BuildAotApplication (string supportedAbis, bool enableLLVM, bool exp
209210
proj.SetProperty (KnownProperties.TargetFrameworkVersion, "v5.1");
210211
proj.SetProperty (KnownProperties.AndroidSupportedAbis, supportedAbis);
211212
proj.SetProperty ("EnableLLVM", enableLLVM.ToString ());
213+
if (enableLLVM) {
214+
// Set //uses-sdk/@android:minSdkVersion so that LLVM uses the right libc.so
215+
proj.AndroidManifest = $@"<?xml version=""1.0"" encoding=""utf-8""?>
216+
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"" package=""{proj.PackageName}"">
217+
<uses-sdk android:minSdkVersion=""10"" />
218+
<application android:label=""{proj.ProjectName}"">
219+
</application>
220+
</manifest>";
221+
}
212222
using (var b = CreateApkBuilder (path)) {
213223
b.ThrowOnBuildFailure = false;
214224
b.Verbosity = LoggerVerbosity.Diagnostic;
215225
Assert.AreEqual (expectedResult, b.Build (proj), "Build should have {0}.", expectedResult ? "succeeded" : "failed");
216226
if (!expectedResult)
217227
return;
228+
if (enableLLVM) {
229+
// LLVM passes a direct path to libc.so, and we need to use the libc.so
230+
// which corresponds to the *minimum* SDK version specified in AndroidManifest.xml
231+
// Since we overrode minSdkVersion=10, that means we should use libc.so from android-9.
232+
var rightLibc = new Regex (@"^\s*\[AOT\].*cross-.*--llvm.*,ld-flags=.*android-9.arch-.*.usr.lib.libc\.so", RegexOptions.Multiline);
233+
var m = rightLibc.Match (b.LastBuildOutput);
234+
Assert.IsTrue (m.Success, "AOT+LLVM should use libc.so from minSdkVersion!");
235+
}
218236
foreach (var abi in supportedAbis.Split (new char [] { ';' })) {
219237
var libapp = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath,
220238
"bundles", abi, "libmonodroid_bundle_app.so");

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,7 @@ because xbuild doesn't support framework reference assemblies.
21452145
AndroidNdkDirectory="$(_AndroidNdkDirectory)"
21462146
AndroidApiLevel="$(_AndroidApiLevel)"
21472147
SdkBinDirectory="$(MonoAndroidBinDirectory)"
2148+
ManifestFile="$(IntermediateOutputPath)android\AndroidManifest.xml"
21482149
SupportedAbis="$(_BuildTargetAbis)"
21492150
AndroidSequencePointsMode="$(_SequencePointsMode)"
21502151
AotAdditionalArguments="$(AndroidAotAdditionalArguments)"

0 commit comments

Comments
 (0)