Skip to content

Commit a584a33

Browse files
committed
[build] Support JDK-21 (#9672)
Context: #9651 PR #9651 demonstrated that it was fairly straightforward to use JDK-21 on CI. We don't want to fully bump to JDK-21, because we still support building with JDK-17, so we *at minimum* need to run tests on both JDK-17 and JDK-21. As a "minimal introductory step", add support for JDK-21: * Update to Gradle 8.12, for harmony with dotnet/java-interop. * Update the `<Javac/>` task to use `javac --release N` when using JDK-17 and later. JDK-11 doesn't support `javac --release`. (Why care about JDK-11? Because .NET 8 still supports it, and this will make future cherry-picking easier.) * Set `$(LatestSupportedJavaVersion)`=21.0.99, which removes the XA0030 error which would result from using JDK-21.
1 parent a8cd27e commit a584a33

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ It is shared between "legacy" binding projects and .NET 7+ projects.
124124
Jars="@(_BindingJavaLibrariesToCompile);@(_ReferenceJavaLibs)"
125125
JavacTargetVersion="$(JavacTargetVersion)"
126126
JavacSourceVersion="$(JavacSourceVersion)"
127+
JdkVersion="$(_JdkVersion)"
127128
IntermediateOutputPath="$(IntermediateOutputPath)"
128129
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
129130
/>
@@ -171,6 +172,7 @@ It is shared between "legacy" binding projects and .NET 7+ projects.
171172
Jars="@(_JavaLibrariesToCompile);@(_InstantRunJavaReference);@(_ReferenceJavaLibs)"
172173
JavacTargetVersion="$(JavacTargetVersion)"
173174
JavacSourceVersion="$(JavacSourceVersion)"
175+
JdkVersion="$(_JdkVersion)"
174176
IntermediateOutputPath="$(IntermediateOutputPath)"
175177
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
176178
/>

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public class Javac : JavaCompileToolTask
2727
public string JavacTargetVersion { get; set; }
2828
public string JavacSourceVersion { get; set; }
2929

30+
public string JdkVersion { get; set; }
31+
3032
public override string DefaultErrorCode => "JAVAC0000";
3133

3234
public override bool RunTask ()
@@ -61,12 +63,28 @@ protected override string GenerateCommandLineCommands ()
6163
cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8");
6264

6365
cmd.AppendFileNameIfNotNull (string.Format ("@{0}", TemporarySourceListFile));
64-
cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion);
65-
cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion);
66+
67+
if (int.TryParse (JavacSourceVersion, out int sourceVersion) &&
68+
int.TryParse (JavacTargetVersion, out int targetVersion) &&
69+
JavacSupportsRelease ()) {
70+
cmd.AppendSwitchIfNotNull ("--release ", Math.Max (sourceVersion, targetVersion).ToString ());
71+
} else {
72+
cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion);
73+
cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion);
74+
}
6675

6776
return cmd.ToString ();
6877
}
6978

79+
bool JavacSupportsRelease ()
80+
{
81+
if (string.IsNullOrEmpty (JdkVersion)) {
82+
return false;
83+
}
84+
var jdkVersion = Version.Parse (JdkVersion);
85+
return jdkVersion.Major >= 17;
86+
}
87+
7088
protected override void WriteOptionsToResponseFile (StreamWriter sw)
7189
{
7290
sw.WriteLine ($"-d \"{ClassesOutputDirectory.Replace (@"\", @"\\")}\"");

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>
1818
<CopyNuGetImplementations Condition=" '$(CopyNuGetImplementations)' == ''">true</CopyNuGetImplementations>
1919
<YieldDuringToolExecution Condition="'$(YieldDuringToolExecution)' == ''">true</YieldDuringToolExecution>
20-
<LatestSupportedJavaVersion Condition="'$(LatestSupportedJavaVersion)' == ''">17.0.99</LatestSupportedJavaVersion>
20+
<LatestSupportedJavaVersion Condition="'$(LatestSupportedJavaVersion)' == ''">21.0.99</LatestSupportedJavaVersion>
2121
<MinimumSupportedJavaVersion Condition="'$(MinimumSupportedJavaVersion)' == ''">1.6.0</MinimumSupportedJavaVersion>
2222
<AndroidVersionCodePattern Condition=" '$(AndroidUseLegacyVersionCode)' != 'True' And '$(AndroidVersionCodePattern)' == '' ">{abi}{versionCode:D5}</AndroidVersionCodePattern>
2323
<AndroidResourceGeneratorTargetName>UpdateGeneratedFiles</AndroidResourceGeneratorTargetName>

0 commit comments

Comments
 (0)