Skip to content

Tutorial: Building SFML for Android on Windows with Android Studio

4rcz3r edited this page Feb 23, 2023 · 4 revisions

1. Introduction

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.

2. Requirements

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.

3. Setup

  • 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 in SFML folder and armeabi-v7a, x86, arm64-v8a and x86_64 folders in build folder.
    Unpack extlibs and create build folder in every unpacked folder.

4. Building Extlibs

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 to SFML/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 to SFML/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 to SFML/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 and libFLAC++.a for Flac and libvorbis.a, libvorbisenc.a and libvorbisfile.a to SFML/extlibs/libs-android/arm64-v8a

Repeat these steps replacing arm64-v8a with x86_64

You can also replace c++_shared with c++_static.

5. Building SFML for Android

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.

6. My Android Studio SFML project template

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.

7. Some of my thoughts

  • 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 ;)

Author:

4rcz3r
Feel free to update and edit this text.

Clone this wiki locally