Skip to content

Commit ace5169

Browse files
committed
[build] Support JDK-21 (#9672)
* [build] Support JDK-21 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. * Update `tools/workload-dependencies` to use `$(LatestSupportedJavaVersion)` property, instead of always using `$(JavaSdkVersion)+1`. * Address comments.
1 parent 32cf22e commit ace5169

File tree

6 files changed

+41
-5
lines changed

6 files changed

+41
-5
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.10.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
@@ -121,6 +121,7 @@ It is shared between "legacy" binding projects and .NET 7+ projects.
121121
Jars="@(_BindingJavaLibrariesToCompile);@(_ReferenceJavaLibs)"
122122
JavacTargetVersion="$(JavacTargetVersion)"
123123
JavacSourceVersion="$(JavacSourceVersion)"
124+
JdkVersion="$(_JdkVersion)"
124125
IntermediateOutputPath="$(IntermediateOutputPath)"
125126
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
126127
/>
@@ -168,6 +169,7 @@ It is shared between "legacy" binding projects and .NET 7+ projects.
168169
Jars="@(_JavaLibrariesToCompile);@(_ReferenceJavaLibs)"
169170
JavacTargetVersion="$(JavacTargetVersion)"
170171
JavacSourceVersion="$(JavacSourceVersion)"
172+
JdkVersion="$(_JdkVersion)"
171173
IntermediateOutputPath="$(IntermediateOutputPath)"
172174
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
173175
/>

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
<AndroidVersionCodePattern Condition=" '$(AndroidUseLegacyVersionCode)' != 'True' And '$(AndroidVersionCodePattern)' == '' ">{abi}{versionCode:D5}</AndroidVersionCodePattern>
2222
<AndroidResourceGeneratorTargetName>UpdateGeneratedFiles</AndroidResourceGeneratorTargetName>
2323
<AndroidUseApkSigner Condition=" '$(AndroidUseApkSigner)' == '' ">True</AndroidUseApkSigner>

tools/workload-dependencies/Program.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
var CmdlineToolsVersion = (string?) null;
1515
var BuildToolsVersion = (string?) null;
1616
var JdkVersion = (string?) null;
17+
var JdkMaxVersion = (string?) null;
1718
var NdkVersion = (string?) null;
1819
var PlatformToolsVersion = (string?) null;
1920
var PlatformVersion = (string?) null;
@@ -37,6 +38,9 @@
3738
{ "jdk-version=",
3839
"The JDK {VERSION} dotnet/android is built against.",
3940
v => JdkVersion = v },
41+
{ "jdk-max-version=",
42+
"The maximum JDK {VERSION} dotnet/android supports.",
43+
v => JdkMaxVersion = v },
4044
{ "ndk-version=",
4145
"The Android NDK {VERSION} dotnet/android is built against.",
4246
v => NdkVersion = v },
@@ -160,7 +164,7 @@ JProperty CreateJdkProperty (XDocument doc)
160164
{
161165
var v = new Version (JdkVersion ?? "17.0");
162166
var start = new Version (v.Major, v.Minor);
163-
var end = new Version (v.Major+1, 0);
167+
var end = GetMaxJdkVersion (v);
164168
var latestRevision = JdkVersion ?? GetLatestRevision (doc, "jdk");
165169
var contents = new JObject (
166170
new JProperty ("version", $"[{start},{end})"));
@@ -169,6 +173,17 @@ JProperty CreateJdkProperty (XDocument doc)
169173
return new JProperty ("jdk", contents);
170174
}
171175

176+
string GetMaxJdkVersion (Version v)
177+
{
178+
if (!string.IsNullOrEmpty (JdkMaxVersion)) {
179+
// JdkMaxVersion is `$(LatestSupportedJavaVersion)`, which is still a supported version!
180+
// Return the major version past JdkMaxVersion
181+
var x = new Version (JdkMaxVersion);
182+
return new Version (x.Major+1, 0).ToString ();
183+
}
184+
return new Version (v.Major+1, 0).ToString ();
185+
}
186+
172187
IEnumerable<XElement> GetSupportedElements (XDocument doc, string element)
173188
{
174189
if (doc.Root == null) {

tools/workload-dependencies/WorkloadDependencies.proj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
<_WorkloadDeps Include="--build-tools-version=$(AndroidSdkBuildToolsVersion)" />
6969
<_WorkloadDeps Include="--cmdline-tools-version=$(AndroidCommandLineToolsVersion)" />
7070
<_WorkloadDeps Include="--jdk-version=$(JavaSdkVersion)" />
71+
<_WorkloadDeps Include="--jdk-max-version=$(LatestSupportedJavaVersion)" />
7172
<_WorkloadDeps Include="--ndk-version=$(AndroidNdkVersion)" />
7273
<_WorkloadDeps Include="--platform-tools-version=$(AndroidSdkPlatformToolsVersion)" />
7374
<_WorkloadDeps Include="--platform-version=$(AndroidSdkPlatformVersion)" />

0 commit comments

Comments
 (0)