From ca99808fd25b3906386e88ab07710ec1a674699f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Tue, 7 Sep 2021 03:03:02 -0700 Subject: [PATCH] Update AVD support - Support arm64 AVD images - Support setup on Windows Close #4637 --- build.py | 37 ++++++++++++++++++++++++++++++------- scripts/emulator.sh | 30 +++++++++++++----------------- 2 files changed, 43 insertions(+), 24 deletions(-) diff --git a/build.py b/build.py index d9578c17e..3cf303028 100755 --- a/build.py +++ b/build.py @@ -61,10 +61,12 @@ archs = ['armeabi-v7a', 'x86', 'arm64-v8a', 'x86_64'] default_targets = ['magisk', 'magiskinit', 'magiskboot', 'busybox'] support_targets = default_targets + ['magiskpolicy', 'resetprop', 'test'] -ndk_root = op.join(os.environ['ANDROID_SDK_ROOT'], 'ndk') +sdk_path = os.environ['ANDROID_SDK_ROOT'] +ndk_root = op.join(sdk_path, 'ndk') ndk_path = op.join(ndk_root, 'magisk') ndk_build = op.join(ndk_path, 'ndk-build') gradlew = op.join('.', 'gradlew' + ('.bat' if is_windows else '')) +adb_path = op.join(sdk_path, 'platform-tools', 'adb' + ('.exe' if is_windows else '')) # Global vars config = {} @@ -277,6 +279,9 @@ def build_binary(args): if props['Pkg.Revision'] != config['fullNdkVersion']: error('Incorrect NDK. Please install/upgrade NDK with "build.py ndk"') + if 'target' not in vars(args): + vars(args)['target'] = [] + if args.target: args.target = set(args.target) & set(support_targets) if not args.target: @@ -360,7 +365,7 @@ def build_app(args): def build_stub(args): - header('* Building stub APK') + header('* Building the stub app') build_apk(args, 'stub') @@ -444,8 +449,24 @@ def setup_ndk(args): vprint(f'Replaced {path}') +def setup_avd(args): + build_binary(args) + build_app(args) + + header('* Setting up emulator') + + abi = cmd_out([adb_path, 'shell', 'getprop', 'ro.product.cpu.abi']) + proc = execv([adb_path, 'push', f'native/out/{abi}/busybox', 'out/app-debug.apk', + 'scripts/emulator.sh', '/data/local/tmp']) + if proc.returncode != 0: + error('adb push failed!') + + proc = execv([adb_path, 'shell', 'sh', '/data/local/tmp/emulator.sh']) + if proc.returncode != 0: + error('emulator.sh failed!') + + def build_all(args): - vars(args)['target'] = [] build_stub(args) build_binary(args) build_app(args) @@ -474,14 +495,16 @@ binary_parser.set_defaults(func=build_binary) app_parser = subparsers.add_parser('app', help='build the Magisk app') app_parser.set_defaults(func=build_app) -stub_parser = subparsers.add_parser( - 'stub', help='build stub APK') +stub_parser = subparsers.add_parser('stub', help='build the stub app') stub_parser.set_defaults(func=build_stub) +avd_parser = subparsers.add_parser( + 'emulator', help='build and setup AVD for development') +avd_parser.set_defaults(func=setup_avd) + # Need to bind mount snet sources on top of stub folder # Note: source code for the snet extension is *NOT* public -snet_parser = subparsers.add_parser( - 'snet', help='build snet extension') +snet_parser = subparsers.add_parser('snet', help='build snet extension') snet_parser.set_defaults(func=build_snet) clean_parser = subparsers.add_parser('clean', help='cleanup') diff --git a/scripts/emulator.sh b/scripts/emulator.sh index b2569757a..e636aec33 100755 --- a/scripts/emulator.sh +++ b/scripts/emulator.sh @@ -3,7 +3,7 @@ # AVD Magisk Setup ##################################################################### # -# Support emulator ABI: x86_64 *only* +# Support emulator ABI: x86_64 and arm64 # Support API level: 23 - 31 (21 and 22 images do not have SELinux) # # This script will stop zygote, simulate the Magisk start up process @@ -15,15 +15,8 @@ # This only covers the "core" features of Magisk. Testing magiskinit # and magiskboot require additional setups that are not covered here. # -# Build everything by `./build.py all` before running this script. -# ##################################################################### -abort() { - echo "$@" - exit 1 -} - mount_sbin() { mount -t tmpfs -o 'mode=0755' tmpfs /sbin chcon u:object_r:rootfs:s0 /sbin @@ -31,10 +24,8 @@ mount_sbin() { if [ ! -f /system/build.prop ]; then # Running on PC - cd "$(dirname "$0")/.." - adb push native/out/x86_64/busybox out/app-debug.apk scripts/emulator.sh /data/local/tmp - adb shell sh /data/local/tmp/emulator.sh - exit 0 + echo 'Please run `./build.py emulator` instead of directly executing the script!' + exit 1 fi cd /data/local/tmp @@ -43,7 +34,7 @@ chmod 755 busybox if [ -z "$FIRST_STAGE" ]; then export FIRST_STAGE=1 export ASH_STANDALONE=1 - if [ `./busybox id -u` -ne 0 ]; then + if [ $(./busybox id -u) -ne 0 ]; then # Re-exec script with root exec /system/xbin/su 0 ./busybox sh $0 else @@ -55,7 +46,12 @@ fi pm install -r $(pwd)/app-debug.apk # Extract files from APK -unzip -oj app-debug.apk 'lib/x86_64/*' 'lib/x86/libmagisk32.so' -x 'lib/x86_64/busybox.so' +unzip -oj app-debug.apk 'assets/util_functions.sh' +. ./util_functions.sh + +api_level_arch_detect + +unzip -oj app-debug.apk "lib/$ABI/*" "lib/$ABI32/libmagisk32.so" -x "lib/$ABI/busybox.so" for file in lib*.so; do chmod 755 $file mv "$file" "${file:3:${#file}-6}" @@ -72,11 +68,11 @@ fi # SELinux stuffs ln -sf ./magiskinit magiskpolicy if [ -f /vendor/etc/selinux/precompiled_sepolicy ]; then - ./magiskpolicy --load /vendor/etc/selinux/precompiled_sepolicy --live --magisk + ./magiskpolicy --load /vendor/etc/selinux/precompiled_sepolicy --live --magisk 2>&1 elif [ -f /sepolicy ]; then - ./magiskpolicy --load /sepolicy --live --magisk + ./magiskpolicy --load /sepolicy --live --magisk 2>&1 else - ./magiskpolicy --live --magisk + ./magiskpolicy --live --magisk 2>&1 fi MAGISKTMP=/sbin