Skip to content

Commit 8140991

Browse files
[Xamarin.Android.Build.Tasks] require JDK 11 for API-31+ (#6084)
API-31 builds fail when using JDK 1.8 and using AndroidX with: /Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets(1728,3): error JAVAC0000: java.lang.AssertionError: annotationType() : unrecognized Attribute name MODULE (class com.sun.tools.javac.util.SharedNameTable$NameImpl) There are two plausible solutions to better handle this: 1. Only require JDK 11 when API-31 and AndroidX are being used, or 2. Always required JDK 11 for API-31+ As we believe *most* apps will be using AndroidX, there is little point to attempting so support (1); (2) it is! We must require JDK 11 for API-31+. There are two code paths here: one for "legacy" Xamarin.Android, and one for .NET 6. Legacy is straightforward, as we already had a pattern for this, we just update the `<ValidateJavaVersion/>` task. For .NET 6, I checked the `$(TargetPlatformVersion)` property for now and require JDK 11 in that case. When API-31 is stable, we can probably remove this code and just set this value by default: <MinimumSupportedJavaVersion Condition=" '$(MinimumSupportedJavaVersion)' == '' ">11.0</MinimumSupportedJavaVersion> For our unit tests -- most of which would be solution (1) (API-31 and *no* AndroidX use), but many of which started to fail because we went with solution (2) -- we did one of two things: 1. Use a different `$(TargetFrameworkVersion)` and/or remove `$(TargetFrameworkVersion)` entirely, or 2. Use a new `BuildTest.AssertTargetFrameworkVersionSupported()` method which `Assert.Ignore()`s the test if it would not pass otherwise.
1 parent 27e91bb commit 8140991

File tree

10 files changed

+54
-18
lines changed

10 files changed

+54
-18
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#### Application build and deployment
2+
3+
- [GitHub Pull Request #6084](https://github.com/xamarin/xamarin-android/pull/6084):
4+
Android API 31 and a `$(TargetFrameworkVersion)` of `v11.0.99`
5+
requires JDK 11. This can be obtained manually by installing
6+
[Microsoft OpenJDK 11][ms-openjdk].
7+
8+
[ms-openjdk]: https://docs.microsoft.com/java/openjdk/download

src/Xamarin.Android.Build.Tasks/Microsoft.Android.Sdk/targets/Microsoft.Android.Sdk.Tooling.targets

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ called for "legacy" projects in Xamarin.Android.Legacy.targets.
2323
JavaSdkPath="$(_JavaSdkDirectory)"
2424
JavaToolExe="$(JavaToolExe)"
2525
JavacToolExe="$(JavacToolExe)"
26+
TargetPlatformVersion="$(TargetPlatformVersion)"
2627
LatestSupportedJavaVersion="$(LatestSupportedJavaVersion)"
2728
MinimumSupportedJavaVersion="$(MinimumSupportedJavaVersion)">
2829
<Output TaskParameter="JdkVersion" PropertyName="_JdkVersion" />

src/Xamarin.Android.Build.Tasks/Tasks/Legacy/ValidateJavaVersion.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,8 @@ protected override bool ValidateJava (string javaExe, Regex versionRegex)
5050
Version GetJavaVersionForFramework ()
5151
{
5252
var apiLevel = MonoAndroidHelper.SupportedVersions.GetApiLevelFromFrameworkVersion (TargetFrameworkVersion);
53-
if (apiLevel >= 30) {
54-
// At present, it *looks like* API-R works with Build-tools r29, but
55-
// historically API-X requires Build-tools rX, so if/when API-30
56-
// requires Build-tools r30, it will require JDK11.
57-
// return new Version (11, 0);
58-
return new Version (1, 8);
59-
}
53+
if (apiLevel >= 31)
54+
return new Version (11, 0);
6055
if (apiLevel >= 24)
6156
return new Version (1, 8);
6257
else if (apiLevel == 23)

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class ValidateJavaVersion : AndroidTask
2121

2222
public string JavacToolExe { get; set; }
2323

24+
public string TargetPlatformVersion { get; set; }
25+
2426
[Required]
2527
public string LatestSupportedJavaVersion { get; set; }
2628

@@ -67,6 +69,9 @@ bool ValidateJava ()
6769
protected virtual bool ValidateJava (string javaExe, Regex versionRegex)
6870
{
6971
var required = Version.Parse (MinimumSupportedJavaVersion);
72+
if (Version.TryParse (TargetPlatformVersion, out var targetPlatformVersion) && targetPlatformVersion.Major >= 31) {
73+
required = new Version (11, 0);
74+
}
7075
MinimumRequiredJdkVersion = required.ToString ();
7176

7277
try {

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ public static string [] SupportedTargetFrameworks ()
4848
[Test]
4949
public void BuildBasicApplication ([ValueSource (nameof (SupportedTargetFrameworks))] string tfv, [Values (true, false)] bool isRelease)
5050
{
51+
AssertTargetFrameworkVersionSupported (tfv);
52+
5153
var proj = new XamarinAndroidApplicationProject {
5254
IsRelease = isRelease,
5355
TargetFrameworkVersion = tfv,
@@ -1143,8 +1145,6 @@ public void BuildMultiDexApplication ([Values ("dx", "d8")] string dexTool)
11431145
}
11441146

11451147
using (var b = CreateApkBuilder (Path.Combine ("temp", TestName), false, false)) {
1146-
proj.TargetFrameworkVersion = b.LatestTargetFrameworkVersion ();
1147-
11481148
string intermediateDir;
11491149
if (IsWindows && !Builder.UseDotNet) {
11501150
intermediateDir = Path.Combine (Root, b.ProjectDirectory, proj.IntermediateOutputPath, proj.TargetFrameworkAbbreviated);
@@ -2348,11 +2348,13 @@ public void CheckTargetFrameworkVersion ([Values (true, false)] bool isRelease)
23482348
proj.SetProperty ("AndroidUseLatestPlatformSdk", "False");
23492349
using (var builder = CreateApkBuilder ()) {
23502350
builder.GetTargetFrameworkVersionRange (out var _, out string firstFrameworkVersion, out var _, out string lastFrameworkVersion, out string[] _);
2351+
AssertTargetFrameworkVersionSupported (firstFrameworkVersion);
23512352
proj.SetProperty ("TargetFrameworkVersion", firstFrameworkVersion);
23522353
if (!Directory.Exists (Path.Combine (builder.FrameworkLibDirectory, firstFrameworkVersion)))
23532354
Assert.Ignore ("This is a Pull Request Build. Ignoring test.");
23542355
Assert.IsTrue (builder.Build (proj), "Build should have succeeded.");
23552356
Assert.IsTrue (StringAssertEx.ContainsText (builder.LastBuildOutput, $"Output Property: TargetFrameworkVersion={firstFrameworkVersion}"), $"TargetFrameworkVerson should be {firstFrameworkVersion}");
2357+
AssertTargetFrameworkVersionSupported (lastFrameworkVersion);
23562358
Assert.IsTrue (builder.Build (proj, parameters: new [] { $"TargetFrameworkVersion={lastFrameworkVersion}" }), "Build should have succeeded.");
23572359
Assert.IsTrue (StringAssertEx.ContainsText (builder.LastBuildOutput, $"Output Property: TargetFrameworkVersion={lastFrameworkVersion}"), $"TargetFrameworkVersion should be {lastFrameworkVersion}");
23582360
}

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,6 @@ string[] GetBuildProperties (LocalBuilder builder, bool manyBuild, bool dtbBuild
478478
var ret = new List <string> {
479479
"AndroidGenerateLayoutBindings=true"
480480
};
481-
if (!Builder.UseDotNet)
482-
ret.Add ("TargetFrameworkVersion=" + builder.LatestTargetFrameworkVersion ());
483481
if (manyBuild)
484482
ret.Add ("ForceParallelBuild=true");
485483

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

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ public class ResolveSdksTaskTests : BaseTest {
2121
new ApiInfo () { Id = "25", Level = 25, Name = "Nougat", FrameworkVersion = "v7.1", Stable = true },
2222
new ApiInfo () { Id = "26", Level = 26, Name = "Oreo", FrameworkVersion = "v8.0", Stable = true },
2323
new ApiInfo () { Id = "27", Level = 27, Name = "Oreo", FrameworkVersion = "v8.1", Stable = true },
24-
new ApiInfo () { Id = "P", Level = 28, Name = "P", FrameworkVersion = "v8.99", Stable = false },
24+
new ApiInfo () { Id = "28", Level = 28, Name = "Pie", FrameworkVersion = "v9.0", Stable = false },
25+
new ApiInfo () { Id = "29", Level = 29, Name = "Android10", FrameworkVersion = "v10.0", Stable = false },
26+
new ApiInfo () { Id = "30", Level = 30, Name = "Android11", FrameworkVersion = "v11.0", Stable = false },
27+
new ApiInfo () { Id = "S", Level = 31, Name = "S", FrameworkVersion = "v11.0.99", Stable = false },
2528
new ApiInfo () { Id = "Z", Level = 127, Name = "Z", FrameworkVersion = "v108.1.99", Stable = false },
2629
};
2730

@@ -35,9 +38,9 @@ public class ResolveSdksTaskTests : BaseTest {
3538
/* jdk */ "1.8.0",
3639
/* apis*/ apiInfoSelection,
3740
/* useLatestAndroidSdk */ true,
38-
/* targetFrameworkVersion */ "v8.99",
41+
/* targetFrameworkVersion */ "v9.0",
3942
/* expectedTaskResult */ true,
40-
/* expectedTargetFramework */ "v8.99",
43+
/* expectedTargetFramework */ "v9.0",
4144
/* expectedError */ "",
4245
/* expectedErrorMessage */ "",
4346
},
@@ -101,9 +104,9 @@ public class ResolveSdksTaskTests : BaseTest {
101104
/* jdk */ "1.8.0",
102105
/* apis*/ apiInfoSelection,
103106
/* useLatestAndroidSdk */ false,
104-
/* targetFrameworkVersion */ "v8.99",
107+
/* targetFrameworkVersion */ "v9.0",
105108
/* expectedTaskResult */ true,
106-
/* expectedTargetFramework */ "v8.99",
109+
/* expectedTargetFramework */ "v9.0",
107110
/* expectedError */ "",
108111
/* expectedErrorMessage */ "",
109112
},
@@ -151,6 +154,17 @@ public class ResolveSdksTaskTests : BaseTest {
151154
/* expectedError */ "XA0001",
152155
/* expectedErrorMessage */ "Unsupported or invalid $(TargetFrameworkVersion) value of 'v6.0'. Please update your Project Options.",
153156
},
157+
new object[] {
158+
/* buildtools */ "30.0.0",
159+
/* jdk */ "11.0",
160+
/* apis*/ apiInfoSelection,
161+
/* useLatestAndroidSdk */ false,
162+
/* targetFrameworkVersion */ "v11.0.99",
163+
/* expectedTaskResult */ true,
164+
/* expectedTargetFramework */ "v11.0.99",
165+
/* expectedError */ "",
166+
/* expectedErrorMessage */ "",
167+
},
154168
};
155169
#pragma warning restore 414
156170
[Test]
@@ -184,7 +198,7 @@ public void UseLatestAndroidSdk (string buildtools, string jdk, ApiInfo[] apis,
184198
JavaSdkPath = javaPath,
185199
JavaToolExe = javaExe,
186200
JavacToolExe = javacExe,
187-
LatestSupportedJavaVersion = "1.8.0",
201+
LatestSupportedJavaVersion = LatestSupportedJavaVersion,
188202
MinimumSupportedJavaVersion = "1.7.0",
189203
};
190204
var androidTooling = new ResolveAndroidTooling {
@@ -429,6 +443,7 @@ public void LatestTFV_OldTargetSdkVersion ()
429443
proj.TargetSdkVersion = "19";
430444
using (var b = CreateApkBuilder ()) {
431445
proj.TargetFrameworkVersion = b.LatestTargetFrameworkVersion ();
446+
AssertTargetFrameworkVersionSupported (proj.TargetFrameworkVersion);
432447
Assert.IsTrue (b.Build (proj), "Build should have succeeded.");
433448
}
434449
}

src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/Utilities/BaseTest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,16 @@ protected static void AssertLLVMSupported (bool llvm)
212212
}
213213
}
214214

215+
protected static void AssertTargetFrameworkVersionSupported (string targetFrameworkVersion)
216+
{
217+
if (Builder.UseDotNet)
218+
return; // N/A in .NET 6
219+
if (!Version.TryParse (targetFrameworkVersion.TrimStart ('v'), out var version) || version <= new Version (11, 0))
220+
return; // TFV is 11.0 or less
221+
if (!TestEnvironment.IsUsingJdk11)
222+
Assert.Ignore ("Test is only supported when using JDK 11.");
223+
}
224+
215225
protected static void WaitFor(int milliseconds)
216226
{
217227
var pause = new ManualResetEvent(false);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ public void XamarinLegacySdk ()
581581

582582
using var b = new Builder ();
583583
var dotnetTargetFramework = "net6.0-android30.0";
584-
var legacyTargetFrameworkVersion = b.LatestTargetFrameworkVersion ().TrimStart ('v');
584+
var legacyTargetFrameworkVersion = "11.0";
585585
var legacyTargetFramework = $"monoandroid{legacyTargetFrameworkVersion}";
586586
proj.SetProperty ("TargetFramework", value: "");
587587
proj.SetProperty ("TargetFrameworks", value: $"{dotnetTargetFramework};{legacyTargetFramework}");

tests/MSBuildDeviceIntegration/Tests/DebuggingTest.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ void SetTargetFrameworkAndManifest(XamarinAndroidApplicationProject proj, Builde
3232
proj.TargetFrameworkVersion = "v11.0";
3333
}
3434

35+
AssertTargetFrameworkVersionSupported (proj.TargetFrameworkVersion);
36+
3537
proj.AndroidManifest = $@"<?xml version=""1.0"" encoding=""utf-8""?>
3638
<manifest xmlns:android=""http://schemas.android.com/apk/res/android"" android:versionCode=""1"" android:versionName=""1.0"" package=""{proj.PackageName}"">
3739
<uses-sdk android:minSdkVersion=""24"" android:targetSdkVersion=""{apiLevel}"" />

0 commit comments

Comments
 (0)