From 5ddbd6154b0f553a8b747875366ae56c6347017d Mon Sep 17 00:00:00 2001 From: TheM14 Date: Sat, 31 Oct 2026 03:52:57 +0800 Subject: [PATCH] Build the JNI bridge for the Android app The app's MPVLib loads libplayer.so before libmpv, and mpv-android builds that bridge with ndk-build over app/src/main/jni, installing it and the prebuilt ffmpeg/mpv objects into app/src/main/libs/. The workflow only built the mpv target and then ran Gradle, so the APK shipped without libplayer.so and every playback died with UnsatisfiedLinkError. Run ndk-build the way buildscripts/scripts/mpv-android.sh does, and write ndk.properties, which the Gradle build needs to find the NDK for symbol stripping (the previous APK carried 134 MB of unstripped libraries). --- .github/workflows/build-android.yml | 50 +++++++++++++++++++++++++++++ fork/docs/android.md | 10 +++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-android.yml b/.github/workflows/build-android.yml index a5d2f62..ddbb49a 100644 --- a/.github/workflows/build-android.yml +++ b/.github/workflows/build-android.yml @@ -131,6 +131,30 @@ jobs: working-directory: android/buildscripts run: ./buildall.sh --arch arm64 -n mpv + - name: Build the JNI bridge (libplayer.so) + shell: bash + run: | + set -euo pipefail + cd "$GITHUB_WORKSPACE/android" + source buildscripts/include/path.sh + source buildscripts/include/depinfo.sh + ndk="$GITHUB_WORKSPACE/android/buildscripts/sdk/android-ndk-${v_ndk}" + export ANDROID_NDK_ROOT="$ndk" + export PATH="$ndk:$PATH" + # mpv-android's own build runs ndk-build over app/src/main/jni and + # installs libplayer.so plus the prebuilt ffmpeg/mpv objects into + # app/src/main/libs/, which is what the jniLibs symlink points + # at. The app loads libplayer.so before libmpv, so without this step + # every playback dies with UnsatisfiedLinkError. + PREFIX32="" PREFIX64="$(realpath buildscripts/prefix/arm64)" \ + PREFIX_X86="" PREFIX_X64="" \ + ndk-build -C app/src/main -j"$(nproc)" + # The Gradle build reads this to find the NDK for symbol stripping. + printf '%s\n' \ + "# This file is automatically written by the build scripts, and read using Gradle" \ + "ndkVersion=$v_ndk_n" "ndkRoot=$ANDROID_NDK_ROOT" > ndk.properties + ls -l app/src/main/libs/arm64-v8a + - name: Package Android shared libraries shell: bash run: | @@ -179,6 +203,8 @@ jobs: fi libs="$main/libs/$abi" mkdir -p "$libs" + echo "staged by the build scripts in libs/$abi:" + ls -l "$libs" 2>/dev/null || true # This build links the ffmpeg stack dynamically, so the app needs # every shared object from the cross-build prefix, not just libmpv.so # (libmpv's DT_NEEDED lists libav*/libsw*). @@ -292,6 +318,30 @@ jobs: "$apksigner" verify --print-certs "$apk" | head -2 fi done + # The player loads its JNI bridge (libplayer.so) before libmpv, and + # libmpv in turn needs the ffmpeg shared objects; shipping an ABI + # directory without them produces a crash on the first playback. + abi=arm64-v8a + needed="libplayer.so libmpv.so libjoc_core.so libc++_shared.so + libavcodec.so libavfilter.so libavformat.so libavutil.so + libavdevice.so libswresample.so libswscale.so" + for apk in "${selected[@]}"; do + packaged=$(unzip -l "$apk" | awk -v p="lib/$abi/" '$4 ~ p { print $4 }') + missing="" + for lib in $needed; do + case " $packaged " in *" lib/$abi/$lib "*) ;; *) missing="$missing $lib" ;; esac + done + if [ -n "$missing" ]; then + echo "::error::$apk misses$missing" + echo "$packaged" + echo "staged in app/src/main/libs/$abi:" + ls -l "$GITHUB_WORKSPACE/android/app/src/main/libs/$abi" 2>/dev/null | head -40 || true + echo "libplayer.so found anywhere in the tree:" + find "$GITHUB_WORKSPACE/android" -name 'libplayer.so' -print 2>/dev/null | head -10 || true + exit 1 + fi + echo "$apk: $(echo "$packaged" | wc -l) shared object(s) packaged" + done cp "${selected[@]}" dist/apk/ ls -lh dist/apk diff --git a/fork/docs/android.md b/fork/docs/android.md index 4ccdb72..b13eb2c 100644 --- a/fork/docs/android.md +++ b/fork/docs/android.md @@ -64,10 +64,12 @@ - `mpv-ad-joc-android-arm64-v8a`:`libmpv.so`、`libjoc_core.so`、`libc++_shared.so`, 给已经自己编好的 mpv 用; -- `mpv-joc-android-apk`:一个 `app-allstorage-universal-release.apk`,上面这三个库 - 已经打进 mpv-android 的 `libs/arm64-v8a/`,由 `assembleRelease` 打包并签名, - 直接可装。mpv-android 本身会出 `default` / `allstorage` × `arm64-v8a` / `universal` - 四个包,CI 只保留 allstorage 的 universal 版:权限最全,ABI 拆包也不会让工件变多。 +- `mpv-joc-android-apk`:一个 `app-allstorage-universal-release.apk`,由 `assembleRelease` + 打包并签名,直接可装。包里的 `libplayer.so` 由 `ndk-build` 现编(`app/src/main/jni`), + `libmpv.so` 和 ffmpeg 的 `libav*` / `libsw*` 一起放进 `libs/arm64-v8a/`;`ndk.properties` + 让 AGP 用 NDK 的 strip 处理这些库。mpv-android 本身会出 `default` / `allstorage` + × `arm64-v8a` / `universal` 四个包,CI 只保留 allstorage 的 universal 版:权限最全, + ABI 拆包也不会让工件变多。 签名用的是 CI 现场生成的临时 keystore(`-Pandroid.injected.signing.*`),只保证可安装, 不代表发布身份;要正式分发就把这段换成仓库 secret 里的 keystore。APK 里仍是原版