1# Copyright (C) 2025 The Android Open Source Project 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15load("@rules_android//android:rules.bzl", "ApkInfo", "StarlarkApkInfo") 16 17def _get_apk_info(apk): 18 if StarlarkApkInfo in apk: 19 return apk[StarlarkApkInfo].signed_apk 20 return apk[ApkInfo].signed_apk 21 22def _android_instrumentation_test_impl(ctx): 23 apk_file = _get_apk_info(ctx.attr.app) 24 test_apk_file = _get_apk_info(ctx.attr.test_app) 25 adb_file = ctx.attr._adb_exe.files_to_run.executable 26 aapt2_file = ctx.attr._aapt2_exe.files_to_run.executable 27 subst = dict() 28 subst["%%adb_tool_path%%"] = adb_file.short_path 29 subst["%%appt2_tool_path%%"] = aapt2_file.short_path 30 subst["%%apk_path%%"] = apk_file.short_path 31 subst["%%test_apk_path%%"] = test_apk_file.short_path 32 out = ctx.actions.declare_file(ctx.label.name + "_script.sh") 33 ctx.actions.expand_template( 34 template = ctx.file._sh_script_template, 35 is_executable = True, 36 substitutions = subst, 37 output = out, 38 ) 39 runfiles = ctx.runfiles( 40 files = [apk_file, test_apk_file], 41 transitive_files = ctx.attr._adb_exe.files, 42 ).merge(ctx.attr._aapt2_exe.default_runfiles) 43 return [DefaultInfo(executable = out, runfiles = runfiles)] 44 45android_instrumentation_test = rule( 46 implementation = _android_instrumentation_test_impl, 47 attrs = { 48 "app": attr.label( 49 providers = [[ApkInfo], [StarlarkApkInfo]], 50 allow_files = True, 51 ), 52 "test_app": attr.label( 53 providers = [[ApkInfo], [StarlarkApkInfo]], 54 allow_files = True, 55 ), 56 "_adb_exe": attr.label( 57 allow_single_file = True, 58 cfg = "exec", 59 default = Label("@androidsdk//:platform-tools/adb"), 60 ), 61 "_aapt2_exe": attr.label( 62 executable = True, 63 cfg = "exec", 64 default = Label("@androidsdk//:aapt2_binary"), 65 ), 66 "_sh_script_template": attr.label( 67 allow_single_file = True, 68 default = Label(":run_ait_with_adb.sh"), 69 ), 70 }, 71 test = True, 72) 73