Merge sc-mainline-prod to master.

Change-Id: I0efc48448761331630e281b77b796a5f56bb8275
diff --git a/experiments/prepare_bazel_test_env/README.md b/experiments/prepare_bazel_test_env/README.md
index 896a715..7f0de8f 100644
--- a/experiments/prepare_bazel_test_env/README.md
+++ b/experiments/prepare_bazel_test_env/README.md
@@ -1,5 +1,263 @@
 # Overview
 
-The `prepare_bazel_test_env` script is a proof-of-concept script
-to create a simulated Bazel environment within the Android source
-tree using targets build by the Soong build system.
+The `prepare_bazel_test_env` script is a proof-of-concept script to create a
+simulated Bazel environment within the Android source tree using targets build
+by the Soong build system.
+
+# Supported Modules
+
+The script currently support generation of a Bazel environment to run the
+following Soong test modules:
+
+*   `//platform_testing/tests/example/native:hello_world_test`
+*   `//platform_testing/tests/example/jarhosttest: HelloWorldHostTest`
+*   `//platform_testing/tests/example/instrumentation: HelloWorldTests`
+
+Additionally, the system supports running the Tradefed console directly through
+the `//tools/tradefederation/core:tradefed` target.
+
+# Usage
+
+There are three actions that the script can perform, which are supplied as the
+first argument to the script: generate, sync, and clean, discussed below. All
+the commands below are written with the `-v` flag for verbose output, however
+this can be safely removed if desired.
+
+## Generate
+
+**Command Line**: `bazel run //build/pesto/experiments/prepare_bazel_test_env --
+-v generate`
+
+The generate command builds the required targets for a Bazel environment via
+Soong, then stages this environment and associated dependencies at
+`out/pesto-environment/` .
+
+The generate command performs the following actions:
+
+1.  Builds a set of modules (defined in the packaged templates in the
+    `data/templates` directory) via Soong
+2.  Creates a prebuilts directory at `out/pesto-environment/prebuilts` which
+    contains symlinks to the Android Build environment provided directories
+    (`ANDROID_HOST_OUT`, `ANDROID_HOST_OUT_TESTCASES`, `ANDROID_PRODUCT_OUT`,
+    `ANDROID_TARGET_OUT_TESTCASES`), and will later be linked to by a
+    `.soong_prebuilts` symlink that is placed adjacent to generated BUILD files.
+3.  Generates a Bazel environment at `out/pesto-environment/gen` using the
+    packaged environment to determine the locations of files, which are placed
+    in locations relative to the source tree root. For example, the
+    `out/pesto-environment/gen/tools/tradefederation/core/BUILD.bazel`
+    corresponds to a file that will eventually live at
+    `tools/tradefederation/core/BUILD.bazel`.
+4.  For each BUILD file that is staged, place a `.soong_prebuilts` symlink that
+    links to the aforementioned `prebuilts` directory.
+
+After generation, the environment create can serve as a standalone Bazel
+environment, or can be synced to the source tree using the sync command,
+discussed below.
+
+## Sync
+
+**Command Line**: `bazel run //build/pesto/experiments/prepare_bazel_test_env --
+-v sync`
+
+The sync command scans the staging directory at `out/pesto-environment/gen` for
+all files and then creates symlinks in the source tree that point at these
+files, additionally each synced BUILD file is provided local access to the
+`prebuilts` directory through a `.soong_prebuilts` symlink.
+
+The sync command performs the following actions:
+
+1.  Iterates through all files in the staged `out/pesto-environment/gen`
+    directory and create a symlink in the source tree to each file at the proper
+    location, overwriting file in the tree if it exists. For example, the sync
+    action would create a symlink at `packages/modules/adb/BUILD.bazel` that
+    links to `out/pesto-environment/gen/packages/modules/adb/BUILD.bazel`.
+2.  Create a `.soong_prebuilts` directory in every location in the source tree
+3.  where a BUILD file is placed, providing local access to the Soong staging
+4.  directories.
+
+After synchronization, the Bazel environment has been merged with the source
+tree and can be used directly from within the source tree. Additionally, after
+synchronization, subsequent calls to the generate command will propogate
+automatically to the source tree.
+
+## Clean
+
+**Command Line**: `bazel run //build/pesto/experiments/prepare_bazel_test_env --
+-v clean`
+
+The clean command removes all files that have been created in the tree, and also
+cleans up the environment directory at `out/pesto-environment` .
+
+The clean command performs the following actions:
+
+1.  For each file packaged with the script, remove the corresponding file from
+    the source tree.
+2.  For each BUILD file packaged with the script, remove the corresponding
+    `.soong_prebuilts` directory for the source tree.
+3.  Remove the `out/pesto-environment` directory.
+
+After clean, the environment should be removed from the tree. However, as some
+files may have been overwritten, certain repositories may need to be reset. The
+`build/bazel/rules/BUILD.bazel` file is a notable example that needs to be
+manually reset.
+
+# Adding New Modules.
+
+Adding support for an additional module depends on the type of module to be
+added. Each is discussed below. Of note, all files should be added in the
+`templates` directory and end in the `.template` file extension unless the file
+is a static, non-Bazel file.
+
+## Test Modules (without existing Test Rules)
+
+For targets needing a new test rule, if the test is a Tradefed run test, use the
+existing test rules as a template, otherwise a custom Bazel rule can be added.
+
+An example rule is provided at
+`templates/build/bazel/rules/cc_test.bzl.template` . For a new rule that
+leverages Tradefed, use the above rule as an example of how to package
+dependencies and test artifacts into the runfiles for the test. For each
+Tradefed rule, the required dependencies should be included as private
+attributes, for use by the rule implementation.
+
+```
+cc_test = rule(
+    _cc_test_impl,
+    attrs = {{
+        "_adb": attr.label(
+            default = Label("//packages/modules/adb"),
+            allow_single_file = True,
+        ),
+        "_tradefed_launcher": attr.label(
+            default = Label("//tools/tradefederation/core:atest_tradefed"),
+            allow_single_file = True,
+        ),
+        "_tradefed_script_help": attr.label(
+            default = Label("//tools/tradefederation/core:atest_script_help"),
+        ),
+        "_tradefed_jars": attr.label(
+            default = Label("//tools/tradefederation/core:tradefed_lib"),
+        ),
+        "_template": attr.label(
+            default = Label(
+                "//build/bazel/rules:tf_test_executable.sh.template",
+            ),
+            allow_single_file = True,
+        ),
+        "_launcher": attr.label(default = Label("//build/bazel/rules:cc_tf_test_launcher")),
+        "deps": attr.label_list(allow_files = True),
+    }},
+    executable = True,
+    test = True,
+)
+```
+
+Additionally, the Soong produced artifacts should also be included as runfiles
+so they can be seen during Tradefed execution. Including a target as runfiles
+here, ensures that the target shows up during execution. Furthermore, it ensures
+that Bazel knows when to rebuild/rerun a test when artifacts change.
+
+```
+runfiles = ctx.runfiles(
+        files = ctx.files._launcher,
+        transitive_files = depset(
+            transitive = [
+                depset(ctx.files.deps),
+                depset(ctx.files._adb),
+                depset(ctx.files._tradefed_launcher),
+                depset(ctx.files._tradefed_script_help),
+                depset(ctx.files._tradefed_jars),
+            ],
+        ),
+    )
+```
+
+Finally, the test rule should use the tf_test_executable.sh file as its
+executable and provide the proper substitutions to this file, which can be seen
+in the above example rule. The tf_test_executable.sh handles setting important
+variables needed by Tradefed before test execution in a Bazel environment.
+
+```
+ctx.actions.expand_template(
+    template = ctx.file._template,
+    output = script,
+    substitutions = {{
+        "{{module_name}}": ctx.label.name,
+        "{{module_path}}": ctx.label.package,
+        "{{tradefed_launcher_module_path}}": ctx.attr._tradefed_launcher.label.package,
+        "{{tradefed_jars_module_path}}": ctx.attr._tradefed_jars.label.package,
+        "{{path_additions}}": ctx.attr._adb.label.package,
+        "{{launcher_path}}": "{{}}/{{}}".format(
+            ctx.attr._launcher.label.package,
+            ctx.attr._launcher.label.name,
+        ),
+    }},
+    is_executable = True,
+)
+```
+
+After the rule logic is added, follow the steps in the below section for how to
+add a test target leveraging the newly added rule.
+
+## Test Modules (with existing Test Rules)
+
+For targets where the test rule is already provided (i.e. `cc_test` ), adding a
+new test module requires only adding a new BUILD file (with associated import
+logic).
+
+All added BUILD templates should end in `.template` to ensure Bazel does not see
+these files as part of a package, and should contain the required Soong targets,
+defined like the following:
+
+```
+# SOONG_TARGET:CtsAppTestCases
+# SOONG_TARGET:org.apache.http.legacy
+```
+
+Refer to the
+`data/templates/platform_testing/tests/example/native/BUILD.bazel.template` as
+an example of how to import files into the Bazel environment using a genrule.
+The genrule logic in the example template is used to combine files from multiple
+locations into a single target and strip the Soong paths from the files imported
+to Bazel. The below genrule serves as a foundation and copies all files from
+srcs to the files listed in outs.
+
+```
+genrule(name="hello_world_test_prebuilt",
+        srcs=_LIB_SRCS + _TESTCASE_HOST_SRCS + _TESTCASE_DEVICE_SRCS,
+        outs=_LIB_OUTS + _TESTCASE_HOST_OUTS + _TESTCASE_DEVICE_OUTS,
+        cmd="""
+          src_files=($(SRCS))
+          out_files=($(OUTS))
+          for i in "$${{!src_files[@]}}"
+          do
+            src_file=$${{src_files[$$i]}}
+            out_file=$${{out_files[$$i]}}
+            mkdir -p $$(dirname $$src_file)
+            cp $$src_file $$out_file
+          done
+          """)
+```
+
+When referring to files imported from Bazel, use the `{prebuilts_dir_name}`
+substitution variable instead of referring to the `.soong_prebuilts` directory
+directly since this may change.
+
+```
+_LIB_SRCS = glob([
+    "{prebuilts_dir_name}/host/lib/**/*",
+    "{prebuilts_dir_name}/host/lib64/**/*"
+])
+```
+
+Then, the newly imported module can be referenced from an existing test rule as
+a dependency, as is done in the example template. Ensure that the test rule is
+imported, such as in the example file:
+
+```
+load("//build/bazel/rules:cc_test.bzl", "cc_test")
+.
+.
+.
+cc_test(name="hello_world_test", deps=[":hello_world_test_prebuilt"])
+```
diff --git a/experiments/prepare_bazel_test_env/bazelenv.py b/experiments/prepare_bazel_test_env/bazelenv.py
index e42e767..c64242c 100644
--- a/experiments/prepare_bazel_test_env/bazelenv.py
+++ b/experiments/prepare_bazel_test_env/bazelenv.py
@@ -153,7 +153,7 @@
         super().sync()
 
     def clean(self):
-        super().sync()
+        super().clean()
 
     def __repr__(self):
         return (f"StaticResource(stage_path={self.stage_path}, "
diff --git a/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/android_tf_test_launcher.sh b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/android_tf_test_launcher.sh
new file mode 100644
index 0000000..4db2929
--- /dev/null
+++ b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/android_tf_test_launcher.sh
@@ -0,0 +1,37 @@
+#!/bin/bash
+#
+# Launches an Android instrumentation test with Tradefed.
+# The script expects the following:
+#   1. adb is present on the path.
+#   2. LD_LIBRARY_PATH is set for any libraries that need to be referenced.
+#   3. script_help.sh is included in the runfiles.
+#   4. TF_JAR_DIR and TF_PATH are set corresponding to the location of the
+#      Tradefed JARs and any associated libraries.
+#   5. The Tradefed launch script, test_module (i.e. hello_world_tests),
+#      and test_path (i.e. platform_testing/tests/example/native) are provided
+#      as the first and second positional arguments respectively.
+
+TRADEFED_LAUNCHER=$1
+shift
+TEST_MODULE=$1
+shift
+TEST_PATH=$1
+shift
+
+if [ "$1" == "--host" ]; then
+  HOST_ARGS=(-n --prioritize-host-config --skip-host-arch-check)
+  shift
+fi
+
+exec $TRADEFED_LAUNCHER template/atest_local_min \
+    --template:map test=atest \
+    --tests-dir "$TEST_PATH" \
+    --logcat-on-failure \
+    --no-enable-granular-attempts \
+    --no-early-device-release \
+    --include-filter "$TEST_MODULE" \
+    --skip-loading-config-jar \
+    --log-level-display VERBOSE \
+    --log-level VERBOSE \
+    "${HOST_ARGS[@]}" \
+    "$@"
\ No newline at end of file
diff --git a/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/cc_tf_test_launcher.sh b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/cc_tf_test_launcher.sh
index 2dc64ca..99b4ab5 100644
--- a/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/cc_tf_test_launcher.sh
+++ b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/cc_tf_test_launcher.sh
@@ -17,7 +17,7 @@
 TEST_PATH=$1
 shift
 
-if [ $1 != "" ] && [ $1 == "--host" ]; then
+if [ "$1" == "--host" ]; then
   HOST_ARGS=(-n --prioritize-host-config --skip-host-arch-check)
   shift
 fi
diff --git a/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/java_tf_test_launcher.sh b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/java_tf_test_launcher.sh
new file mode 100644
index 0000000..be6f076
--- /dev/null
+++ b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/java_tf_test_launcher.sh
@@ -0,0 +1,36 @@
+#!/bin/bash
+#
+# Launches a Java test with Tradefed. The script expects the following:
+#   1. adb is present on the path.
+#   2. LD_LIBRARY_PATH is set for any libraries that need to be referenced.
+#   3. script_help.sh is included in the runfiles.
+#   4. TF_JAR_DIR and TF_PATH are set corresponding to the location of the
+#      Tradefed JARs and any associated libraries.
+#   5. The Tradefed launch script, test_module (i.e. hello_world_tests),
+#      and test_path (i.e. platform_testing/tests/example/native) are provided
+#      as the first and second positional arguments respectively.
+
+TRADEFED_LAUNCHER=$1
+shift
+TEST_MODULE=$1
+shift
+TEST_PATH=$1
+shift
+
+if [ "$1" == "--host" ]; then
+  HOST_ARGS=(-n --prioritize-host-config --skip-host-arch-check)
+  shift
+fi
+
+exec $TRADEFED_LAUNCHER template/atest_local_min \
+    --template:map test=atest \
+    --tests-dir "$TEST_PATH" \
+    --logcat-on-failure \
+    --no-enable-granular-attempts \
+    --no-early-device-release \
+    --include-filter "$TEST_MODULE" \
+    --skip-loading-config-jar \
+    --log-level-display VERBOSE \
+    --log-level VERBOSE \
+    "${HOST_ARGS[@]}" \
+    "$@"
\ No newline at end of file
diff --git a/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/tf_test_executable.sh.template b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/tf_test_executable.sh.template
index f48e4fa..f9a46c2 100644
--- a/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/tf_test_executable.sh.template
+++ b/experiments/prepare_bazel_test_env/data/static/build/bazel/rules/tf_test_executable.sh.template
@@ -26,7 +26,7 @@
 fi
 
 # Set the required variables for the environment.
-export PATH={adb_path}:$PATH
+export PATH={path_additions}:$PATH
 export LD_LIBRARY_PATH="$TESTCASE_RELPATH/lib:$TESTCASE_RELPATH/lib64:"
 export TF_PATH="{tradefed_deps_module_path}/*"
 export TF_JAR_DIR="{tradefed_deps_module_path}/"
diff --git a/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/BUILD.bazel.template b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/BUILD.bazel.template
index 6d4ffe3..78de3b9 100644
--- a/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/BUILD.bazel.template
+++ b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/BUILD.bazel.template
@@ -18,4 +18,6 @@
 package(default_visibility=["//visibility:public"])
 exports_files(["lunch.bzl", "tf_test_executable.sh.template"])
 
-sh_binary(name="cc_tf_test_launcher", srcs=["cc_tf_test_launcher.sh"])
\ No newline at end of file
+sh_binary(name="android_tf_test_launcher", srcs=["android_tf_test_launcher.sh"])
+sh_binary(name="cc_tf_test_launcher", srcs=["cc_tf_test_launcher.sh"])
+sh_binary(name="java_tf_test_launcher", srcs=["java_tf_test_launcher.sh"])
\ No newline at end of file
diff --git a/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/android_test.bzl.template b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/android_test.bzl.template
new file mode 100644
index 0000000..bc529d4
--- /dev/null
+++ b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/android_test.bzl.template
@@ -0,0 +1,105 @@
+# Copyright {year}, The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# WARNING: This BUILD file was generated by a tool.
+# It should not be manually modified.
+"""android_test implementation for the Android environment."""
+
+
+def _android_test_impl(ctx):
+    # Emit the launcher script.
+    script = ctx.actions.declare_file("%s.sh" % ctx.label.name)
+
+    ctx.actions.expand_template(
+        template=ctx.file._template,
+        output=script,
+        substitutions={{
+            "{{module_name}}":
+            ctx.label.name,
+            "{{module_path}}":
+            ctx.label.package,
+            "{{tradefed_launcher_module_path}}":
+            ctx.attr._tradefed_launcher.label.package,
+            "{{tradefed_jars_module_path}}":
+            ctx.attr._tradefed_jars.label.package,
+            "{{path_additions}}":
+            ":".join([
+                ctx.attr._adb.label.package,
+                ctx.attr._aapt.label.package,
+            ]),
+            "{{launcher_path}}":
+            "{{}}/{{}}".format(
+                ctx.attr._launcher.label.package,
+                ctx.attr._launcher.label.name,
+            ),
+        }},
+        is_executable=True,
+    )
+
+    # Pass the deps on as runfiles as Tradefed will scan the resulting
+    # directory for tests.
+    runfiles = ctx.runfiles(
+        files=ctx.files._launcher,
+        transitive_files=depset(transitive=[
+            depset(ctx.files.deps),
+            depset(ctx.files._adb),
+            depset(ctx.files._aapt),
+            depset(ctx.files._tradefed_launcher),
+            depset(ctx.files._tradefed_script_help),
+            depset(ctx.files._tradefed_jars),
+        ], ),
+    )
+
+    return [DefaultInfo(executable=script, runfiles=runfiles)]
+
+
+android_test = rule(
+    _android_test_impl,
+    attrs={{
+        "_adb":
+        attr.label(
+            default=Label("//packages/modules/adb"),
+            allow_single_file=True,
+        ),
+        "_aapt":
+        attr.label(
+            default=Label("//frameworks/base/tools/aapt"),
+            allow_single_file=True,
+        ),
+        "_tradefed_launcher":
+        attr.label(
+            default=Label("//tools/tradefederation/core:atest_tradefed"),
+            allow_single_file=True,
+        ),
+        "_tradefed_script_help":
+        attr.label(
+            default=Label("//tools/tradefederation/core:atest_script_help"), ),
+        "_tradefed_jars":
+        attr.label(
+            default=Label("//tools/tradefederation/core:tradefed_lib"), ),
+        "_template":
+        attr.label(
+            default=Label(
+                "//build/bazel/rules:tf_test_executable.sh.template", ),
+            allow_single_file=True,
+        ),
+        "_launcher":
+        attr.label(
+            default=Label("//build/bazel/rules:android_tf_test_launcher")),
+        "deps":
+        attr.label_list(allow_files=True),
+    }},
+    executable=True,
+    test=True,
+)
diff --git a/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/cc_test.bzl.template b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/cc_test.bzl.template
index 588f881..a62aa8f 100644
--- a/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/cc_test.bzl.template
+++ b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/cc_test.bzl.template
@@ -27,7 +27,7 @@
             "{{module_path}}": ctx.label.package,
             "{{tradefed_launcher_module_path}}": ctx.attr._tradefed_launcher.label.package,
             "{{tradefed_jars_module_path}}": ctx.attr._tradefed_jars.label.package,
-            "{{adb_path}}": ctx.attr._adb.label.package,
+            "{{path_additions}}": ctx.attr._adb.label.package,
             "{{launcher_path}}": "{{}}/{{}}".format(
                 ctx.attr._launcher.label.package,
                 ctx.attr._launcher.label.name,
@@ -65,7 +65,7 @@
             allow_single_file = True,
         ),
         "_tradefed_script_help": attr.label(
-            default = Label("//tools/tradefederation/core:script_help"),
+            default = Label("//tools/tradefederation/core:atest_script_help"),
         ),
         "_tradefed_jars": attr.label(
             default = Label("//tools/tradefederation/core:tradefed_lib"),
diff --git a/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/java_test.bzl.template b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/java_test.bzl.template
new file mode 100644
index 0000000..0f0f386
--- /dev/null
+++ b/experiments/prepare_bazel_test_env/data/templates/build/bazel/rules/java_test.bzl.template
@@ -0,0 +1,94 @@
+# Copyright {year}, The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# WARNING: This BUILD file was generated by a tool.
+# It should not be manually modified.
+"""java_test implementation for the Android environment."""
+
+
+def _java_test_impl(ctx):
+    # Emit the launcher script.
+    script = ctx.actions.declare_file("%s.sh" % ctx.label.name)
+    ctx.actions.expand_template(
+        template=ctx.file._template,
+        output=script,
+        substitutions={{
+            "{{module_name}}":
+            ctx.label.name,
+            "{{module_path}}":
+            ctx.label.package,
+            "{{tradefed_launcher_module_path}}":
+            ctx.attr._tradefed_launcher.label.package,
+            "{{tradefed_jars_module_path}}":
+            ctx.attr._tradefed_jars.label.package,
+            "{{path_additions}}":
+            ctx.attr._adb.label.package,
+            "{{launcher_path}}":
+            "{{}}/{{}}".format(
+                ctx.attr._launcher.label.package,
+                ctx.attr._launcher.label.name,
+            ),
+        }},
+        is_executable=True,
+    )
+
+    # Pass the deps on as runfiles as Tradefed will scan the resulting
+    # directory for tests.
+    runfiles = ctx.runfiles(
+        files=ctx.files._launcher,
+        transitive_files=depset(transitive=[
+            depset(ctx.files.deps),
+            depset(ctx.files._adb),
+            depset(ctx.files._tradefed_launcher),
+            depset(ctx.files._tradefed_script_help),
+            depset(ctx.files._tradefed_jars),
+        ], ),
+    )
+
+    return [DefaultInfo(executable=script, runfiles=runfiles)]
+
+
+java_test = rule(
+    _java_test_impl,
+    attrs={{
+        "_adb":
+        attr.label(
+            default=Label("//packages/modules/adb"),
+            allow_single_file=True,
+        ),
+        "_tradefed_launcher":
+        attr.label(
+            default=Label("//tools/tradefederation/core:atest_tradefed"),
+            allow_single_file=True,
+        ),
+        "_tradefed_script_help":
+        attr.label(
+            default=Label("//tools/tradefederation/core:atest_script_help"), ),
+        "_tradefed_jars":
+        attr.label(
+            default=Label("//tools/tradefederation/core:tradefed_lib"), ),
+        "_template":
+        attr.label(
+            default=Label(
+                "//build/bazel/rules:tf_test_executable.sh.template", ),
+            allow_single_file=True,
+        ),
+        "_launcher":
+        attr.label(default=Label("//build/bazel/rules:java_tf_test_launcher")),
+        "deps":
+        attr.label_list(allow_files=True),
+    }},
+    executable=True,
+    test=True,
+)
diff --git a/experiments/prepare_bazel_test_env/data/templates/frameworks/base/tools/aapt/BUILD.bazel.template b/experiments/prepare_bazel_test_env/data/templates/frameworks/base/tools/aapt/BUILD.bazel.template
new file mode 100644
index 0000000..70a14fa
--- /dev/null
+++ b/experiments/prepare_bazel_test_env/data/templates/frameworks/base/tools/aapt/BUILD.bazel.template
@@ -0,0 +1,27 @@
+# Copyright {year}, The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# WARNING: This BUILD file was generated by a tool.
+# It should not be manually modified.
+
+# SOONG_TARGET:aapt
+
+package(default_visibility=["//visibility:public"])
+
+genrule(name="aapt",
+        srcs=["{prebuilts_dir_name}/host/bin/aapt"],
+        outs=[
+            "aapt",
+        ],
+        cmd="cp -t $(RULEDIR) $<")
diff --git a/experiments/prepare_bazel_test_env/data/templates/platform_testing/tests/example/instrumentation/BUILD.bazel.template b/experiments/prepare_bazel_test_env/data/templates/platform_testing/tests/example/instrumentation/BUILD.bazel.template
new file mode 100644
index 0000000..f5d0064
--- /dev/null
+++ b/experiments/prepare_bazel_test_env/data/templates/platform_testing/tests/example/instrumentation/BUILD.bazel.template
@@ -0,0 +1,51 @@
+# Copyright {year}, The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# WARNING: This BUILD file was generated by a tool.
+# It should not be manually modified.
+
+# SOONG_TARGET:HelloWorldTests
+
+package(default_visibility=["//visibility:public"])
+
+load("//build/bazel/rules:android_test.bzl", "android_test")
+
+_LIB_SRCS = glob([
+    "{prebuilts_dir_name}/host/lib/**/*",
+    "{prebuilts_dir_name}/host/lib64/**/*"
+])
+_TESTCASE_DEVICE_SRCS = glob(["{prebuilts_dir_name}/target_testcases/HelloWorldTests/**/*"])
+
+_LIB_OUTS = [f.replace("{prebuilts_dir_name}/host/", "") for f in _LIB_SRCS]
+_TESTCASE_DEVICE_OUTS = [
+  f.replace("{prebuilts_dir_name}/target_testcases/HelloWorldTests/", "device/")
+  for f in _TESTCASE_DEVICE_SRCS
+]
+
+genrule(name="HelloWorldTests_prebuilt",
+        srcs=_LIB_SRCS + _TESTCASE_DEVICE_SRCS,
+        outs=_LIB_OUTS + _TESTCASE_DEVICE_OUTS,
+        cmd="""
+          src_files=($(SRCS))
+          out_files=($(OUTS))
+          for i in "$${{!src_files[@]}}"
+          do
+            src_file=$${{src_files[$$i]}}
+            out_file=$${{out_files[$$i]}}
+            mkdir -p $$(dirname $$src_file)
+            cp $$src_file $$out_file
+          done
+          """)
+
+android_test(name="HelloWorldTests", deps=[":HelloWorldTests_prebuilt"])
\ No newline at end of file
diff --git a/experiments/prepare_bazel_test_env/data/templates/platform_testing/tests/example/jarhosttest/BUILD.bazel.template b/experiments/prepare_bazel_test_env/data/templates/platform_testing/tests/example/jarhosttest/BUILD.bazel.template
new file mode 100644
index 0000000..c27e0d5
--- /dev/null
+++ b/experiments/prepare_bazel_test_env/data/templates/platform_testing/tests/example/jarhosttest/BUILD.bazel.template
@@ -0,0 +1,51 @@
+# Copyright {year}, The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# WARNING: This BUILD file was generated by a tool.
+# It should not be manually modified.
+
+# SOONG_TARGET:HelloWorldHostTest
+
+package(default_visibility=["//visibility:public"])
+
+load("//build/bazel/rules:java_test.bzl", "java_test")
+
+_LIB_SRCS = glob([
+    "{prebuilts_dir_name}/host/lib/**/*",
+    "{prebuilts_dir_name}/host/lib64/**/*"
+])
+_TESTCASE_HOST_SRCS = glob(["{prebuilts_dir_name}/host_testcases/HelloWorldHostTest/**/*"])
+
+_LIB_OUTS = [f.replace("{prebuilts_dir_name}/host/", "") for f in _LIB_SRCS]
+_TESTCASE_HOST_OUTS = [
+  f.replace("{prebuilts_dir_name}/host_testcases/HelloWorldHostTest/", "host/")
+  for f in _TESTCASE_HOST_SRCS
+]
+
+genrule(name="HelloWorldHostTest_prebuilt",
+        srcs=_LIB_SRCS + _TESTCASE_HOST_SRCS,
+        outs=_LIB_OUTS + _TESTCASE_HOST_OUTS,
+        cmd="""
+          src_files=($(SRCS))
+          out_files=($(OUTS))
+          for i in "$${{!src_files[@]}}"
+          do
+            src_file=$${{src_files[$$i]}}
+            out_file=$${{out_files[$$i]}}
+            mkdir -p $$(dirname $$src_file)
+            cp $$src_file $$out_file
+          done
+          """)
+
+java_test(name="HelloWorldHostTest", deps=[":HelloWorldHostTest_prebuilt"])
\ No newline at end of file
diff --git a/experiments/prepare_bazel_test_env/data/templates/tools/tradefederation/core/BUILD.bazel.template b/experiments/prepare_bazel_test_env/data/templates/tools/tradefederation/core/BUILD.bazel.template
index 3493ba6..2158fd4 100644
--- a/experiments/prepare_bazel_test_env/data/templates/tools/tradefederation/core/BUILD.bazel.template
+++ b/experiments/prepare_bazel_test_env/data/templates/tools/tradefederation/core/BUILD.bazel.template
@@ -21,13 +21,15 @@
 package(default_visibility=["//visibility:public"])
 
 _TF_JARS = glob(["{prebuilts_dir_name}/host/tradefed/*.jar"])
+_ATEST_TF_JAR_SRC = ["{prebuilts_dir_name}/host/framework/atest-tradefed.jar"]
+_ATEST_TF_JAR_DEST = ["atest-tradefed.jar"]
 _TF_JARNAMES = [
     f.replace("{prebuilts_dir_name}/host/tradefed/", "") for f in _TF_JARS
 ]
 
 genrule(name="tradefed_lib_soong_import",
-        srcs=_TF_JARS,
-        outs=_TF_JARNAMES,
+        srcs=_TF_JARS + _ATEST_TF_JAR_SRC,
+        outs=_TF_JARNAMES + _ATEST_TF_JAR_DEST,
         cmd="cp -t $(RULEDIR) $(SRCS)")
 
 java_import(name="tradefed_lib", jars=[":tradefed_lib_soong_import"])
@@ -39,6 +41,13 @@
         outs=["script_help.sh"],
         cmd="cp -t $(RULEDIR) $<")
 
+genrule(name="atest_script_help",
+        srcs=[
+            "{prebuilts_dir_name}/host/bin/atest_script_help.sh"
+        ],
+        outs=["atest_script_help.sh"],
+        cmd="cp -t $(RULEDIR) $<")
+
 genrule(
     name="tradefed",
     srcs=[
@@ -55,8 +64,8 @@
 genrule(
     name="atest_tradefed",
     srcs=[
-        ":script_help", ":tradefed_lib",
-        "{prebuilts_dir_name}/host/bin/atest_tradefed.sh"
+        ":tradefed_lib", ":atest_script_help",
+        "{prebuilts_dir_name}/host/bin/atest_tradefed.sh",
     ],
     outs=["atest_tradefed.sh"],
     cmd=