Skip to content

Commit 165cef7

Browse files
[build] use javac -source 17 -target 17 (#9493)
Context: https://stackoverflow.com/a/76043133 Running `javac` with a newer `-source` and `-target` can run additional optimizations that results in *slightly* smaller Java bytecode and runtime performance. We should do this for all Java code we build as part of the product, as it might improve install size & build times for tools like `manifestmerger.jar` and `r8.jar`. After this change, I got the error: error: option -bootclasspath not allowed with target 17 Which is fixed by using `-classpath` instead.
1 parent efd14a4 commit 165cef7

File tree

8 files changed

+16
-13
lines changed

8 files changed

+16
-13
lines changed

Configuration.props

+2-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@
135135
<TestsFlavor>$(_TestsProfiledAotName)$(_TestsAotName)</TestsFlavor>
136136
</PropertyGroup>
137137
<PropertyGroup>
138-
<JavacSourceVersion>1.8</JavacSourceVersion>
139-
<JavacTargetVersion>1.8</JavacTargetVersion>
138+
<JavacSourceVersion>17</JavacSourceVersion>
139+
<JavacTargetVersion>17</JavacTargetVersion>
140140
</PropertyGroup>
141141
<PropertyGroup>
142142
<AndroidNdkFullPath>$([System.IO.Path]::GetFullPath ('$(AndroidNdkDirectory)'))</AndroidNdkFullPath>

build-tools/scripts/Jar.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<_Jar>"$(JarPath)"</_Jar>
3434
<_Targets>-source $(JavacSourceVersion) -target $(JavacTargetVersion)</_Targets>
3535
<_DestDir>$(IntermediateOutputPath)__CreateTestJarFile-bin</_DestDir>
36-
<_AndroidJar>-bootclasspath "$(AndroidSdkDirectory)\platforms\android-$(_AndroidApiLevelName)\android.jar"</_AndroidJar>
36+
<_AndroidJar>-classpath "$(AndroidSdkDirectory)\platforms\android-$(_AndroidApiLevelName)\android.jar"</_AndroidJar>
3737
<_CP>-cp "$(_JavaInteropJarPath)"</_CP>
3838
<_JavacFilesResponse>$(IntermediateOutputPath)__javac_response.txt</_JavacFilesResponse>
3939
</PropertyGroup>

build-tools/scripts/JavaCallableWrappers.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
<_MonoAndroidRuntimeJar>$(MicrosoftAndroidSdkOutDir)java_runtime.jar</_MonoAndroidRuntimeJar>
3838
</PropertyGroup>
3939
<Exec
40-
Command="&quot;$(JavaCPath)&quot; $(_Target) $(_D) -bootclasspath $(_AndroidJar)$(PathSeparator)&quot;$(_MonoAndroidJar)&quot;$(PathSeparator)&quot;$(_MonoAndroidRuntimeJar)&quot; @$(IntermediateOutputPath)jcw/classes.txt"
40+
Command="&quot;$(JavaCPath)&quot; $(_Target) $(_D) -classpath $(_AndroidJar)$(PathSeparator)&quot;$(_MonoAndroidJar)&quot;$(PathSeparator)&quot;$(_MonoAndroidRuntimeJar)&quot; @$(IntermediateOutputPath)jcw/classes.txt"
4141
/>
4242
<Exec
4343
Condition="Exists('$(_MonoAndroidJar)')"

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ string CreateResponseFile ()
103103

104104
if (BootClassPath != null && BootClassPath.Any ()) {
105105
var classpath = string.Join (Path.PathSeparator.ToString (), BootClassPath.Select (p => Path.GetFullPath (p.ItemSpec)));
106-
AppendArg (response, "--bootclasspath");
106+
AppendArg (response, "--classpath");
107107
AppendArg (response, classpath);
108108
}
109109

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ protected override string GenerateCommandLineCommands ()
5151
// Running command: C:\Program Files (x86)\Java\jdk1.6.0_20\bin\javac.exe
5252
// "-J-Dfile.encoding=UTF8"
5353
// "-d" "bin\classes"
54-
// "-classpath" "C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\android\bin\mono.android.jar"
55-
// "-bootclasspath" "C:\Program Files (x86)\Android\android-sdk-windows\platforms\android-8\android.jar"
54+
// "-classpath" "C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\AndroidMSBuildTest\AndroidMSBuildTest\obj\Debug\android\bin\mono.android.jar";"C:\Program Files (x86)\Android\android-sdk-windows\platforms\android-8\android.jar"
5655
// "-encoding" "UTF-8"
5756
// "@C:\Users\Jonathan\AppData\Local\Temp\tmp79c4ac38.tmp"
5857

@@ -69,9 +68,13 @@ protected override string GenerateCommandLineCommands ()
6968

7069
protected override void WriteOptionsToResponseFile (StreamWriter sw)
7170
{
71+
var jars = new List<string> ();
72+
if (Jars != null)
73+
jars.AddRange (Jars.Select (i => i.ItemSpec.Replace (@"\", @"\\")));
74+
jars.Add (JavaPlatformJarPath.Replace (@"\", @"\\"));
75+
7276
sw.WriteLine ($"-d \"{ClassesOutputDirectory.Replace (@"\", @"\\")}\"");
73-
sw.WriteLine ("-classpath \"{0}\"", Jars == null || !Jars.Any () ? null : string.Join (Path.PathSeparator.ToString (), Jars.Select (i => i.ItemSpec.Replace (@"\", @"\\"))));
74-
sw.WriteLine ("-bootclasspath \"{0}\"", JavaPlatformJarPath.Replace (@"\", @"\\"));
77+
sw.WriteLine ("-classpath \"{0}\"", string.Join (Path.PathSeparator.ToString (), jars));
7578
sw.WriteLine ($"-encoding UTF8");
7679
}
7780
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ public void CheckJavaError ()
15881588
b.ThrowOnBuildFailure = false;
15891589
Assert.IsFalse (b.Build (proj), "Build should have failed.");
15901590
var ext = b.IsUnix ? "" : ".exe";
1591-
var text = $"TestMe.java(1,8): javac{ext} error JAVAC0000: error: class, interface, or enum expected";
1591+
var text = $"TestMe.java(1,8): javac{ext} error JAVAC0000: error: class, interface, enum, or record expected";
15921592
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, text), "TestMe.java(1,8) expected");
15931593
text = $"TestMe2.java(1,41): javac{ext} error JAVAC0000: error: ';' expected";
15941594
Assert.IsTrue (StringAssertEx.ContainsText (b.LastBuildOutput, text), "TestMe2.java(1,41) expected");

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

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

30-
<JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' ">1.8</JavacSourceVersion>
31-
<JavacTargetVersion Condition=" '$(JavacTargetVersion)' == '' ">1.8</JavacTargetVersion>
30+
<JavacSourceVersion Condition=" '$(JavacSourceVersion)' == '' ">17</JavacSourceVersion>
31+
<JavacTargetVersion Condition=" '$(JavacTargetVersion)' == '' ">17</JavacTargetVersion>
3232

3333
<!-- Enable nuget package conflict resolution -->
3434
<ResolveAssemblyConflicts>true</ResolveAssemblyConflicts>

src/java-runtime/java-runtime.targets

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<_AndroidJar>"$(AndroidSdkDirectory)\platforms\android-$(AndroidJavaRuntimeApiLevel)\android.jar"</_AndroidJar>
5555
</PropertyGroup>
5656
<Exec
57-
Command="&quot;$(JavaCPath)&quot; $(_Target) -d %(_RuntimeOutput.IntermediateRuntimeOutputPath) -h %(_RuntimeOutput.IntermediateRuntimeOutputPath) -bootclasspath $(_AndroidJar)$(PathSeparator)&quot;%(_RuntimeOutput.OutputJar)&quot; @%(_RuntimeOutput.IntermediateRuntimeClassesTxt)"
57+
Command="&quot;$(JavaCPath)&quot; $(_Target) -d %(_RuntimeOutput.IntermediateRuntimeOutputPath) -h %(_RuntimeOutput.IntermediateRuntimeOutputPath) -classpath $(_AndroidJar)$(PathSeparator)&quot;%(_RuntimeOutput.OutputJar)&quot; @%(_RuntimeOutput.IntermediateRuntimeClassesTxt)"
5858
/>
5959
<Copy
6060
SourceFiles="$(IntermediateOutputPath)release/mono_android_Runtime.h"

0 commit comments

Comments
 (0)