Set the correct JAVA_HOME for non-RBE based tests.

..and don't run remote device tests if tagged with 'no-remote'

adb_test tags it.

Test: atest_integration_test
Change-Id: Ibf8221d56fbf9b558f59810ac80be57076f41046
diff --git a/rules/tradefed/tradefed.bzl b/rules/tradefed/tradefed.bzl
index 01c118e..ed22599 100644
--- a/rules/tradefed/tradefed.bzl
+++ b/rules/tradefed/tradefed.bzl
@@ -166,6 +166,11 @@
         values = ["", "32", "64"],
         doc = "the suffix of the test binary",
     ),
+    "_java_runtime": attr.label(
+        default = Label("@bazel_tools//tools/jdk:current_java_runtime"),
+        cfg = "exec",
+        providers = [java_common.JavaRuntimeInfo],
+    ),
 }
 
 # The normalized name of test under tradefed harness. This is without any of the
@@ -312,7 +317,7 @@
 # Generate and run tradefed bash script entry point and associated runfiles.
 def _tradefed_test_impl(ctx, tradefed_options = []):
     device_script = ""
-    if _isRemoteDeviceTest(ctx):
+    if _is_remote_device_test(ctx):
         device_script = _abspath(ctx.attr._run_with[DeviceEnvironment].runner.to_list()[0].short_path)
 
     tf_test_dir = paths.join(ctx.label.name, "testcases")
@@ -446,8 +451,13 @@
     runfiles = runfiles.merge(test_target.default_runfiles)
 
     # Append remote device runfiles if using remote execution.
-    if _isRemoteDeviceTest(ctx):
+    if _is_remote_device_test(ctx):
         runfiles = runfiles.merge(ctx.runfiles().merge(ctx.attr._run_with[DeviceEnvironment].data))
+        java_home = "/jdk/jdk17/linux-x86"
+    else:
+        java_runtime = ctx.attr._java_runtime[java_common.JavaRuntimeInfo]
+        runfiles = runfiles.merge(ctx.runfiles(java_runtime.files.to_list()))
+        java_home = java_runtime.java_home_runfiles_path
 
     # Generate script to run tradefed.
     script = ctx.actions.declare_file("%s.sh" % ctx.label.name)
@@ -465,6 +475,7 @@
             "{additional_tradefed_options}": " ".join(tradefed_options),
             "{test_filter_output}": _abspath(test_filter_output.short_path) if test_filter_output else "",
             "{device_script}": device_script,
+            "{java_home}": java_home,
         },
     )
 
@@ -842,5 +853,7 @@
 def _classpath(jars):
     return ":".join([_abspath(f.short_path) for f in depset(jars).to_list()])
 
-def _isRemoteDeviceTest(ctx):
-    return hasattr(ctx.attr, "_exec_mode") and ctx.attr._exec_mode[BuildSettingInfo].value == "remote"
+def _is_remote_device_test(ctx):
+    return hasattr(ctx.attr, "_exec_mode") and \
+           ctx.attr._exec_mode[BuildSettingInfo].value == "remote" and \
+           "no-remote" not in ctx.attr.tags
diff --git a/rules/tradefed/tradefed.sh.tpl b/rules/tradefed/tradefed.sh.tpl
index 29d5ce2..b28e43e 100644
--- a/rules/tradefed/tradefed.sh.tpl
+++ b/rules/tradefed/tradefed.sh.tpl
@@ -26,13 +26,13 @@
     done
 fi
 
-# Execute device launch script if set. This is for remote device test.
+# Set java home path
+JAVA_HOME="{java_home}"
+export PATH=$JAVA_HOME/bin:$PATH
 if [ ! -z LAUNCH_AVD_EXECUTABLE ];
 then
-    # Set java path for remote environment.
-    JAVA_HOME=/jdk/jdk17/linux-x86
-    export PATH=$JAVA_HOME/bin:$PATH
-    java -version
+    # Execute device launch script if set. This is for remote device test.
+    java -version # smoke test for jre
     $LAUNCH_AVD_EXECUTABLE
 fi