bazel icon indicating copy to clipboard operation
bazel copied to clipboard

android_library doesn't work with the Java 11 language level

Open cushon opened this issue 3 years ago • 1 comments

cc @ahumesky

Trying to use the Java 11 language level (--java_language_level=11) with android_library doesn't work.

Starting in Java 9, javac requires the --system flag to be set instead of the --bootclasspath to configure the system APIs. That includes APIs like java.lang.*, and also things like android.* for Android compilations.

The shared logic for Java compilations supports this using the BootClassPathInfo provider, which can represent the arguments to javac's --system flag. But this isn't wired up for android_library, which just configures a legacy --bootclasspath that gets ignored, resulting in errors like package android.app does not exist.

Repro

git diff HEAD
diff --git a/android/tutorial/WORKSPACE b/android/tutorial/WORKSPACE
new file mode 100644
index 0000000..9cc43bb
--- /dev/null
+++ b/android/tutorial/WORKSPACE
@@ -0,0 +1 @@
+android_sdk_repository(name = "androidsdk")
diff --git a/android/tutorial/src/main/java/com/example/bazel/BUILD b/android/tutorial/src/main/java/com/example/bazel/BUILD
new file mode 100644
index 0000000..76f8ba7
--- /dev/null
+++ b/android/tutorial/src/main/java/com/example/bazel/BUILD
@@ -0,0 +1,13 @@
+package(
+    default_visibility = ["//src:__subpackages__"],
+)
+
+android_library(
+    name = "greeter_activity",
+    srcs = [
+        "Greeter.java",
+        "MainActivity.java",
+    ],
+    manifest = "AndroidManifest.xml",
+    resource_files = glob(["res/**"]),
+)

With bazel built at 78c5e417ec981609687594eb70d8569aaa9e7f1c

$ bazel build --java_language_version=11 ...
INFO: Analyzed target //src/main/java/com/example/bazel:greeter_activity (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
ERROR: /tmp/tmp.EBbQNtnZOD/examples/android/tutorial/src/main/java/com/example/bazel/BUILD:5:16: Building src/main/java/com/example/bazel/libgreeter_activity.jar (2 source files) failed: (Exit 1): java failed: error executing command (from target //src/main/java/com/example/bazel:greeter_activity) external/remotejdk17_linux/bin/java '--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED' '--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED' ... (remaining 17 arguments skipped)
warning: [options] system modules path not set in conjunction with -source 11
src/main/java/com/example/bazel/MainActivity.java:3: error: package android.app does not exist
import android.app.Activity;
                  ^

cushon avatar Dec 12 '22 21:12 cushon

[see also b/237102742]

cushon avatar Dec 16 '22 17:12 cushon

Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 1+ years. It will be closed in the next 90 days unless any other activity occurs. If you think this issue is still relevant and should stay open, please post any comment here and the issue will no longer be marked as stale.

github-actions[bot] avatar Feb 20 '24 01:02 github-actions[bot]

This issue has been automatically closed due to inactivity. If you're still interested in pursuing this, please post @bazelbuild/triage in a comment here and we'll take a look. Thanks!

github-actions[bot] avatar May 20 '24 01:05 github-actions[bot]