-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_toxee_android.sh
More file actions
executable file
·393 lines (338 loc) · 11.1 KB
/
run_toxee_android.sh
File metadata and controls
executable file
·393 lines (338 loc) · 11.1 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
#!/bin/bash
# Android mobile/tablet package/deploy/run script for toxee.
# Style aligned with run_toxee.sh.
set -euo pipefail
# ============================================================
# Configuration
# ============================================================
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
FLUTTER_APP_DIR="$SCRIPT_DIR"
BUILD_DIR="$FLUTTER_APP_DIR/build/android_mobile"
FLUTTER_BUILD_LOG="$BUILD_DIR/flutter_android_build.log"
DEPLOY_LOG="$BUILD_DIR/flutter_android_deploy.log"
APP_PACKAGE_ID="com.example.toxee"
JNI_LIBS_DIR="$FLUTTER_APP_DIR/android/app/src/main/jniLibs"
ACTION="run" # package | deploy | run
MODE="debug" # debug | profile | release
DEVICE_TYPE="phone" # phone | tablet | any
DEVICE_ID=""
FFI_LIB_DIR="${TIM2TOX_ANDROID_LIB_DIR:-}"
LIST_DEVICES="false"
SKIP_PUB_GET="false"
ANDROID_ABIS=("arm64-v8a" "armeabi-v7a" "x86_64" "x86")
mkdir -p "$BUILD_DIR"
# Bootstrap dependencies so pubspec_overrides and third_party are ready
(cd "$FLUTTER_APP_DIR" && dart run tool/bootstrap_deps.dart) >> "$BUILD_DIR/bootstrap.log" 2>&1 || true
# Colors
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m'
# ============================================================
# UI helpers
# ============================================================
usage() {
cat <<EOF
Usage: $(basename "$0") [OPTIONS]
Android package/deploy/run script for phone/tablet.
Options:
--action <package|deploy|run> Action to execute (default: run)
--mode <debug|profile|release> Flutter build mode (default: debug)
--device-type <phone|tablet|any>
Target Android device type (default: phone)
--device-id <id> Explicit adb device id (overrides --device-type)
--ffi-lib-dir <dir> Directory containing per-ABI tim2tox libs:
<dir>/<abi>/libtim2tox_ffi.so
--list-devices List connected Android devices and exit
--skip-pub-get Skip flutter pub get step
--help Show this help
Examples:
$(basename "$0") --action package --mode release
$(basename "$0") --action deploy --device-type tablet
$(basename "$0") --action run --device-id emulator-5554
EOF
}
info() { echo -e "${GREEN}[INFO]${NC} $*"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $*"; }
error() { echo -e "${RED}[ERROR]${NC} $*"; }
# ============================================================
# Argument parsing
# ============================================================
while [[ $# -gt 0 ]]; do
case "$1" in
--action)
ACTION="${2:-}"; shift 2;;
--mode)
MODE="${2:-}"; shift 2;;
--device-type)
DEVICE_TYPE="${2:-}"; shift 2;;
--device-id)
DEVICE_ID="${2:-}"; shift 2;;
--ffi-lib-dir)
FFI_LIB_DIR="${2:-}"; shift 2;;
--list-devices)
LIST_DEVICES="true"; shift;;
--skip-pub-get)
SKIP_PUB_GET="true"; shift;;
--help|-h)
usage; exit 0;;
*)
error "Unknown option: $1"
usage
exit 1;;
esac
done
case "$ACTION" in
package|deploy|run) ;;
*)
error "Invalid --action: $ACTION"
usage
exit 1;;
esac
case "$MODE" in
debug|profile|release) ;;
*)
error "Invalid --mode: $MODE"
usage
exit 1;;
esac
case "$DEVICE_TYPE" in
phone|tablet|any) ;;
*)
error "Invalid --device-type: $DEVICE_TYPE"
usage
exit 1;;
esac
# ============================================================
# Preflight
# ============================================================
require_cmd() {
local cmd="$1"
if ! command -v "$cmd" >/dev/null 2>&1; then
error "Missing command: $cmd"
exit 1
fi
}
preflight_checks() {
require_cmd flutter
require_cmd adb
if [[ ! -f "$FLUTTER_APP_DIR/pubspec.yaml" ]]; then
error "Flutter app not found: $FLUTTER_APP_DIR"
exit 1
fi
}
prepare_flutter_deps() {
if [[ "$SKIP_PUB_GET" == "true" ]]; then
warn "Skipping flutter pub get (--skip-pub-get)"
return
fi
if [[ ! -f "$FLUTTER_APP_DIR/pubspec.lock" ]] || \
[[ "$FLUTTER_APP_DIR/pubspec.yaml" -nt "$FLUTTER_APP_DIR/pubspec.lock" ]]; then
info "Running flutter pub get..."
(cd "$FLUTTER_APP_DIR" && flutter pub get) >>"$FLUTTER_BUILD_LOG" 2>&1
fi
}
# ============================================================
# Device selection
# ============================================================
get_connected_android_devices() {
adb devices | awk 'NR>1 && $2=="device" {print $1}'
}
classify_android_device() {
local device_id="$1"
local size density min_px smallest_dp
size="$(adb -s "$device_id" shell wm size 2>/dev/null | tr -d '\r' | awk -F': ' '/Physical size/ {print $2; exit}')"
density="$(adb -s "$device_id" shell wm density 2>/dev/null | tr -d '\r' | awk -F': ' '/Physical density/ {print $2; exit}')"
if [[ -z "$size" || -z "$density" ]]; then
echo "unknown"
return
fi
if ! [[ "$density" =~ ^[0-9]+$ ]]; then
echo "unknown"
return
fi
min_px="$(awk -F'x' '{if ($1 < $2) print $1; else print $2}' <<<"$size")"
if ! [[ "$min_px" =~ ^[0-9]+$ ]]; then
echo "unknown"
return
fi
smallest_dp="$(awk -v px="$min_px" -v den="$density" 'BEGIN {printf "%.0f", (px*160)/den}')"
if [[ "$smallest_dp" -ge 600 ]]; then
echo "tablet"
else
echo "phone"
fi
}
list_android_devices() {
local d device_class count="0"
if [[ -z "$(get_connected_android_devices)" ]]; then
warn "No connected Android devices."
return
fi
echo -e "${CYAN}Connected Android devices:${NC}"
while IFS= read -r d; do
[[ -z "$d" ]] && continue
count=$((count + 1))
device_class="$(classify_android_device "$d")"
echo " $d [$device_class]"
done < <(get_connected_android_devices)
if [[ "$count" -eq 0 ]]; then
warn "No connected Android devices."
fi
}
SELECTED_DEVICE_ID=""
SELECTED_DEVICE_CLASS=""
select_android_device() {
local d c has_any="false"
while IFS= read -r d; do
[[ -z "$d" ]] && continue
has_any="true"
break
done < <(get_connected_android_devices)
if [[ "$has_any" != "true" ]]; then
error "No connected Android devices found."
exit 1
fi
if [[ -n "$DEVICE_ID" ]]; then
while IFS= read -r d; do
[[ -z "$d" ]] && continue
if [[ "$d" == "$DEVICE_ID" ]]; then
SELECTED_DEVICE_ID="$d"
SELECTED_DEVICE_CLASS="$(classify_android_device "$d")"
return
fi
done < <(get_connected_android_devices)
error "Requested device id not found: $DEVICE_ID"
exit 1
fi
if [[ "$DEVICE_TYPE" == "any" ]]; then
while IFS= read -r d; do
[[ -z "$d" ]] && continue
SELECTED_DEVICE_ID="$d"
SELECTED_DEVICE_CLASS="$(classify_android_device "$SELECTED_DEVICE_ID")"
return
done < <(get_connected_android_devices)
fi
while IFS= read -r d; do
[[ -z "$d" ]] && continue
c="$(classify_android_device "$d")"
if [[ "$c" == "$DEVICE_TYPE" ]]; then
SELECTED_DEVICE_ID="$d"
SELECTED_DEVICE_CLASS="$c"
return
fi
done < <(get_connected_android_devices)
error "No Android device matched --device-type=$DEVICE_TYPE"
list_android_devices
exit 1
}
# ============================================================
# FFI library preparation
# ============================================================
prepare_android_ffi_libs() {
mkdir -p "$JNI_LIBS_DIR"
if [[ -n "$FFI_LIB_DIR" ]]; then
if [[ ! -d "$FFI_LIB_DIR" ]]; then
error "--ffi-lib-dir is not a directory: $FFI_LIB_DIR"
exit 1
fi
info "Syncing tim2tox Android FFI libs from: $FFI_LIB_DIR"
local abi src dst copied="0"
for abi in "${ANDROID_ABIS[@]}"; do
src="$FFI_LIB_DIR/$abi/libtim2tox_ffi.so"
dst="$JNI_LIBS_DIR/$abi/libtim2tox_ffi.so"
if [[ -f "$src" ]]; then
mkdir -p "$JNI_LIBS_DIR/$abi"
cp "$src" "$dst"
copied="1"
fi
done
if [[ "$copied" == "0" ]]; then
error "No libtim2tox_ffi.so found in $FFI_LIB_DIR/<abi>/"
exit 1
fi
fi
if ! find "$JNI_LIBS_DIR" -type f -name "libtim2tox_ffi.so" | grep -q .; then
error "Missing tim2tox Android FFI library."
echo "Expected at least one of:"
for abi in "${ANDROID_ABIS[@]}"; do
echo " $JNI_LIBS_DIR/$abi/libtim2tox_ffi.so"
done
echo ""
echo "Provide --ffi-lib-dir <dir> where <dir>/<abi>/libtim2tox_ffi.so exists."
exit 1
fi
}
# ============================================================
# Build / deploy / run
# ============================================================
build_android_apk() {
: >"$FLUTTER_BUILD_LOG"
info "Building Android APK ($MODE)..."
(cd "$FLUTTER_APP_DIR" && flutter build apk --"$MODE" --dart-define=FLUTTER_BUILD_MODE="$MODE") >>"$FLUTTER_BUILD_LOG" 2>&1
info "Build completed."
}
apk_output_path() {
case "$MODE" in
debug) echo "$FLUTTER_APP_DIR/build/app/outputs/flutter-apk/app-debug.apk" ;;
profile) echo "$FLUTTER_APP_DIR/build/app/outputs/flutter-apk/app-profile.apk" ;;
release) echo "$FLUTTER_APP_DIR/build/app/outputs/flutter-apk/app-release.apk" ;;
esac
}
deploy_android_apk() {
local apk_path
apk_path="$(apk_output_path)"
if [[ ! -f "$apk_path" ]]; then
warn "APK not found, building first: $apk_path"
build_android_apk
fi
select_android_device
: >"$DEPLOY_LOG"
info "Deploying APK to $SELECTED_DEVICE_ID ($SELECTED_DEVICE_CLASS)..."
adb -s "$SELECTED_DEVICE_ID" install -r "$apk_path" >>"$DEPLOY_LOG" 2>&1
info "Deploy completed."
}
launch_android_app() {
select_android_device
info "Launching $APP_PACKAGE_ID on $SELECTED_DEVICE_ID..."
adb -s "$SELECTED_DEVICE_ID" shell am force-stop "$APP_PACKAGE_ID" >/dev/null 2>&1 || true
adb -s "$SELECTED_DEVICE_ID" shell monkey -p "$APP_PACKAGE_ID" -c android.intent.category.LAUNCHER 1 >/dev/null 2>&1
local pid
pid="$(adb -s "$SELECTED_DEVICE_ID" shell pidof -s "$APP_PACKAGE_ID" 2>/dev/null | tr -d '\r' || true)"
if [[ -n "$pid" ]]; then
echo ""
echo -e "${GREEN}Tailing logcat for PID $pid (Ctrl+C to stop)...${NC}"
adb -s "$SELECTED_DEVICE_ID" logcat --pid="$pid"
else
warn "Could not get process PID; falling back to package-name grep."
adb -s "$SELECTED_DEVICE_ID" logcat | grep --line-buffered "$APP_PACKAGE_ID"
fi
}
# ============================================================
# Main
# ============================================================
echo -e "${CYAN}╔══════════════════════════════════════════════════════╗${NC}"
echo -e "${CYAN}║ Toxee — Android Mobile/Tablet ║${NC}"
echo -e "${CYAN}╚══════════════════════════════════════════════════════╝${NC}"
preflight_checks
if [[ "$LIST_DEVICES" == "true" ]]; then
list_android_devices
exit 0
fi
prepare_flutter_deps
prepare_android_ffi_libs
case "$ACTION" in
package)
build_android_apk
info "APK: $(apk_output_path)"
;;
deploy)
deploy_android_apk
;;
run)
build_android_apk
deploy_android_apk
launch_android_app
;;
esac