Skip to content

Commit a0aa025

Browse files
authored
build(android): unify the 4 helper build/package scripts behind one parameterized pair (callstack#1466)
* build(android): unify snapshot/ime helper build+package scripts Replace the four ~75%-duplicated shell scripts with one parameterized build script and one parameterized package script, mirroring scripts/build-xcuitest-apple.sh's env-var-driven pattern. The helper is selected via AGENT_DEVICE_ANDROID_HELPER or a first positional arg; per-helper differences (HELPER_DIR/PACKAGE_NAME, snapshot's test-compile+run step, ime's aapt2 resource-compile step, and the manifest JSON fields) live in small case blocks. All package.json entry points keep their names and output paths. Verified byte-level equivalence between main and this branch: identical unzip -l listings, identical classes.dex SHA-256 for both helpers, and identical manifest fields (only the per-signing-run sha256 differs). Fixes callstack#1461 * chore: drop stale android/multitouch-helper .gitignore entries The multitouch helper was consolidated away in callstack#1281; these two lines were never cleaned up.
1 parent 6f476b5 commit a0aa025

11 files changed

Lines changed: 304 additions & 377 deletions

‎.github/workflows/release-android-snapshot-helper.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,11 @@ jobs:
7272
id: package
7373
env:
7474
RELEASE_ASSET_DIR: ${{ github.workspace }}/.tmp/release-assets
75+
AGENT_DEVICE_ANDROID_HELPER: snapshot
7576
run: |
7677
set -euo pipefail
7778
mkdir -p "${RELEASE_ASSET_DIR}"
78-
sh ./scripts/package-android-snapshot-helper.sh \
79+
sh ./scripts/package-android-helper.sh \
7980
"${{ steps.meta.outputs.version }}" \
8081
"${{ steps.meta.outputs.tag }}" \
8182
"${RELEASE_ASSET_DIR}"

‎.gitignore‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,3 @@ android/snapshot-helper/build/
4040
android/snapshot-helper/dist/
4141
android/ime-helper/build/
4242
android/ime-helper/dist/
43-
android/multitouch-helper/build/
44-
android/multitouch-helper/dist/

‎android/ime-helper/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ verified the same way (bundled npm-packaged dist + version-keyed manifest.json),
2121

2222
```sh
2323
VERSION="$(node -p 'require("./package.json").version')"
24-
sh ./scripts/build-android-ime-helper.sh "$VERSION" .tmp/android-ime-helper
24+
AGENT_DEVICE_ANDROID_HELPER=ime sh ./scripts/build-android-helper.sh "$VERSION" .tmp/android-ime-helper
2525
```
2626

2727
## Run

‎android/snapshot-helper/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ requires a stable signing certificate for `adb install -r` upgrades.
1515

1616
```sh
1717
VERSION="$(node -p 'require("./package.json").version')"
18-
sh ./scripts/build-android-snapshot-helper.sh "$VERSION" .tmp/android-snapshot-helper
18+
AGENT_DEVICE_ANDROID_HELPER=snapshot sh ./scripts/build-android-helper.sh "$VERSION" .tmp/android-snapshot-helper
1919
```
2020

2121
The build uses Android SDK command-line tools directly. It expects `ANDROID_HOME` or

‎package.json‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,14 @@
8888
"build:xcuitest:tvos:clean": "pnpm clean:xcuitest:tvos && pnpm build:xcuitest:tvos",
8989
"build:xcuitest:visionos": "AGENT_DEVICE_XCUITEST_PLATFORM=visionos sh ./scripts/build-xcuitest-apple.sh",
9090
"build:xcuitest:visionos:clean": "pnpm clean:xcuitest:visionos && pnpm build:xcuitest:visionos",
91-
"build:android-snapshot-helper": "sh ./scripts/build-android-snapshot-helper.sh $(node -p \"require('./package.json').version\") .tmp/android-snapshot-helper",
92-
"package:android-snapshot-helper": "sh ./scripts/package-android-snapshot-helper.sh $(node -p \"require('./package.json').version\") v$(node -p \"require('./package.json').version\") .tmp/android-snapshot-helper",
93-
"package:android-snapshot-helper:npm": "rm -rf android/snapshot-helper/dist && sh ./scripts/package-android-snapshot-helper.sh $(node -p \"require('./package.json').version\") v$(node -p \"require('./package.json').version\") android/snapshot-helper/dist",
91+
"build:android-snapshot-helper": "AGENT_DEVICE_ANDROID_HELPER=snapshot sh ./scripts/build-android-helper.sh $(node -p \"require('./package.json').version\") .tmp/android-snapshot-helper",
92+
"package:android-snapshot-helper": "AGENT_DEVICE_ANDROID_HELPER=snapshot sh ./scripts/package-android-helper.sh $(node -p \"require('./package.json').version\") v$(node -p \"require('./package.json').version\") .tmp/android-snapshot-helper",
93+
"package:android-snapshot-helper:npm": "rm -rf android/snapshot-helper/dist && AGENT_DEVICE_ANDROID_HELPER=snapshot sh ./scripts/package-android-helper.sh $(node -p \"require('./package.json').version\") v$(node -p \"require('./package.json').version\") android/snapshot-helper/dist",
9494
"package:apple-runner:npm": "node scripts/package-apple-runner-source.mjs",
9595
"build:android": "pnpm package:android-snapshot-helper:npm && pnpm package:android-ime-helper:npm",
96-
"build:android-ime-helper": "sh ./scripts/build-android-ime-helper.sh $(node -p \"require('./package.json').version\") .tmp/android-ime-helper",
97-
"package:android-ime-helper": "sh ./scripts/package-android-ime-helper.sh $(node -p \"require('./package.json').version\") .tmp/android-ime-helper",
98-
"package:android-ime-helper:npm": "rm -rf android/ime-helper/dist && sh ./scripts/package-android-ime-helper.sh $(node -p \"require('./package.json').version\") android/ime-helper/dist",
96+
"build:android-ime-helper": "AGENT_DEVICE_ANDROID_HELPER=ime sh ./scripts/build-android-helper.sh $(node -p \"require('./package.json').version\") .tmp/android-ime-helper",
97+
"package:android-ime-helper": "AGENT_DEVICE_ANDROID_HELPER=ime sh ./scripts/package-android-helper.sh $(node -p \"require('./package.json').version\") .tmp/android-ime-helper",
98+
"package:android-ime-helper:npm": "rm -rf android/ime-helper/dist && AGENT_DEVICE_ANDROID_HELPER=ime sh ./scripts/package-android-helper.sh $(node -p \"require('./package.json').version\") android/ime-helper/dist",
9999
"build:macos-helper": "swift build -c release --package-path apple/macos-helper",
100100
"build:all": "pnpm build:node && pnpm build:xcuitest",
101101
"ad": "node bin/agent-device.mjs",

‎scripts/build-android-helper.sh‎

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
#!/bin/sh
2+
set -eu
3+
4+
HELPER="${AGENT_DEVICE_ANDROID_HELPER:-}"
5+
if [ -z "$HELPER" ]; then
6+
HELPER="${1:-}"
7+
[ "$#" -ge 1 ] && shift
8+
fi
9+
10+
if [ "$#" -ne 2 ]; then
11+
echo "Usage: AGENT_DEVICE_ANDROID_HELPER=<snapshot|ime> $0 <version> <output-dir>" >&2
12+
echo " or: $0 <snapshot|ime> <version> <output-dir>" >&2
13+
exit 1
14+
fi
15+
16+
VERSION="$1"
17+
OUTPUT_DIR="$2"
18+
PROJECT_DIR="$(CDPATH= cd -- "$(dirname -- "$0")/.." && pwd)"
19+
MIN_SDK=23
20+
TARGET_SDK=36
21+
KEYSTORE="$PROJECT_DIR/android/snapshot-helper/debug.keystore"
22+
23+
# Per-helper config: HELPER_DIR/PACKAGE_NAME/APK name always differ; RUN_TEST_CLASS is
24+
# only set for the snapshot helper (it has a unit test to compile+run); RESOURCE_DIR is
25+
# only set for the ime helper (it has a res/ dir that needs an aapt2 compile pass).
26+
case "$HELPER" in
27+
snapshot)
28+
HELPER_DIR="$PROJECT_DIR/android/snapshot-helper"
29+
PACKAGE_NAME="com.callstack.agentdevice.snapshothelper"
30+
RUN_TEST_CLASS="com.callstack.agentdevice.snapshothelper.PointerEventScheduleTest"
31+
RESOURCE_DIR=""
32+
;;
33+
ime)
34+
HELPER_DIR="$PROJECT_DIR/android/ime-helper"
35+
PACKAGE_NAME="com.callstack.agentdevice.imehelper"
36+
RUN_TEST_CLASS=""
37+
RESOURCE_DIR="$PROJECT_DIR/android/ime-helper/res"
38+
;;
39+
*)
40+
echo "Unknown Android helper: '$HELPER' (expected 'snapshot' or 'ime')" >&2
41+
exit 1
42+
;;
43+
esac
44+
45+
APK_BASENAME="agent-device-android-$HELPER-helper-$VERSION.apk"
46+
47+
SDK_ROOT="${ANDROID_HOME:-${ANDROID_SDK_ROOT:-}}"
48+
if [ -z "$SDK_ROOT" ] || [ ! -d "$SDK_ROOT" ]; then
49+
echo "ANDROID_HOME or ANDROID_SDK_ROOT must point to an Android SDK" >&2
50+
exit 1
51+
fi
52+
53+
ANDROID_JAR="$SDK_ROOT/platforms/android-$TARGET_SDK/android.jar"
54+
if [ ! -f "$ANDROID_JAR" ]; then
55+
echo "Missing Android platform jar: $ANDROID_JAR" >&2
56+
exit 1
57+
fi
58+
59+
BUILD_TOOLS_DIR="$(
60+
find "$SDK_ROOT/build-tools" -maxdepth 1 -mindepth 1 -type d 2>/dev/null | sort -V | tail -n 1
61+
)"
62+
if [ -z "$BUILD_TOOLS_DIR" ] || [ ! -x "$BUILD_TOOLS_DIR/aapt2" ]; then
63+
echo "Missing Android build tools under $SDK_ROOT/build-tools" >&2
64+
exit 1
65+
fi
66+
67+
VERSION_CODE="$(
68+
printf '%s\n' "$VERSION" | awk -F. '
69+
/^[0-9]+[.][0-9]+[.][0-9]+$/ {
70+
print ($1 * 1000000) + ($2 * 1000) + $3
71+
next
72+
}
73+
{ print 1 }
74+
'
75+
)"
76+
77+
BUILD_DIR="$HELPER_DIR/build"
78+
CLASSES_DIR="$BUILD_DIR/classes"
79+
TEST_CLASSES_DIR="$BUILD_DIR/test-classes"
80+
DEX_DIR="$BUILD_DIR/dex"
81+
RES_COMPILED_DIR="$BUILD_DIR/res-compiled"
82+
UNSIGNED_APK="$BUILD_DIR/helper-unsigned.apk"
83+
ALIGNED_APK="$BUILD_DIR/helper-aligned.apk"
84+
APK_PATH="$OUTPUT_DIR/$APK_BASENAME"
85+
86+
rm -rf "$BUILD_DIR"
87+
mkdir -p "$CLASSES_DIR" "$TEST_CLASSES_DIR" "$DEX_DIR" "$RES_COMPILED_DIR" "$OUTPUT_DIR"
88+
89+
javac \
90+
--release 11 \
91+
-classpath "$ANDROID_JAR" \
92+
-d "$CLASSES_DIR" \
93+
$(find "$HELPER_DIR/src/main/java" -name '*.java' | sort)
94+
95+
if [ -n "$RUN_TEST_CLASS" ]; then
96+
javac \
97+
--release 11 \
98+
-classpath "$ANDROID_JAR:$CLASSES_DIR" \
99+
-d "$TEST_CLASSES_DIR" \
100+
$(find "$HELPER_DIR/src/test/java" -name '*.java' | sort)
101+
102+
java \
103+
-classpath "$ANDROID_JAR:$CLASSES_DIR:$TEST_CLASSES_DIR" \
104+
"$RUN_TEST_CLASS"
105+
fi
106+
107+
"$BUILD_TOOLS_DIR/d8" \
108+
--min-api "$MIN_SDK" \
109+
--classpath "$ANDROID_JAR" \
110+
--output "$DEX_DIR" \
111+
$(find "$CLASSES_DIR" -name '*.class' | sort)
112+
113+
if [ -n "$RESOURCE_DIR" ]; then
114+
"$BUILD_TOOLS_DIR/aapt2" compile \
115+
--dir "$RESOURCE_DIR" \
116+
-o "$RES_COMPILED_DIR"
117+
118+
"$BUILD_TOOLS_DIR/aapt2" link \
119+
--manifest "$HELPER_DIR/AndroidManifest.xml" \
120+
-I "$ANDROID_JAR" \
121+
--min-sdk-version "$MIN_SDK" \
122+
--target-sdk-version "$TARGET_SDK" \
123+
--version-code "$VERSION_CODE" \
124+
--version-name "$VERSION" \
125+
-R "$RES_COMPILED_DIR"/*.flat \
126+
-o "$UNSIGNED_APK"
127+
else
128+
"$BUILD_TOOLS_DIR/aapt2" link \
129+
--manifest "$HELPER_DIR/AndroidManifest.xml" \
130+
-I "$ANDROID_JAR" \
131+
--min-sdk-version "$MIN_SDK" \
132+
--target-sdk-version "$TARGET_SDK" \
133+
--version-code "$VERSION_CODE" \
134+
--version-name "$VERSION" \
135+
-o "$UNSIGNED_APK"
136+
fi
137+
138+
zip -q -j "$UNSIGNED_APK" "$DEX_DIR/classes.dex"
139+
140+
"$BUILD_TOOLS_DIR/zipalign" -f 4 "$UNSIGNED_APK" "$ALIGNED_APK"
141+
142+
if [ ! -f "$KEYSTORE" ]; then
143+
echo "Missing Android helper signing keystore: $KEYSTORE" >&2
144+
exit 1
145+
fi
146+
147+
"$BUILD_TOOLS_DIR/apksigner" sign \
148+
--ks "$KEYSTORE" \
149+
--ks-pass pass:android \
150+
--key-pass pass:android \
151+
--out "$APK_PATH" \
152+
"$ALIGNED_APK"
153+
154+
"$BUILD_TOOLS_DIR/apksigner" verify --min-sdk-version "$MIN_SDK" "$APK_PATH"
155+
156+
printf 'apk=%s\n' "$APK_PATH"
157+
printf 'package=%s\n' "$PACKAGE_NAME"
158+
printf 'version_code=%s\n' "$VERSION_CODE"

‎scripts/build-android-ime-helper.sh‎

Lines changed: 0 additions & 107 deletions
This file was deleted.

0 commit comments

Comments
 (0)