feat(android): build native libs via CMake presets into jniLibs #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Android Build | |
| on: | |
| workflow_dispatch: | |
| pull_request: | |
| paths: | |
| # Android project itself, plus the shared C++ runtime it compiles from source. | |
| - "runtime/android/**" | |
| - "runtime/bindings/android/**" | |
| - "runtime/CMakeLists.txt" | |
| - "runtime/CMakePresets.json" | |
| - "runtime/processor/**" | |
| - "runtime/utils/**" | |
| - "runtime/cmake/**" | |
| - ".github/workflows/android.yml" | |
| jobs: | |
| build-android: | |
| name: build (android) | |
| runs-on: ubuntu-latest | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - uses: actions/setup-java@v4 | |
| with: | |
| distribution: temurin | |
| java-version: '21' | |
| # Caches downloaded Gradle distribution and dependencies across runs. | |
| - name: Setup Gradle | |
| uses: gradle/actions/setup-gradle@v4 | |
| with: | |
| build-root-directory: runtime/android | |
| # Puts sdkmanager on PATH, accepts licenses, installs platform + build-tools. | |
| - name: Setup Android SDK | |
| uses: android-actions/setup-android@v3 | |
| with: | |
| packages: 'platforms;android-34 build-tools;34.0.0' | |
| # sdkmanager has no bare "ndk" package — must install "ndk;X.Y.Z". Pick the | |
| # newest side-by-side NDK from the package list. | |
| - name: Install NDK (latest) | |
| run: | | |
| NDK_PKG=$(sdkmanager --list 2>/dev/null \ | |
| | grep -oE 'ndk;[0-9]+\.[0-9]+\.[0-9]+' \ | |
| | sort -t';' -k2 -V \ | |
| | tail -1) | |
| if [ -z "$NDK_PKG" ]; then | |
| echo "ERROR: no ndk;* package found in sdkmanager --list" >&2 | |
| sdkmanager --list 2>/dev/null | grep -i ndk | head -20 >&2 || true | |
| exit 1 | |
| fi | |
| echo "Installing $NDK_PKG" | |
| sdkmanager --install "$NDK_PKG" | |
| NDK_VER="${NDK_PKG#ndk;}" | |
| echo "Using NDK $NDK_VER" | |
| echo "ANDROID_NDK_HOME=$ANDROID_HOME/ndk/$NDK_VER" >> "$GITHUB_ENV" | |
| # Build the native lib with CMake presets and install it into jniLibs/<abi>/. | |
| # Gradle does NOT drive CMake; it only packages the prebuilt .so. | |
| - name: Build native lib | |
| working-directory: runtime | |
| run: | | |
| cmake --preset android-arm64-v8a | |
| cmake --build --preset android-arm64-v8a | |
| cmake --install build/aarch64-linux-android --component jni | |
| - uses: actions/setup-python@v5 | |
| with: | |
| python-version: '3.x' | |
| # Generate the four FST models straight into assets/ so the APK ships with | |
| # them (the app loads zh_tn_* and zh_itn_* at runtime). | |
| - name: Generate model assets | |
| run: | | |
| pip install pynini importlib_resources | |
| assets=runtime/android/app/src/main/assets | |
| python -m tn --language zh --overwrite_cache --cache_dir "$assets" | |
| python -m itn --language zh --overwrite_cache --cache_dir "$assets" | |
| - name: Build APK | |
| working-directory: runtime/android | |
| run: ./gradlew :app:assembleDebug -PabiFilters=arm64-v8a --no-daemon | |
| - name: Upload APK | |
| uses: actions/upload-artifact@v4 | |
| with: | |
| name: app-debug-arm64-v8a | |
| path: runtime/android/app/build/outputs/apk/debug/app-debug.apk | |
| if-no-files-found: error |