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:
- A (more or less) functional SDL2 project
- The SDL2 Source
- CMake 3.8 to compile SDL 2
- Android Studio with NDK and CMake
- Bash, to run shell scripts (*.sh). The Bash that ships with the Git client is just fine.
- the
rustup
executable
Also check that your Rust project:
- Is built as a library.
- Has an SDL entry point inside
lib.rs
likeso that the Android/Java SDL wrapper can call your library.#[no_mangle] pub extern fn SDL_main(_argc: libc::c_int, _argv: *const *const libc::c_char) -> libc::c_int { my_actual_main_function(); return 0; }
- You have something like this in your
cargo.toml
:so that the library build is usable for C applications.[lib] name = "main" crate-type = ["cdylib"]
- 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
-
Install the build targets for Rust with
rustup target add aarch64-linux-android armv7-linux-androideabi i686-linux-android
-
Setup the file
~./cargo/config
to point to the NDK toochains by adding:[target.aarch64-linux-android] 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 " [target.armv7-linux-androideabi] 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 " [target.i686-linux-android] 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 theandroid26
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:
-
Download the SDL2 Source Code
-
Extract the contents into a folder within your Rust project
-
Navigate to the extraction folder and execute from a shell
ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk APP_PLATFORM=android-18
Setup the Android project
- Copy the “android-project” form the SDL2 source folder into your rust project (or where else you like). This will be our starting point.
- Create a symbolic link with the Windows CMD command:
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).
mklink /D *full path to your sdl2 source folder here*
- Enter
app/gradle.build
and comment out anyndkBuild
blocks and remove the comment oncmake
blocks - Enter
app/jni/CMakeLists.txt
and remove the last lineadd_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
- https://wiki.libsdl.org/Android
- https://medium.com/visly/rust-on-android-19f34a2fb43
- http://lazyfoo.net/tutorials/SDL/52_hello_mobile/android_windows/index.php
- https://lliwynd.blogspot.com/2018/05/rust-for-android-games-using-sdl2.html
- https://github.com/Rust-SDL2/rust-sdl2/blob/master/README.md
- https://github.com/Rust-SDL2/rust-sdl2/issues/648