Skip to content

Commit

Permalink
[build] Support JDK-21 (#9672) (#9714)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jonpryor authored Jan 28, 2025
1 parent a8cd27e commit cdb777a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 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.1.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 @@ -124,6 +124,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 @@ -171,6 +172,7 @@ It is shared between "legacy" binding projects and .NET 7+ projects.
Jars="@(_JavaLibrariesToCompile);@(_InstantRunJavaReference);@(_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 @@ -61,12 +63,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)
{
sw.WriteLine ($"-d \"{ClassesOutputDirectory.Replace (@"\", @"\\")}\"");
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>
<MinimumSupportedJavaVersion Condition="'$(MinimumSupportedJavaVersion)' == ''">1.6.0</MinimumSupportedJavaVersion>
<AndroidVersionCodePattern Condition=" '$(AndroidUseLegacyVersionCode)' != 'True' And '$(AndroidVersionCodePattern)' == '' ">{abi}{versionCode:D5}</AndroidVersionCodePattern>
<AndroidResourceGeneratorTargetName>UpdateGeneratedFiles</AndroidResourceGeneratorTargetName>
Expand Down

0 comments on commit cdb777a

Please sign in to comment.