#!/bin/bash # SPDX-License-Identifier: AGPL-3.0-or-later # # Build Beaver for Android. # # Usage: # ./scripts/build-android.sh [--abi arm64|armv7|all] [--profile ] # [--check | --no-gradle] [--install] [--run] # # --abi arm64 for phones (the default), armv7 for Android TV, or all # --profile the cargo profile: production (the default), or release, which # builds faster and keeps the symbols that ndk-stack needs # --check only type-check the Android build (cargo check) # --no-gradle stop once the libraries and assets are staged # --install install the APK with adb (ANDROID_SERIAL picks the device) # --run restart the app, then follow its log # # It builds libbeaver_shell.so for each ABI and stages it with libc++_shared.so # in target/android/jniLibs//, stages the UI and Servo's resources in # target/android/assets/beaver/, then packages them with packaging/android into # dist/beaver__android__.apk. # # Needs an Android SDK with NDK r29 (see scripts/android-env.sh for how they # are found), a JDK 17 or newer for Gradle, and the Rust targets: # rustup target add aarch64-linux-android armv7-linux-androideabi set -e cd "$(dirname "$0")/.." usage() { sed -n '6,16p' "$0" >&2 exit 2 } app_id=org.webbeef.beaver abi=arm64 profile=production mode=build gradle=1 install=0 run=0 while [ $# -gt 0 ]; do case "$1" in --abi | --profile) [ $# -ge 2 ] || usage if [ "$1" = --abi ]; then abi="$2"; else profile="$2"; fi shift ;; --check) mode=check ;; --no-gradle) gradle=0 ;; --install) install=1 ;; --run) run=1 ;; *) usage ;; esac shift done case "$abi" in arm64) triples=aarch64-linux-android ;; armv7) triples=armv7-linux-androideabi ;; all) triples="aarch64-linux-android armv7-linux-androideabi" ;; *) usage ;; esac if [ "$install" = 1 ] && { [ "$mode" = check ] || [ "$gradle" = 0 ]; }; then echo "build-android.sh: --install needs the APK, so it can't go with --check or --no-gradle" >&2 exit 2 fi case "$profile" in dev) profile_dir=debug ;; *) profile_dir="$profile" ;; esac target_dir="${CARGO_TARGET_DIR:-target}" staging="$target_dir/android" features=(--no-default-features --features android) # The UI and Servo's resources, plus the two files src/android/assets.rs relies # on: MANIFEST lists the files, and STAMP hashes their names and contents, so # the app only extracts them again when they changed. stage_assets() { local dest="$staging/assets/beaver" rm -rf "$dest" mkdir -p "$dest" cp -R ui resources "$dest/" # Servo's Android font list falls back to this emoji font: the system's is # out of its reach (see packaging/android/fonts/source.txt). cp -R packaging/android/fonts "$dest/resources/" # Dotfiles and editor or merge leftovers stay out of the APK. find "$dest" \( -name '.*' -o -name '*~' -o -name '*.orig' -o -name '*.rej' \) \ -prune -exec rm -rf {} + # The APK packaging silently drops directories whose name starts with "_". local dropped dropped="$(find "$dest" -type d -name '_*')" if [ -n "$dropped" ]; then echo "build-android.sh: these directories would be left out of the APK:" >&2 echo "$dropped" >&2 exit 1 fi ( cd "$dest" find ui resources -type f | LC_ALL=C sort > MANIFEST if command -v sha256sum > /dev/null; then sha256="sha256sum" else sha256="shasum -a 256" fi tr '\n' '\0' < MANIFEST | xargs -0 $sha256 | $sha256 | cut -d ' ' -f 1 > STAMP ) echo "Staged $(wc -l < "$dest/MANIFEST" | tr -d ' ') files in $dest" } # The APK carries exactly the ABIs of this run. if [ "$mode" = build ]; then rm -rf "$staging/jniLibs" fi for triple in $triples; do # One subshell per ABI, so one ABI's toolchain variables never reach the next. ( . scripts/android-env.sh "$triple" if [ "$mode" = check ]; then cargo check -p beaver-shell --target "$triple" --profile "$profile" "${features[@]}" exit 0 fi cargo rustc -p beaver-shell --lib --crate-type=cdylib --target "$triple" \ --profile "$profile" "${features[@]}" libs="$staging/jniLibs/$BEAVER_ANDROID_ABI" mkdir -p "$libs" # With the release profile, the library in target/ keeps its debug info # for ndk-stack. The production profile strips it already. "$TARGET_STRIP" --strip-debug -o "$libs/libbeaver_shell.so" \ "$target_dir/$triple/$profile_dir/libbeaver_shell.so" cp "$BEAVER_NDK_LIBCXX" "$libs/" echo "Staged $libs" ) done if [ "$mode" = check ]; then exit 0 fi stage_assets # The SDK, for Gradle and adb. sdk="$(. scripts/android-env.sh aarch64-linux-android > /dev/null && echo "$ANDROID_HOME")" if [ "$gradle" = 1 ]; then version="$(sed -n 's/^version = "\(.*\)"$/\1/p' Cargo.toml | head -n 1)" ( export ANDROID_HOME="$sdk" BEAVER_ANDROID_STAGING_DIR="$(cd "$staging" && pwd)" export BEAVER_ANDROID_STAGING_DIR cd packaging/android # Gradle updates the last APK in place, and keeps the space of what it # drops (another run's ABIs, a library it replaces): start afresh. rm -f app/build/outputs/apk/debug/app-debug.apk ./gradlew assembleDebug -PbeaverVersionName="$version" \ -PbeaverVersionCode="$(date +%Y%m%d%H)" ) apk="dist/beaver_${version}_android_${abi}_${profile}.apk" mkdir -p dist cp packaging/android/app/build/outputs/apk/debug/app-debug.apk "$apk" echo "Built $apk" fi adb=adb if [ -x "$sdk/platform-tools/adb" ]; then adb="$sdk/platform-tools/adb" fi if [ "$install" = 1 ]; then "$adb" install -r -d "$apk" fi if [ "$run" = 1 ]; then "$adb" shell am force-stop "$app_id" "$adb" shell monkey -p "$app_id" -c android.intent.category.LAUNCHER 1 > /dev/null pid="" for _ in 1 2 3 4 5 6 7 8 9 10; do pid="$("$adb" shell pidof -s "$app_id" | tr -d '\r')" [ -n "$pid" ] && break sleep 0.5 done if [ -z "$pid" ]; then echo "build-android.sh: $app_id didn't start" >&2 exit 1 fi exec "$adb" logcat --pid="$pid" fi