From 546984883648f3a46d3c1c286033f58d776628f0 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 17 Apr 2025 08:39:20 -0500 Subject: [PATCH 1/2] [build] default `$(JavacSource/TargetVersion)` to 1.8 for Android code Fixes: https://github.com/dotnet/android/issues/9922 Fixes: https://github.com/dotnet/android/issues/9925 In 165cef7d, we started compiling with Java 17 by default. This causes warnings in #9922 such as: "/Users/builder/android-toolchain/jdk-21/bin/java" -classpath "/Users/builder/azdo/_work/11/s/xamarin-android/bin/Release/lib/packs/Microsoft.Android.Sdk.Darwin/35.99.0/tools/r8.jar" com.android.tools.r8.D8 --release --no-desugaring --output "obj/Release/release" "/Users/builder/azdo/_work/11/s/xamarin-android/bin/Release/lib/packs/Microsoft.Android.Sdk.Darwin/35.99.0/tools/java_runtime.jar" Warning in /Users/builder/azdo/_work/11/s/xamarin-android/bin/Release/lib/packs/Microsoft.Android.Sdk.Darwin/35.99.0/tools/java_runtime.jar:mono/MonoPackageManager.class at Lmono/MonoPackageManager;LoadApplication(Landroid/content/Context;)V: Invoke-customs are only supported starting with Android O (--min-api 26) This also happens if we go back to Java 9. We can get rid of the warning by desugaring the code to API 21: * https://github.com/dotnet/android/pull/10039 However, we think desugaring in itself could break something (especially for servicing). For now, we think the safest option is: * Target 1.8 for Java code targeting Android. * Desktop Java projects can still target 17. With these changes, tests like `BuildHasNoWarnings()` fail due to build warnings. Next: * When using the `-source` and `-target` switches in the `` MSBuild task, we can pass `-Xlint:-options` to `javac` to prevent warnings about these switches being deprecated. This change is suitable for servicing in .NET 9 and the `BuildHasNoWarnings()` test now passes. --- Configuration.props | 1 + src/Mono.Android/Mono.Android.targets | 4 ---- src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs | 4 ++++ .../Xamarin.Android.Common.props.in | 6 ++++-- src/java-runtime/java-runtime.csproj | 4 ++++ 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/Configuration.props b/Configuration.props index 70a463140b2..bb4d34767a2 100644 --- a/Configuration.props +++ b/Configuration.props @@ -132,6 +132,7 @@ $(_TestsProfiledAotName)$(_TestsAotName) + 17 17 diff --git a/src/Mono.Android/Mono.Android.targets b/src/Mono.Android/Mono.Android.targets index d825ccd4b54..fcac7c58bc4 100644 --- a/src/Mono.Android/Mono.Android.targets +++ b/src/Mono.Android/Mono.Android.targets @@ -162,10 +162,6 @@ $(DefineConstants);$([System.String]::Copy('$(_GeneratedDefineConstants)').Replace ('%24(DefineConstants);', '')) - - 1.8 - 1.6 - diff --git a/src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs b/src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs index eb17f14152a..5d752cf735c 100644 --- a/src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs +++ b/src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs @@ -70,6 +70,10 @@ protected override string GenerateCommandLineCommands () } else { cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion); cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion); + // Ignore warning when targeting older Java versions + // JAVAC : warning : [options] source value 8 is obsolete and will be removed in a future release + // JAVAC : warning : [options] target value 8 is obsolete and will be removed in a future release + cmd.AppendSwitchIfNotNull ("-Xlint:", "-options"); } return cmd.ToString (); diff --git a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in index 7ec64c69f5e..559599e9ec9 100644 --- a/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in +++ b/src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in @@ -26,8 +26,10 @@ @BUNDLETOOL_VERSION@ <_XamarinAndroidMSBuildDirectory>$(MSBuildThisFileDirectory) - 17 - 17 + + + 1.8 + 1.8 true diff --git a/src/java-runtime/java-runtime.csproj b/src/java-runtime/java-runtime.csproj index 2d65059d87f..d09fb57b735 100644 --- a/src/java-runtime/java-runtime.csproj +++ b/src/java-runtime/java-runtime.csproj @@ -3,6 +3,10 @@ netstandard2.0 Debug false + + + 1.8 + 1.8 From 0e105871f0f8a05d821c7edf4a56162c6fe118c4 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Thu, 17 Apr 2025 09:54:55 -0500 Subject: [PATCH 2/2] Update BuildTest.cs --- .../Tests/Xamarin.Android.Build.Tests/BuildTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs index 944333118ea..5d813aae938 100644 --- a/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs +++ b/src/Xamarin.Android.Build.Tasks/Tests/Xamarin.Android.Build.Tests/BuildTest.cs @@ -1604,7 +1604,7 @@ public void CheckJavaError () b.ThrowOnBuildFailure = false; Assert.IsFalse (b.Build (proj), "Build should have failed."); var ext = b.IsUnix ? "" : ".exe"; - var text = $"TestMe.java(1,8): javac{ext} error JAVAC0000: error: class, interface, enum, or record expected"; + var text = $"TestMe.java(1,8): javac{ext} error JAVAC0000: error: class, interface, or enum expected"; Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, text), "TestMe.java(1,8) expected"); text = $"TestMe2.java(1,41): javac{ext} error JAVAC0000: error: ';' expected"; Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, text), "TestMe2.java(1,41) expected");