Something went wrong. Try again.
Rewild Your Web forked from me.webbeef.org/browser.html
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192#!/bin/bash# SPDX-License-Identifier: AGPL-3.0-or-later## Build Beaver for Android.## Usage:# ./scripts/build-android.sh [--abi arm64|armv7|all] [--profile <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/<abi>/, stages the UI and Servo's resources in# target/android/assets/beaver/, then packages them with packaging/android into# dist/beaver_<version>_android_<abi>_<profile>.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.beaverabi=arm64profile=productionmode=buildgradle=1install=0run=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 shiftdone
case "$abi" in arm64) triples=aarch64-linux-android ;; armv7) triples=armv7-linux-androideabi ;; all) triples="aarch64-linux-android armv7-linux-androideabi" ;; *) usage ;;esacif [ "$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 2fi
case "$profile" in dev) profile_dir=debug ;; *) profile_dir="$profile" ;;esactarget_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 0fistage_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=adbif [ -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