Skip to content

Commit

Permalink
[build] Support JDK-21 (#9672)
Browse files Browse the repository at this point in the history
* [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.
  • Loading branch information
jonpryor authored Jan 13, 2025
1 parent dff6de5 commit 14a6bfb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ It is shared between "legacy" binding projects and .NET 7+ projects.
Jars="@(_BindingJavaLibrariesToCompile);@(_ReferenceJavaLibs)"
JavacTargetVersion="$(JavacTargetVersion)"
JavacSourceVersion="$(JavacSourceVersion)"
JdkVersion="$(_JdkVersion)"
IntermediateOutputPath="$(IntermediateOutputPath)"
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
/>
Expand Down Expand Up @@ -168,6 +169,7 @@ It is shared between "legacy" binding projects and .NET 7+ projects.
Jars="@(_JavaLibrariesToCompile);@(_ReferenceJavaLibs)"
JavacTargetVersion="$(JavacTargetVersion)"
JavacSourceVersion="$(JavacSourceVersion)"
JdkVersion="$(_JdkVersion)"
IntermediateOutputPath="$(IntermediateOutputPath)"
AssemblyIdentityMapFile="$(_AndroidLibrayProjectAssemblyMapFile)"
/>
Expand Down
22 changes: 20 additions & 2 deletions src/Xamarin.Android.Build.Tasks/Tasks/Javac.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public class Javac : JavaCompileToolTask
public string JavacTargetVersion { get; set; }
public string JavacSourceVersion { get; set; }

public string JdkVersion { get; set; }

public override string DefaultErrorCode => "JAVAC0000";

public override bool RunTask ()
Expand Down Expand Up @@ -60,12 +62,28 @@ protected override string GenerateCommandLineCommands ()
cmd.AppendSwitchIfNotNull ("-J-Dfile.encoding=", "UTF8");

cmd.AppendFileNameIfNotNull (string.Format ("@{0}", TemporarySourceListFile));
cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion);
cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion);

if (int.TryParse (JavacSourceVersion, out int sourceVersion) &&
int.TryParse (JavacTargetVersion, out int targetVersion) &&
JavacSupportsRelease ()) {
cmd.AppendSwitchIfNotNull ("--release ", Math.Max (sourceVersion, targetVersion).ToString ());
} else {
cmd.AppendSwitchIfNotNull ("-target ", JavacTargetVersion);
cmd.AppendSwitchIfNotNull ("-source ", JavacSourceVersion);
}

return cmd.ToString ();
}

bool JavacSupportsRelease ()
{
if (string.IsNullOrEmpty (JdkVersion)) {
return false;
}
var jdkVersion = Version.Parse (JdkVersion);
return jdkVersion.Major >= 17;
}

protected override void WriteOptionsToResponseFile (StreamWriter sw)
{
var jars = new List<string> ();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<ImplicitlyExpandNETStandardFacades>false</ImplicitlyExpandNETStandardFacades>
<CopyNuGetImplementations Condition=" '$(CopyNuGetImplementations)' == ''">true</CopyNuGetImplementations>
<YieldDuringToolExecution Condition="'$(YieldDuringToolExecution)' == ''">true</YieldDuringToolExecution>
<LatestSupportedJavaVersion Condition="'$(LatestSupportedJavaVersion)' == ''">17.0.99</LatestSupportedJavaVersion>
<LatestSupportedJavaVersion Condition="'$(LatestSupportedJavaVersion)' == ''">21.0.99</LatestSupportedJavaVersion>
<AndroidVersionCodePattern Condition=" '$(AndroidUseLegacyVersionCode)' != 'True' And '$(AndroidVersionCodePattern)' == '' ">{abi}{versionCode:D5}</AndroidVersionCodePattern>
<AndroidResourceGeneratorTargetName>UpdateGeneratedFiles</AndroidResourceGeneratorTargetName>
<AndroidUseApkSigner Condition=" '$(AndroidUseApkSigner)' == '' ">True</AndroidUseApkSigner>
Expand Down
17 changes: 16 additions & 1 deletion tools/workload-dependencies/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
var CmdlineToolsVersion = (string?) null;
var BuildToolsVersion = (string?) null;
var JdkVersion = (string?) null;
var JdkMaxVersion = (string?) null;
var NdkVersion = (string?) null;
var PlatformToolsVersion = (string?) null;
var PlatformVersion = (string?) null;
Expand All @@ -37,6 +38,9 @@
{ "jdk-version=",
"The JDK {VERSION} dotnet/android is built against.",
v => JdkVersion = v },
{ "jdk-max-version=",
"The maximum JDK {VERSION} dotnet/android supports.",
v => JdkMaxVersion = v },
{ "ndk-version=",
"The Android NDK {VERSION} dotnet/android is built against.",
v => NdkVersion = v },
Expand Down Expand Up @@ -160,7 +164,7 @@ JProperty CreateJdkProperty (XDocument doc)
{
var v = new Version (JdkVersion ?? "17.0");
var start = new Version (v.Major, v.Minor);
var end = new Version (v.Major+1, 0);
var end = GetMaxJdkVersion (v);
var latestRevision = JdkVersion ?? GetLatestRevision (doc, "jdk");
var contents = new JObject (
new JProperty ("version", $"[{start},{end})"));
Expand All @@ -169,6 +173,17 @@ JProperty CreateJdkProperty (XDocument doc)
return new JProperty ("jdk", contents);
}

string GetMaxJdkVersion (Version v)
{
if (!string.IsNullOrEmpty (JdkMaxVersion)) {
// JdkMaxVersion is `$(LatestSupportedJavaVersion)`, which is still a supported version!
// Return the major version past JdkMaxVersion
var x = new Version (JdkMaxVersion);
return new Version (x.Major+1, 0).ToString ();
}
return new Version (v.Major+1, 0).ToString ();
}

IEnumerable<XElement> GetSupportedElements (XDocument doc, string element)
{
if (doc.Root == null) {
Expand Down
1 change: 1 addition & 0 deletions tools/workload-dependencies/WorkloadDependencies.proj
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
<_WorkloadDeps Include="--build-tools-version=$(AndroidSdkBuildToolsVersion)" />
<_WorkloadDeps Include="--cmdline-tools-version=$(AndroidCommandLineToolsVersion)" />
<_WorkloadDeps Include="--jdk-version=$(JavaSdkVersion)" />
<_WorkloadDeps Include="--jdk-max-version=$(LatestSupportedJavaVersion)" />
<_WorkloadDeps Include="--ndk-version=$(AndroidNdkVersion)" />
<_WorkloadDeps Include="--platform-tools-version=$(AndroidSdkPlatformToolsVersion)" />
<_WorkloadDeps Include="--platform-version=$(AndroidSdkPlatformVersion)" />
Expand Down

0 comments on commit 14a6bfb

Please sign in to comment.