Tutorial: Building SFML for Android on Windows with Android Studio
Since all tutorials about building SFML for Android on Windows I found were quite outdated or missing some points (e.g. compiling for arm64-v8a), I decided to write how I'm doing this.
This tutorial assumes installing whole Android Studio to reduce using cmd and make it more beginner-friendly.
Update
Now SFML provides extlibs for arm64-v8a and x86_64 so you can ommit Building Extlibs step.
First you need to install necessary tools. If some links will no longer work, try Google.
Versions I used writing this tutorial are in brackets.
- SFML master branch or 3.x or 2.6.x source code
-
Android Studio with installed latest:
- Android SDK Platform (33)
- Sources for Android (33)
- Android SDK Build-Tools (33)
- NDK (Side by side) (25)
- Android SDK Platform Tools (33)
- Cmake (3.24.2)
- Ninja
- External libraries for SFML on Android:
- Set PATH (Enviroment Variables) for current user:
- Cmake (C:\Program Files\CMake\bin)
- Ninja (I just moved the .exe to C:\Program Files\CMake\bin)
- NDK make.exe (%USERPROFILE%\AppData\Local\Android\Sdk\ndk\25.1.8937393\prebuilt\windows-x86_64\bin)
- NDK clang++.exe (%USERPROFILE%\AppData\Local\Android\Sdk\ndk\25.1.8937393\toolchains\llvm\prebuilt\windows-x86_64\bin)
- Unpack SFML and create
build
folder inSFML
folder andarmeabi-v7a
,x86
,arm64-v8a
andx86_64
folders inbuild
folder.
Unpack extlibs and createbuild
folder in every unpacked folder.
Update: SFML provides now all extlibs out of the box in SFML 2.6.x and 3.x branches.
Old: You can skip this step if you want to use only armeabi-v7a and x86 arch (but probably you won't).
Open cmd as administrator and go to build
folder.
- Freetype
cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=[Path to your NDK] (for me: C:/Users/[Your username]/AppData/Local/Android/Sdk/ndk/25.1.8937393) -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_MAKE_PROGRAM=make -G "MinGW Makefiles" -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=Release -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a ..
make
- Copy
libfreetype.a
toSFML/extlibs/libs-android/arm64-v8a
- OpenAL Soft
cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=[Path to your NDK] -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_MAKE_PROGRAM=make -G "MinGW Makefiles" -DCMAKE_C_FLAGS=-fPIC -DCMAKE_CXX_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=Release -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a ..
make
- Copy
libopenal.so
toSFML/extlibs/libs-android/arm64-v8a
- Ogg
cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=[Path to your NDK] -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_MAKE_PROGRAM=make -G "MinGW Makefiles" -DCMAKE_C_FLAGS=-fPIC -DCMAKE_BUILD_TYPE=Release -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a ..
make
-
make install
(!) - Copy
libogg.a
toSFML/extlibs/libs-android/arm64-v8a
- Flac and Vorbis
cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=[Path to your NDK] -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_MAKE_PROGRAM=make -G "MinGW Makefiles" -DCMAKE_C_FLAGS="-fPIC" -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_BUILD_TYPE=Release -DOGG_INCLUDE_DIR="C:/Program Files (x86)/libogg/include" -DOGG_LIBRARY="C:/Program Files (x86)/libogg/lib/libogg.a" -DCMAKE_ANDROID_ARCH_ABI=arm64-v8a ..
make
- Copy
libFLAC.a
andlibFLAC++.a
for Flac andlibvorbis.a
,libvorbisenc.a
andlibvorbisfile.a
toSFML/extlibs/libs-android/arm64-v8a
You can also replace c++_shared
with c++_static
.
Open cmd and go to SFML/build/[x86/armeabi-v7a/x86_64/arm64-v8a]
folder.
cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=[Path to your NDK] -DCMAKE_ANDROID_ARCH_ABI=[arch] -DCMAKE_ANDROID_STL_TYPE=c++_shared -DCMAKE_MAKE_PROGRAM=make -G "MinGW Makefiles" -DCMAKE_CXX_FLAGS="-fPIC" -DCMAKE_BUILD_TYPE=Debug ../..
make
make install
Repeat changing Debug
to Release
.
Replace [arch] with appropriate architecture same as folder name and repeat those steps for every architecture.
You can also replace c++_shared
with c++_static
.
Download my project template (IX 2022) and open it in Android Studio.
Find and replace all com.you.example
with your app id.
Find and replace all example
with your app name.
Sync Project with Gradle Files
and Make Project
.
Jni calls are explained in original SFML example, in my template that part is deleted.
In my template std::cout and std::err are redirected to Logcat with EXAMPLE_DEBUG signature.
- Emulators usually use x86/x86_64 architecture, your smartphone - armeabi-v7a/arm64-v8a.
- When updating NDK - remember to also update PATH.
- In TouchEvents use event.touch because sf::Touch::isDown can be false and Touch::getPosition may throw null pointer exception.
- Don't create sf::Texture as static class member. You can create e.g. static std::vectorsf::Texture and create textures in init function.
- Don't relly on saving things to file in destructors?
- Android Studio uses Relwithdebinfo in release build variant so I mapped it to Release in CMakeLists to import SFML libs properly.
- This tutorial seems to be already a little outdated ;)
4rcz3r
Feel free to update and edit this text.