Repository: bazelbuild/bazel
Issue: `android_local_test` isn't compatible with Android Platforms
Labels: type: bug
P1
team-Android

Forked from #18262.

The `android_local_test` rule is a strange beast which wants to do two things: it wants to [build an APK](https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/rules/android/AndroidLocalTestBase.java;drc=196515539edb352f10835f38aeea4d7efa832643;l=99), using a [valid Android SDK](https://cs.opensource.google/bazel/bazel/+/master:src/main/java/com/google/devtools/build/lib/bazel/rules/android/BazelAndroidLocalTestRule.java;drc=5efeaa5a8562f8517917c875c93a05c5fc6a72b2;l=104).

But, it also wants to run a Java binary on the local platform, with that APK as a data file. This worked before #18262, but now that we are being honest about Java versions, the cracks show up.

This sounds like a case for execution groups to me, but I also don't actually understand how this works under the hood.

For now, this means that we need to disable testing `android_local_test` with Android Platforms, and get the code cleaned up.

We probably also need to fix the Starlark version in bazelbuild/rules_android.

Issue comments:
Is this a blocker for flipping the flag? If one of the android rules would fail with it?
I've marked it as a blocker for Bazel 7 release (not for starting the release, but for finishing it), but we need someone from the rules_android team to investigate because the code is pretty confusing.
@bazel-io fork 7.0.0
Error log from `bazel test //src/test/shell/bazel/android:android_local_test_integration_test_with_platforms` (with the test re-enabled):
```
** test_android_local_test *****************************************************
$TEST_TMPDIR defined: output root default is '/usr/local/google/home/jcater/.cache/bazel/_bazel_jcater/a85a0dacd9e20e18c47928db0f1530e0/sandbox/linux-sandbox/1280/execroot/_main/_tmp/c0008185402992cea4ecb54c46211ee6' and max_idle_secs default is '15'.
WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files:
/etc/bazel.bazelrc
Extracting Bazel installation...
Starting local Bazel server and connecting to it...
INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes.
-- Test log: -----------------------------------------------------------
$TEST_TMPDIR defined: output root default is '/usr/local/google/home/jcater/.cache/bazel/_bazel_jcater/a85a0dacd9e20e18c47928db0f1530e0/sandbox/linux-sandbox/1280/execroot/_main/_tmp/c0008185402992cea4ecb54c46211ee6' and max_idle_secs default is '15'.
WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files:
/etc/bazel.bazelrc
Computing main repo mapping: 
WARNING: --enable_bzlmod is set, but no MODULE.bazel file was found at the workspace root. Bazel will create an empty MODULE.bazel file. Please consider migrating your external dependencies from WORKSPACE to MODULE.bazel. For more details, please refer to https://github.com/bazelbuild/bazel/issues/18958.
Loading: 
Loading: 0 packages loaded
Analyzing: target //javatests/com/bin:test (1 packages loaded, 0 targets configured)
Analyzing: target //javatests/com/bin:test (1 packages loaded, 0 targets configured)
[0 / 1] checking cached actions
Analyzing: target //javatests/com/bin:test (5 packages loaded, 6 targets configured)
[1 / 1] checking cached actions
Analyzing: target //javatests/com/bin:test (62 packages loaded, 12 targets configured)
[1 / 1] checking cached actions
ERROR: /usr/local/google/home/jcater/.cache/bazel/_bazel_jcater/a85a0dacd9e20e18c47928db0f1530e0/sandbox/linux-sandbox/1280/execroot/_main/_tmp/c0008185402992cea4ecb54c46211ee6/workspace.FJHlJe57/javatests/com/bin/BUILD:5:19: While resolving toolchains for target //javatests/com/bin:test (d3e1c6f): No matching toolchains found for types @bazel_tools//tools/jdk:runtime_toolchain_type.
To debug, rerun with --toolchain_resolution_debug='@bazel_tools//tools/jdk:runtime_toolchain_type'
If platforms or toolchains are a new concept for you, we'd encourage reading https://bazel.build/concepts/platforms-intro.
ERROR: Analysis of target '//javatests/com/bin:test' failed; build aborted
INFO: Elapsed time: 3.703s, Critical Path: 0.04s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: No test targets were found, yet testing was requested
------------------------------------------------------------------------
test_android_local_test FAILED: Tests for //javatests/com/bin:test failed.
/usr/local/google/home/jcater/.cache/bazel/_bazel_jcater/a85a0dacd9e20e18c47928db0f1530e0/sandbox/linux-sandbox/1280/execroot/_main/bazel-out/k8-fastbuild/bin/src/test/shell/bazel/android/android_local_test_integration_test_with_platforms.runfiles/_main/src/test/shell/bazel/android/android_local_test_integration_test_with_platforms:162: in call to test_android_local_test

Tear down:

INFO[android_local_test_integration_test_with_platforms 2023-11-06 21:16:31 (+0000)] Cleaning up workspace

FAILED: test_android_local_test
```

The problem (I think) is that the test sets `--platforms` to an Android platform (in android_helper.sh), but there's no JDK available for that platform.
Hullo! We're trying to get 7.0 blockers under control and this one seems to not have been investigated yet. Could we get an initial triage and an estimate on how long this'll take to fix?
@ahumesky was investigating, I'll ask him to update.
I have a change out for review that should address this

Pull request review comments:

Patch that resolved the issue:
diff --git a/tools/android/android_sdk_repository_template.bzl b/tools/android/android_sdk_repository_template.bzl
index 0da0416429b214..04c073000c784c 100644
--- a/tools/android/android_sdk_repository_template.bzl
+++ b/tools/android/android_sdk_repository_template.bzl
@@ -199,9 +199,6 @@ def create_android_sdk_rules(
         native.toolchain(
             name = "sdk-%d-toolchain" % api_level,
             exec_compatible_with = HOST_CONSTRAINTS,
-            target_compatible_with = [
-                "@platforms//os:android",
-            ],
             toolchain = ":sdk-%d" % api_level,
             toolchain_type = "@bazel_tools//tools/android:sdk_toolchain_type",
         )


Test patch:
diff --git a/src/test/shell/bazel/android/android_helper.sh b/src/test/shell/bazel/android/android_helper.sh
index a8b942d506dd58..2cb7864a4c2867 100755
--- a/src/test/shell/bazel/android/android_helper.sh
+++ b/src/test/shell/bazel/android/android_helper.sh
@@ -115,7 +115,7 @@ function resolve_android_toolchains() {
     echo "This test uses platform-based Android toolchain resolution."
     add_to_bazelrc "build --incompatible_enable_android_toolchain_resolution"
     add_to_bazelrc "build --incompatible_enable_cc_toolchain_resolution"
-    add_to_bazelrc "build --platforms=//test_android_platforms:simple"
+    add_to_bazelrc "build --android_platforms=//test_android_platforms:simple"
   else
     echo "This test uses legacy Android toolchains."
     add_to_bazelrc "build --noincompatible_enable_android_toolchain_resolution"
diff --git a/src/test/shell/bazel/android/android_local_test_integration_test.sh b/src/test/shell/bazel/android/android_local_test_integration_test.sh
index 3aa761ff11c791..0c5cf553450e01 100755
--- a/src/test/shell/bazel/android/android_local_test_integration_test.sh
+++ b/src/test/shell/bazel/android/android_local_test_integration_test.sh
@@ -34,11 +34,6 @@ fail_if_no_android_sdk
 source "${CURRENT_DIR}/../../integration_test_setup.sh" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
-if [[ "$1" = '--with_platforms' ]]; then
-  # TODO(https://github.com/bazelbuild/bazel/issues/19829): Re-enable when
-  # android_local_test works properly with Android platforms.
-  exit 0
-fi
 resolve_android_toolchains "$1"
 
 function setup_android_local_test_env() {