Rust Android Examples

repository·main·Indexed 41 days ago

https://github.com/rust-mobile/rust-android-examples

A collection of minimal test applications demonstrating how to integrate Rust with Android entry points such as GameActivity and NativeActivity. Includes examples for JNI notifications, subclassing, and integrations with libraries like egui, winit, wgpu, Cpal, and Oboe.

Tokens
25.8K
Snippets
87
Records
97
Agent score
51%

What's inside Rust Android Examples

  1. How Winit, WGPU, and Egui integration works in agdk-winit-wgpu-egui

    main

    This example demonstrates a portable architecture using Winit and WGPU that can target Android (via GameActivity), Desktop, and Web.

    Instead of letting egui-wgpu manage the entire rendering state, this implementation shows how to integrate Egui as a UI overlay on top of an existing winit + wgpu application. This allows for more granular control over the rendering pipeline while still providing a demo UI.

  2. Setup Oculus Quest SDK for na-openxr-wgpu

    main

    To build this application for Oculus Quest, you must manually provide the Oculus OpenXR Mobile SDK library.

    1. Download the Oculus OpenXR Mobile SDK from the Oculus Developer portal.
    2. Unpack the zip file.
    3. Copy the appropriate libopenxr_loader.so file to your project's app/src/main/jniLibs/<abi> directory.

    For an arm64-v8a build, the command follows this pattern:

    cp path/to/ovr_openxr_mobile_sdk_42.0/OpenXR/Libs/Android/arm64-v8a/Debug/libopenxr_loader.so app/src/main/jniLibs/arm64-v8a
  3. Run the agdk-oboe test application

    main

    The agdk-oboe example is a minimal test application based on GameActivity. It runs a mainloop using android_activity::poll_events() and plays a sine wave audio test using the Oboe audio library.

    To run this application, you must configure your Android environment, install the necessary Rust targets and tools, build the project using cargo-ndk to generate JNI libraries, and then use Gradle to build and install the APK onto an Android device or emulator.

    export ANDROID_NDK_HOME="path/to/ndk"
    export ANDROID_HOME="path/to/sdk"
    
    rustup target add aarch64-linux-android
    cargo install cargo-ndk
    
    cargo ndk -t arm64-v8a -o app/src/main/jniLibs/ build
    ./gradlew build
    ./gradlew installDebug
    adb shell am start -n co.realfit.agdkoboe/.MainActivity
  4. Build and run the agdk-winit-wgpu-egui example on Android

    main

    To build and deploy this example to an Android device, you must have the Android SDK configured and the aarch64-linux-android target installed. The process uses cargo-ndk to compile the Rust code into JNI libraries and Gradle to build the Android application package.

    1. Set your ANDROID_HOME environment variable.
    2. Ensure the aarch64-linux-android target is added via rustup.
    3. Install cargo-ndk.
    4. Use cargo ndk to build the library and output it to the project's JNI directory.
    5. Use ./gradlew to build and install the app.
    6. Use adb to launch the MainActivity.
    export ANDROID_HOME="path/to/sdk"
    
    rustup target add aarch64-linux-android
    cargo install cargo-ndk
    
    cargo ndk -t arm64-v8a -o app/src/main/jniLibs/  build
    ./gradlew build
    ./gradlew installDebug
    adb shell am start -n com.github.rust_mobile.agdkwinitwgpuegui/.MainActivity
  5. Build and run the na-egui example on Android

    main

    The na-egui example demonstrates using NativeActivity integrated with egui, winit, and wgpu. To build and deploy this example to an Android device, follow these steps:

    1. Configure Environment Variables: Ensure ANDROID_NDK_HOME and ANDROID_HOME are set to your local NDK and SDK paths.
    2. Prepare Rust Toolchain: Install the aarch64-linux-android target and the cargo-ndk helper.
    3. Compile for Android: Use cargo ndk to build the Rust code for the arm64-v8a architecture and output the resulting libraries into the Android project's JNI directory.
    4. Build and Install: Use Gradle to build the Android application and install it on a connected device.
    5. Launch: Use adb to start the MainActivity.
    # 1. Set environment variables
    export ANDROID_NDK_HOME="path/to/ndk"
    export ANDROID_HOME="path/to/sdk"
    
    # 2. Setup Rust toolchain
    rustup target add aarch64-linux-android
    cargo install cargo-ndk
    
    # 3. Build Rust binaries for Android
    cargo ndk -t arm64-v8a -o app/src/main/jniLibs/ build
    
    # 4. Build and install the Android app
    ./gradlew build
    ./gradlew installDebug
    
    # 5. Launch the application
    adb shell am start -n com.github.rust_mobile.naegui/.MainActivity
  6. Build and run the na-winit-glutin example using cargo-apk

    main

    For a faster development loop, you can use cargo-apk to build and run the application directly. Ensure ANDROID_NDK_HOME and ANDROID_SDK_HOME are set, and that the aarch64-linux-android target is installed.

    export ANDROID_NDK_HOME="path/to/ndk"
    export ANDROID_SDK_HOME="path/to/sdk"
    
    rustup target add aarch64-linux-android
    cargo install cargo-apk
    
    cargo apk run
  7. Build for Oculus Quest

    main

    To build this application for Oculus Quest, you must provide the Oculus OpenXR Mobile SDK's loader library.

    1. Download the Oculus OpenXR Mobile SDK from the Oculus developer website.
    2. Unpack the SDK and copy the appropriate libopenxr_loader.so to your project's JNI directory under app/src/main/jniLibs/<abi>. For arm64-v8a, the path should be app/src/main/jniLibs/arm64-v8a/libopenxr_loader.so.
    3. Ensure your environment variables ANDROID_NDK_HOME and ANDROID_HOME are set.
    4. Install the required Rust targets and tools: aarch64-linux-android and cargo-ndk.
    5. Use cargo-ndk to build the library and then use Gradle to build and install the Android application.
    # 1. Prepare JNI directory and copy the loader library
    mkdir app/src/main/jniLibs/arm64-v8a
    cp ovr_openxr_mobile_sdk_53.0/OpenXR/Libs/Android/arm64-v8a/Debug/libopenxr_loader.so app/src/main/jniLibs/arm64-v8a/libopenxr_loader.so
    
    # 2. Set up environment
    export ANDROID_NDK_HOME="path/to/ndk"
    export ANDROID_HOME="path/to/sdk"
    
    # 3. Install targets and tools
    rustup target add aarch64-linux-android
    cargo install cargo-ndk
    
    # 4. Build and Install
    cargo ndk -t arm64-v8a -o app/src/main/jniLibs/ build --features=android
    ./gradlew build
    ./gradlew installDebug
  8. Build for Android using GameActivity, winit, and wgpu

    main

    To build and run this example on an Android device, you must have the Android SDK configured and the aarch64-linux-android target installed. The build process uses cargo-ndk to compile the Rust code into JNI libraries and Gradle to package the Android application.

    Prerequisites:

    • ANDROID_HOME environment variable set to your Android SDK path.
    • Rust target aarch64-linux-android installed via rustup.
    • cargo-ndk installed via cargo.

    Steps:

    1. Build the Rust binaries for the arm64-v8a architecture and output them to the Android project's JNI directory.
    2. Use Gradle to build the APK.
    3. Install the APK onto a connected device.
    4. Launch the application via adb.
    export ANDROID_HOME="path/to/sdk"
    
    rustup target add aarch64-linux-android
    cargo install cargo-ndk
    
    cargo ndk -t arm64-v8a -o app/src/main/jniLibs/  build
    ./gradlew build
    ./gradlew installDebug
    adb shell am start -n com.github.rust_mobile.agdkwinitwgpu/.MainActivity
  9. Build and run the agdk-cpal example

    main

    To run the agdk-cpal test application, which uses GameActivity to run a mainloop via android_activity::poll_events() and plays a sine wave using the Cpal audio library, follow these steps:

    1. Set Environment Variables: Ensure your Android NDK and SDK paths are exported.
    2. Prepare Rust Toolchain: Install the aarch64-linux-android target and the cargo-ndk helper.
    3. Build with cargo-ndk: Use cargo ndk to build the project for the arm64-v8a target, specifying API level 31 and outputting the JNI libraries to the Android project directory.
    4. Build Android App: Use the Gradle wrapper to build the Android application.
    5. Install and Launch: Install the debug build via Gradle and start the activity using adb.
    export ANDROID_NDK_HOME="path/to/ndk"
    export ANDROID_HOME="path/to/sdk"
    
    rustup target add aarch64-linux-android
    cargo install cargo-ndk
    
    cargo ndk -t arm64-v8a -P 31 -o app/src/main/jniLibs/  build
    ./gradlew build
    ./gradlew installDebug
    adb shell am start -n com.github.rust_mobile.agdkcpal/.MainActivity