TL;DR Building An Android App With Rust And SDL2

TL;DR Building An Android App With Rust And SDL2

Guides on this topic are still pretty sparse, so I decided to write down the steps to make this work.

Assuming you’ve already built a functional SDL2 Program in Rust that uses OpenGL, this small guide will help you bring it onto Android. You will need:

Also check that your Rust project:

  • Is built as a library.
  • Has an SDL entry point inside like
    pub extern fn SDL_main(_argc: libc::c_int, _argv: *const *const libc::c_char) -> libc::c_int {
        return 0;
    so that the Android/Java SDL wrapper can call your library.
  • You have something like this in your cargo.toml:
    name = "main"
    crate-type = ["cdylib"]
    so that the library build is usable for C applications.
  • Is using an OpenGL version that is supported on Android, like OpenGL ES 2.0. And that the shaders are compatible with this version.

I’ve tested this guide on Windows 10 x64, but it should work on other systems quite similarly.

Prepare Android toolchains for Rust

  1. Install the build targets for Rust with

    rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android
  2. Setup the file ~./cargo/config to point to the NDK toochains by adding:

    ar = "*PATH OF YOUR ANDROID NDK DIR*\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\aarch64-linux-android-ar"
    linker ="*PATH OF YOUR ANDROID NDK DIR*\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\aarch64-linux-android26-clang.cmd "
    ar = "*PATH OF YOUR ANDROID NDK DIR*\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\arm-linux-androideabi-ar"
    linker = "*PATH OF YOUR ANDROID NDK DIR*\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\armv7a-linux-androideabi26-clang.cmd "
    ar = "*PATH OF YOUR ANDROID NDK DIR*\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\i686-linux-android-ar"
    linker = "*PATH OF YOUR ANDROID NDK DIR*\\toolchains\\llvm\\prebuilt\\windows-x86_64\\bin\\i686-linux-android26-clang.cmd "

    And add the full path to your Android NDK installation, typically C:\Users\*USERNAME*\AppData\Local\Android\sdk\ndk\*NDKVERSION*\. You can change the target SDK by changing the android26 to any supported SDK version. Keep in mind that the cmd ending is Windows specific.

Prepare SDL2 binaries

Now we need to build the libraries for Rust to build against:

  1. Download the SDL2 Source Code

  2. Extract the contents into a folder within your Rust project

  3. Navigate to the extraction folder and execute from a shell


Setup the Android project

  1. Copy the “android-project” form the SDL2 source folder into your rust project (or where else you like). This will be our starting point.
  2. Create a symbolic link with the Windows CMD command:
    mklink /D *full path to your sdl2 source folder here*
    from into the “android-project/app/jni” to the SDL2 source folder you’ve extracted. Or just copy the whole folder over (messy, not recommended).
  3. Enter app/ and comment out any ndkBuild blocks and remove the comment on cmake blocks
  4. Enter app/jni/CMakeLists.txt and remove the last line add_subdirectory(src), since we’re building the game libraries on our own.

Build the Rust binaries

Execute this script in your Rust root folder every time you change your project:

Then run the app from Android Studio and you’re done! On the first time, it should spend some time building SDL2, but any further rebuild will just rebuild the APK file.

For further troubleshooting with the Android Project itself, check this guide.

Sources / Futher links