Skip to content

Commit e517d53

Browse files
[build] $(Javac{Source,Target}Version)=1.8 for JCWs (#10050)
Fixes: #9922 Fixes: #9925 Context: 165cef7 Context: #10039 In 165cef7, we started compiling Java code to target JDK-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 …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 target JDK-9. We can get rid of the warning by desugaring the code to API-21; see #10039. However, we think desugaring in itself could break something, especially for servicing. For now, we think the safest option is: * Target JDK 1.8 for Java code targeting Android. (This partially reverts 165cef7 and is consistent with .NET 9.) * Desktop Java projects continue targeting JDK-17. Additionally, update the `<Javac/>` task to *ignore* "options" warnings by using `javac -Xlint:-options`, which prevents warning messages such as the following: JAVAC : warning : [options] target value 8 is obsolete and will be removed in a future release JAVAC : warning : [options] To suppress warnings about obsolete options, use -Xlint:-options. These warnings appear when using JDK-21 to build Java source code while using `java -source 1.8 -target 1.8`; they do not appear when building Java source code with JDK-17 or earlier. With these changes, tests like `BuildHasNoWarnings()` fail due to build warnings. This change is suitable for servicing in .NET 9.
1 parent 893a981 commit e517d53

File tree

6 files changed

+14
-7
lines changed

6 files changed

+14
-7
lines changed

Configuration.props

+1
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
<TestsFlavor>$(_TestsProfiledAotName)$(_TestsAotName)</TestsFlavor>
133133
</PropertyGroup>
134134
<PropertyGroup>
135+
<!-- Default to Java 17 for desktop, projects targeting Android should use 1.8 -->
135136
<JavacSourceVersion>17</JavacSourceVersion>
136137
<JavacTargetVersion>17</JavacTargetVersion>
137138
</PropertyGroup>

src/Mono.Android/Mono.Android.targets

-4
Original file line numberDiff line numberDiff line change
@@ -162,10 +162,6 @@
162162
<DefineConstants>$(DefineConstants);$([System.String]::Copy('$(_GeneratedDefineConstants)').Replace ('%24(DefineConstants);', ''))</DefineConstants>
163163
</PropertyGroup>
164164
</Target>
165-
<PropertyGroup>
166-
<JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' And '$(AndroidApiLevel)' != '' And $(AndroidApiLevel) &gt; 23 ">1.8</JavacSourceVersion>
167-
<JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' ">1.6</JavacSourceVersion>
168-
</PropertyGroup>
169165
<ItemGroup>
170166
<JavaCallableWrapperSource Include="java\**\*.java" />
171167
</ItemGroup>

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

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ protected override string GenerateCommandLineCommands ()
7070
} else {
7171
cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion);
7272
cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion);
73+
// Ignore warning when targeting older Java versions
74+
// JAVAC : warning : [options] source value 8 is obsolete and will be removed in a future release
75+
// JAVAC : warning : [options] target value 8 is obsolete and will be removed in a future release
76+
cmd.AppendSwitchIfNotNull ("-Xlint:", "-options");
7377
}
7478

7579
return cmd.ToString ();

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ public void CheckJavaError ()
16041604
b.ThrowOnBuildFailure = false;
16051605
Assert.IsFalse (b.Build (proj), "Build should have failed.");
16061606
var ext = b.IsUnix ? "" : ".exe";
1607-
var text = $"TestMe.java(1,8): javac{ext} error JAVAC0000: error: class, interface, enum, or record expected";
1607+
var text = $"TestMe.java(1,8): javac{ext} error JAVAC0000: error: class, interface, or enum expected";
16081608
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, text), "TestMe.java(1,8) expected");
16091609
text = $"TestMe2.java(1,41): javac{ext} error JAVAC0000: error: ';' expected";
16101610
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, text), "TestMe2.java(1,41) expected");

src/Xamarin.Android.Build.Tasks/Xamarin.Android.Common.props.in

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
<BundleToolVersion Condition="'$(BundleToolVersion)' == ''">@BUNDLETOOL_VERSION@</BundleToolVersion>
2727
<_XamarinAndroidMSBuildDirectory>$(MSBuildThisFileDirectory)</_XamarinAndroidMSBuildDirectory>
2828

29-
<JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' ">17</JavacSourceVersion>
30-
<JavacTargetVersion Condition=" '$(JavacTargetVersion)' == '' ">17</JavacTargetVersion>
29+
<!-- NOTE: Compile Java code for Android against 1.8 -->
30+
<!-- Invoke-customs are only supported starting with Android O (-min-api 26) -->
31+
<JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' ">1.8</JavacSourceVersion>
32+
<JavacTargetVersion Condition=" '$(JavacTargetVersion)' == '' ">1.8</JavacTargetVersion>
3133

3234
<!-- Enable nuget package conflict resolution -->
3335
<ResolveAssemblyConflicts>true</ResolveAssemblyConflicts>

src/java-runtime/java-runtime.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
<TargetFramework>netstandard2.0</TargetFramework>
44
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
55
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
6+
<!-- NOTE: Compile Java code for Android against 1.8 -->
7+
<!-- Invoke-customs are only supported starting with Android O (-min-api 26) -->
8+
<JavacSourceVersion>1.8</JavacSourceVersion>
9+
<JavacTargetVersion>1.8</JavacTargetVersion>
610
</PropertyGroup>
711

812
<Import Project="..\..\Configuration.props" />

0 commit comments

Comments
 (0)