Repository navigation
Expand file tree
/
Copy pathstartAppFunctionTestingAgent
More file actions
executable file
Β·137 lines (121 loc) Β· 5.28 KB
/
Copy pathstartAppFunctionTestingAgent
File metadata and controls
executable file
Β·137 lines (121 loc) Β· 5.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/bin/bash
#
# Copyright 2026 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
#
# https://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.
#
set -e
PACKAGE_NAME="com.example.appfunctions.agent"
INSTRUMENTATION_NAME="${PACKAGE_NAME}/.ShellIdentityInstrumentation"
SERIAL=""
usage() {
echo "Usage: $0 [OPTIONS]"
echo ""
echo "Options:"
echo " -s, --serial SERIAL ADB device serial number"
echo " -h, --help Show this help message"
echo ""
echo "Description:"
echo " Launches the AppFunction Testing Agent app with elevated privileges"
echo " using ShellIdentityInstrumentation."
echo " Note: The app must be manually installed before running this script."
}
while [[ "$#" -gt 0 ]]; do
case $1 in
-s|--serial) SERIAL="$2"; shift ;;
-h|--help) usage; exit 0 ;;
*) echo "Unknown parameter: $1"; usage; exit 1 ;;
esac
shift
done
# Device disambiguation
DEVICE_COUNT=$(adb devices | grep -v "List of devices attached" | grep -w "device" | wc -l | tr -d ' ')
if [[ -n "$SERIAL" ]]; then
ADB="adb -s $SERIAL"
elif [[ "$DEVICE_COUNT" -eq 1 ]]; then
SERIAL=$(adb devices | grep -v "List of devices attached" | grep -w "device" | awk '{print $1}')
ADB="adb -s $SERIAL"
echo "π± Using device: $SERIAL"
elif [[ "$DEVICE_COUNT" -eq 0 ]]; then
echo "β No devices connected via ADB."
exit 1
else
echo "β Multiple devices connected. Please specify one with -s or --serial:"
adb devices | grep -v "List of devices attached" | grep -w "device"
exit 1
fi
echo "π Verifying package installation..."
if ! $ADB shell pm list packages | grep -q "$PACKAGE_NAME"; then
echo "β Package $PACKAGE_NAME is not installed on $SERIAL."
echo "Please install it first."
exit 1
fi
echo "π Extracting package fingerprint from device..."
FINGERPRINT=$($ADB shell dumpsys package $PACKAGE_NAME | grep PackageSignatures | awk -F 'signatures:\\[' '{print $2}' | awk -F '\\]' '{print $1}' | tr '[:upper:]' '[:lower:]')
if [[ -z "$FINGERPRINT" ]]; then
echo "β Could not extract fingerprint from device for package $PACKAGE_NAME."
exit 1
fi
echo "β
Using fingerprint: $FINGERPRINT"
SDK_VERSION=$($ADB shell getprop ro.build.version.sdk)
if [[ $SDK_VERSION -ge 37 ]]; then
echo "π Adding package to allowlist..."
# Replaced by flush-allowlist-change in 17.1
if $ADB shell cmd app_function help 2>&1 | grep -q "purge-allowlist-cache"; then
$ADB shell cmd app_function purge-allowlist-cache
fi
if $ADB shell cmd allowlist help 2>&1 | grep -q "add-package-with-metadata-multimap"; then
$ADB shell cmd allowlist add-package-with-metadata-multimap 2 ${PACKAGE_NAME}:${FINGERPRINT} android.allowlist.metadata.key.APP_FUNCTION_TYPE_DOWNLOADED:boolean:true "'*'" ignore:string:default
else
$ADB shell cmd allowlist add-package-multimap 2 ${PACKAGE_NAME}:${FINGERPRINT} "'*'"
fi
# flush-allowlist-changes is not in help page. Use set-temporary-caller as proxy since it is
# guarded with the same flag
if $ADB shell cmd app_function help 2>&1 | grep -q "set-temporary-caller"; then
echo "Flush allowlist changes"
$ADB shell cmd app_function flush-allowlist-changes
fi
else
echo "Skipping allowlist for SDK version $SDK_VERSION..."
fi
if [[ $SDK_VERSION -ge 35 ]]; then
# Allow audio focus AppOps for com.android.shell since ShellIdentityInstrumentation
# redirects permissionless AppOps checks to the shell UID under audio hardening.
for OP in TAKE_AUDIO_FOCUS CONTROL_AUDIO CONTROL_AUDIO_PARTIAL; do
$ADB shell cmd appops set --uid com.android.shell "$OP" allow 2>/dev/null || true
$ADB shell cmd appops set com.android.shell "$OP" allow 2>/dev/null || true
done
fi
echo "π Stopping any existing AppFunction Testing Agent instances..."
$ADB shell am force-stop "$PACKAGE_NAME"
echo "π Starting AppFunction Testing Agent via ShellIdentityInstrumentation..."
# Run in background with -w to ensure UiAutomation is available
$ADB shell am instrument -w "$INSTRUMENTATION_NAME" > ./.instrumentation_output.txt 2>&1 &
PID=$!
sleep 2
if ! kill -0 $PID 2>/dev/null; then
echo "β οΈ Instrumentation failed to start immediately."
OUTPUT=$(cat ./.instrumentation_output.txt)
echo "$OUTPUT"
echo "Falling back to normal app launch..."
$ADB shell am start -n "$PACKAGE_NAME/.MainActivity" --es "EXTRA_INSTRUMENTATION_ERROR" "$OUTPUT"
rm ./.instrumentation_output.txt
else
echo "β
Instrumentation started successfully."
fi
CURRENT_VOICE_SERVICE=$($ADB shell settings get secure voice_interaction_service | tr -d '\r')
if [[ "$CURRENT_VOICE_SERVICE" == "${PACKAGE_NAME}/"* ]]; then
echo "π Rebinding active VoiceInteractionService after force-stop..."
$ADB shell settings delete secure voice_interaction_service >/dev/null
$ADB shell settings put secure voice_interaction_service "$CURRENT_VOICE_SERVICE"
fi