Platform-enable shell-based Android tests.

PiperOrigin-RevId: 350438618
diff --git a/src/test/shell/bazel/android/aapt_integration_test.sh b/src/test/shell/bazel/android/aapt_integration_test.sh
index c67174c..99016c0 100755
--- a/src/test/shell/bazel/android/aapt_integration_test.sh
+++ b/src/test/shell/bazel/android/aapt_integration_test.sh
@@ -46,6 +46,10 @@
 # (bazelbuild/continuous-integration#578).
 add_to_bazelrc "build --incompatible_use_python_toolchains=false"
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 function test_build_with_aapt2() {
   create_new_workspace
   setup_android_sdk_support
diff --git a/src/test/shell/bazel/android/aar_integration_test.sh b/src/test/shell/bazel/android/aar_integration_test.sh
index 06fdcd1..ed1dd1b 100755
--- a/src/test/shell/bazel/android/aar_integration_test.sh
+++ b/src/test/shell/bazel/android/aar_integration_test.sh
@@ -33,6 +33,10 @@
 source "${CURRENT_DIR}/../../integration_test_setup.sh" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 # Regression test for https://github.com/bazelbuild/bazel/issues/1928.
 function test_empty_tree_artifact_action_inputs_mount_empty_directories() {
   create_new_workspace
@@ -114,6 +118,14 @@
   create_new_workspace
   setup_android_sdk_support
   setup_android_ndk_support
+
+  # TODO(b/161709111): enable platform-based toolchain resolution when
+  # --fat_apk_cpu fully supports it. Now it sets a split transition that clears
+  # out --platforms. The mapping in android_helper.sh re-enables a test Android
+  # platform for ARM but not x86. Enabling it for x86 requires an
+  # Android-compatible cc toolchain in tools/cpp/BUILD.tools.
+  add_to_bazelrc "build --noincompatible_enable_android_toolchain_resolution"
+
   # sample.aar contains native shared libraries for x86 and armeabi-v7a
   cp "${TEST_SRCDIR}/io_bazel/src/test/shell/bazel/android/sample.aar" .
   cat > AndroidManifest.xml <<EOF
diff --git a/src/test/shell/bazel/android/aidl_integration_test.sh b/src/test/shell/bazel/android/aidl_integration_test.sh
index e94c407..5170628 100755
--- a/src/test/shell/bazel/android/aidl_integration_test.sh
+++ b/src/test/shell/bazel/android/aidl_integration_test.sh
@@ -32,6 +32,10 @@
 source "$(rlocation io_bazel/src/test/shell/integration_test_setup.sh)" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 function test_simple_idl_srcs() {
   create_new_workspace
   setup_android_sdk_support
diff --git a/src/test/shell/bazel/android/android_helper.sh b/src/test/shell/bazel/android/android_helper.sh
index 30363e4..f9d19c4 100755
--- a/src/test/shell/bazel/android/android_helper.sh
+++ b/src/test/shell/bazel/android/android_helper.sh
@@ -109,4 +109,12 @@
   fi
 }
 
+# Resolves Android toolchains with platforms.
+function resolve_android_toolchains_with_platforms() {
+  echo "This test uses platform-based Android toolchain resolution."
+  add_to_bazelrc "build --incompatible_enable_android_toolchain_resolution"
+  add_to_bazelrc "build --platform_mappings=test_android_platforms/mappings"
+  add_to_bazelrc "build --platforms=//test_android_platforms:simple"
+}
+
 setup_head_android_tools_if_exists
diff --git a/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh b/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh
index 2239ad4..37da120 100755
--- a/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh
+++ b/src/test/shell/bazel/android/android_instrumentation_test_integration_test.sh
@@ -34,6 +34,10 @@
 # (bazelbuild/continuous-integration#578).
 add_to_bazelrc "build --incompatible_use_python_toolchains=false"
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 function setup_android_instrumentation_test_env() {
   mkdir -p java/com/bin/res/values
   mkdir -p javatests/com/bin
diff --git a/src/test/shell/bazel/android/android_integration_test.sh b/src/test/shell/bazel/android/android_integration_test.sh
index 92764e0..2ca0669 100755
--- a/src/test/shell/bazel/android/android_integration_test.sh
+++ b/src/test/shell/bazel/android/android_integration_test.sh
@@ -33,6 +33,10 @@
 source "${CURRENT_DIR}/../../integration_test_setup.sh" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 function test_sdk_library_deps() {
   create_new_workspace
   setup_android_sdk_support
diff --git a/src/test/shell/bazel/android/android_ndk_integration_test.sh b/src/test/shell/bazel/android/android_ndk_integration_test.sh
index 4095bc8..2d5d18d 100755
--- a/src/test/shell/bazel/android/android_ndk_integration_test.sh
+++ b/src/test/shell/bazel/android/android_ndk_integration_test.sh
@@ -38,6 +38,9 @@
 source "${CURRENT_DIR}/../../integration_test_setup.sh" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
 
 function create_android_binary() {
   mkdir -p java/bazel
@@ -202,6 +205,13 @@
   setup_android_ndk_support
   create_android_binary
 
+  # TODO(b/161709111): enable platform-based toolchain resolution when
+  # --fat_apk_cpu fully supports it. Now it sets a split transition that clears
+  # out --platforms. The mapping in android_helper.sh re-enables a test Android
+  # platform for ARM but not x86. Enabling it for x86 requires an
+  # Android-compatible cc toolchain in tools/cpp/BUILD.tools.
+  add_to_bazelrc "build --noincompatible_enable_android_toolchain_resolution"
+
   cpus="armeabi,armeabi-v7a,arm64-v8a,x86,x86_64"
 
   bazel build -s //java/bazel:bin --fat_apk_cpu="$cpus" || fail "build failed"
@@ -215,6 +225,13 @@
   setup_android_ndk_support
   create_android_binary
 
+  # TODO(b/161709111): enable platform-based toolchain resolution when
+  # --fat_apk_cpu fully supports it. Now it sets a split transition that clears
+  # out --platforms. The mapping in android_helper.sh re-enables a test Android
+  # platform for ARM but not x86. Enabling it for x86 requires an
+  # Android-compatible cc toolchain in tools/cpp/BUILD.tools.
+  add_to_bazelrc "build --noincompatible_enable_android_toolchain_resolution"
+
   cpus="armeabi,armeabi-v7a,arm64-v8a,x86,x86_64"
 
   bazel build -s //java/bazel:bin \
@@ -228,6 +245,7 @@
 # Regression test for https://github.com/bazelbuild/bazel/issues/2601.
 function test_clang_include_paths() {
   create_new_workspace
+  setup_android_sdk_support
   setup_android_ndk_support
   cat > BUILD <<EOF
 cc_binary(
@@ -250,6 +268,7 @@
 
 function test_android_ndk_repository_path_from_environment() {
   create_new_workspace
+  setup_android_sdk_support
   setup_android_ndk_support
   cat > WORKSPACE <<EOF
 android_ndk_repository(
@@ -291,6 +310,7 @@
 
 function test_stripped_cc_binary() {
   create_new_workspace
+  setup_android_sdk_support
   setup_android_ndk_support
   cat > BUILD <<EOF
 cc_binary(
@@ -310,6 +330,7 @@
 
 function test_crosstool_stlport() {
   create_new_workspace
+  setup_android_sdk_support
   setup_android_ndk_support
   cat > BUILD <<EOF
 cc_binary(
@@ -341,6 +362,7 @@
 
 function test_crosstool_libcpp() {
   create_new_workspace
+  setup_android_sdk_support
   setup_android_ndk_support
   cat > BUILD <<EOF
 cc_binary(
@@ -372,6 +394,7 @@
 
 function test_crosstool_gnu_libstdcpp() {
   create_new_workspace
+  setup_android_sdk_support
   setup_android_ndk_support
   cat > BUILD <<EOF
 cc_binary(
@@ -402,6 +425,7 @@
 
 function test_platforms_and_toolchains() {
   create_new_workspace
+  setup_android_sdk_support
   setup_android_ndk_support
   cat > BUILD <<EOF
 cc_binary(
@@ -445,6 +469,13 @@
   setup_android_ndk_support
   create_android_binary
 
+  # TODO(b/161709111): enable platform-based toolchain resolution when
+  # --fat_apk_cpu fully supports it. Now it sets a split transition that clears
+  # out --platforms. The mapping in android_helper.sh re-enables a test Android
+  # platform for ARM but not x86. Enabling it for x86 requires an
+  # Android-compatible cc toolchain in tools/cpp/BUILD.tools.
+  add_to_bazelrc "build --noincompatible_enable_android_toolchain_resolution"
+
   cpus="armeabi,armeabi-v7a,arm64-v8a,x86,x86_64"
 
   assert_build //java/bazel:bin \
diff --git a/src/test/shell/bazel/android/android_sh_test.bzl b/src/test/shell/bazel/android/android_sh_test.bzl
index 819df21..1822b97 100644
--- a/src/test/shell/bazel/android/android_sh_test.bzl
+++ b/src/test/shell/bazel/android/android_sh_test.bzl
@@ -39,6 +39,7 @@
     # Test with released android_tools version.
     native.sh_test(
         name = name,
+        args = ["--without_platforms"],
         data = data,
         **kwargs
     )
@@ -47,8 +48,17 @@
     # as the test itself.
     native.sh_test(
         name = name + "_with_head_android_tools",
+        args = ["--without_platforms"],
         data = data + [
             "//tools/android/runtime_deps:android_tools.tar.gz",
         ],
         **kwargs
     )
+
+    # Test with platform-based toolchain resolution.
+    native.sh_test(
+        name = name + "_with_platforms",
+        data = data,
+        args = ["--with_platforms"],
+        **kwargs
+    )
diff --git a/src/test/shell/bazel/android/desugarer_integration_test.sh b/src/test/shell/bazel/android/desugarer_integration_test.sh
index 2a9f5e1..598c35b 100755
--- a/src/test/shell/bazel/android/desugarer_integration_test.sh
+++ b/src/test/shell/bazel/android/desugarer_integration_test.sh
@@ -38,6 +38,10 @@
 source "$(rlocation io_bazel/src/test/shell/integration_test_setup.sh)" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 function create_java_8_android_binary() {
   mkdir -p java/bazel
   cat > java/bazel/BUILD <<EOF
diff --git a/src/test/shell/bazel/android/proguard_integration_test.sh b/src/test/shell/bazel/android/proguard_integration_test.sh
index 54a2cee..5a93d24 100755
--- a/src/test/shell/bazel/android/proguard_integration_test.sh
+++ b/src/test/shell/bazel/android/proguard_integration_test.sh
@@ -38,6 +38,10 @@
 source "$(rlocation io_bazel/src/test/shell/integration_test_setup.sh)" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 function test_proguard() {
   create_new_workspace
   setup_android_sdk_support
diff --git a/src/test/shell/bazel/android/resource_processing_integration_test.sh b/src/test/shell/bazel/android/resource_processing_integration_test.sh
index 50bf4bd..72aba16 100755
--- a/src/test/shell/bazel/android/resource_processing_integration_test.sh
+++ b/src/test/shell/bazel/android/resource_processing_integration_test.sh
@@ -38,6 +38,10 @@
 source "$(rlocation io_bazel/src/test/shell/integration_test_setup.sh)" \
   || { echo "integration_test_setup.sh not found!" >&2; exit 1; }
 
+if [[ "$1" = '--with_platforms' ]]; then
+  resolve_android_toolchains_with_platforms
+fi
+
 function setup_font_resources() {
   rm java/bazel/BUILD
 
diff --git a/src/test/shell/testenv.sh b/src/test/shell/testenv.sh
index a61de7b..4319af3 100755
--- a/src/test/shell/testenv.sh
+++ b/src/test/shell/testenv.sh
@@ -360,6 +360,35 @@
 )
 register_toolchains("//tools/android:all")
 EOF
+
+  setup_android_platforms
+}
+
+# Sets up sufficient platform definitions to support Android shell tests using
+# platform-based toolchain resolution.
+#
+# See resolve_android_toolchains_with_platforms in
+# src/test/shell/bazel/android/android_helper.sh for how to platform-based
+# toolchain resolution.
+function setup_android_platforms() {
+  mkdir -p test_android_platforms
+
+  cat > test_android_platforms/BUILD <<EOF
+platform(
+    name = "simple",
+    constraint_values = [
+        "@bazel_tools//platforms:android",
+        "@bazel_tools//platforms:arm",
+    ],
+)
+EOF
+
+  cat > test_android_platforms/mappings <<EOF
+platforms:
+flags:
+  --cpu=armeabi-v7a
+    //test_android_platforms:simple
+EOF
 }
 
 function setup_android_ndk_support() {