-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Tutorial: Building SFML for Android
Cross-compiling SFML for Android can be a bit tricky at times, depending on your host system. Unfortunately, it's not as straightforward as compiling SFML for the actual host machine. This guide is meant to help you get started. It won't go too far into detail. You'll still have to attain at least some knowledge on your own on how to properly utilize the Android SDK/NDK.
These steps are experimental, feel free to edit this page and make improvements.
If you're using Windows, feel free to check out the scripts here, these will build SFML for you.
You can also try this Tutorial.
You can check out this tutorial. Though note that building SFML on Android with Visual Studio isn't maintained much nowadays. So if your goal is only to build a working .apk that you can send to someone, this tutorial covers that.
If you're using Linux, feel free to check out the scripts here, these will build SFML for you.
Before you can start, you'll need the proper development environment. Basically, you'll need the following things installed (some might be installed already, e.g. to build SFML for your host machine):
- CMake
- Git
-
Android SDK
- If not automatically set, set
ANDROID_HOMEto the SDK directory (e.g. on WindowsC:\Users\[Username]\AppData\Local\Android\Sdk)
- If not automatically set, set
- Android NDK
-
Gradle
- This requires Java (e.g. OpenJDK) and for the environment variable
JAVA_HOMEto be set
- This requires Java (e.g. OpenJDK) and for the environment variable
Before you can start, there are a few things to note, that will make the following steps easier.
-
Update your
PATHenvironment variable to include the following paths:[Path to CMake]/bin[Path to Git]/bin-
(not applicable for Android Studio >2.2)
[Path to SDK]/tools- On macOS: This can be found at
/Users/[Username]/Library/Android/sdk/tools
- On macOS: This can be found at
-
[Path to SDK]/platform-tools- On macOS: This can be found at
/Users/[Username]/Library/Android/sdk/platform-tools - On Windows: This can be found at
C:\Users\[Username]\AppData\Local\Android\Sdk\platform-tools
- On macOS: This can be found at
-
[Path to NDK]- On Windows when installed through Android Studio:
C:\Users\[Username]\AppData\Local\Android\Sdk\ndk\[Version]
- On Windows when installed through Android Studio:
Make sure to use backslashes (
\) for these paths on Windows.
On macOS, use the commandsudo nano /etc/pathsto editPATH. Make sure to escape any spaces with a backslash (\).
Note: You may encounter further problems if your directory paths contain spaces. It is recommended you avoid spaces in any of your directories.
Follow these steps to download, build and install SFML:
-
Open a console or terminal window and go to a directory where you'd like to place your SFML sources and build files, e.g.
/home/code. -
Clone the official SFML repository:
git clone https://github.com/SFML/SFML.git SFMLThis will create a sub directory
SFMLcontaining all build files. -
Enter the new sub directory:
cd SFML -
Create a build directory and enter it:
mkdir build && cd build -
You can repeat the following steps for all available architectures. Unfortunately, you can't build all targets for SFML at once. The following lines create an
armeabi-v7abuild. If you'd like to build for any other target, just replace all occurrences. Other valid targets would bemips, andx86.-
Create a subdirectory and enter it:
mkdir armeabi-v7a && cd armeabi-v7a -
Now invoke CMake. Make sure to pass all parameters:
cmake -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_NDK=/path/to/ndk -DCMAKE_ANDROID_ARCH_ABI=armeabi-v7a -DCMAKE_ANDROID_STL_TYPE=c++_static -DCMAKE_BUILD_TYPE=Debug ../.. -
If you've got multiple toolsets installed, like Visual Studio and MinGW, you might want to pick the type of project or makefile to create. You can do this by adding a parameter like
-G "MinGW Makefiles"(note the quotes). -
If the building process fails due to the error 'No toolchain for ABI 'armeabi' found in the NDK_PATH' then you need to update your CMake.
-
Important: It can be tricky to get this process to work with Visual Studio! I'd recommend you use MinGW's make (which is essentially GNU make). See the previous step to create the proper makefiles.
-
Wait for the process to complete. There might be a few warnings regarding the toolchain(s), but you shouldn't see any other warnings or error messages.
-
This will create a makefile or project for you, based on your current host system.
-
Use it to build and then install SFML. For example, under Linux you'd issue the following commands:
make && sudo make install -
If everything went fine, this should have copied the created binaries as well as header files and dependencies to your Android NDK's
sourcedirectory. -
You're now ready to use SFML in the NDK together with devices understanding the compiled target files (in this example
armeabi-v7a).
-
Go into the example directory:
cd SFML/examples/android
Add a "local.properties" file with the following contents, specifying the android SDK and NDK paths:
sdk.dir=/path/to/android-sdk
ndk.dir=/path/to/android-ndk
If you aren't using armeabi-v7a as ABI then you must also change the abiFilters property in app/build.gradle and the APP_ABI property in app/src/main/jni/Application.mk.
Now you should be able to build project with Gradle:
gradle build
If this results in an error stating "Could not open terminal for stdout: could not get termcap entry" then set the TERM variable to dumb (e.g. run TERM=dumb gradle build on Linux).
If all goes well then you can now install the apk to a device or emulator by running
gradle installDebug
Go into the example directory:
cd SFML/examples/android
If the Android SDK isn't set as an environment variable then add a "local.properties" file with the following contents, specifying the android SDK path:
sdk.dir=/path/to/android-sdk
Inside the app/build.gradle.kts file you may need to change NDK_VERSION and ARCH_ABI at the top of the file to match the values passed to CMake when building SFML. The value of NDK_VERSION needs to match the version inside CMAKE_ANDROID_NDK, while ARCH_ABI needs to be the same as the used CMAKE_ANDROID_ARCH_ABI value. If either of these are wrong (or if you forgot to install SFML), then running gradle in the next step will result in an error about SFMLConfig.cmake not being found.
Now you should be able to build the project with Gradle:
./gradlew assembleDebug
If this results in an error stating "Could not open terminal for stdout: could not get termcap entry" then set the TERM variable to dumb (e.g. run TERM=dumb ./gradlew build on Linux).
If all goes well then you can now install the apk to a device or emulator by running
./gradlew installDebug
This just means that there was an error when trying to build the app part of the SFML android example. Check the actual error message below that line.
This happens if the specified gradle version doesn't match. Change in examples/android/build.gradle the line classpath 'com.android.tools.build:gradle:3.0.0' to match at least classpath 'com.android.tools.build:gradle:4.0.0' or newer.
Make sure the follow environment variables are set to the SDK directory:
-
ANDROID_SDK_ROOT(e.g. on WindowsC:\Users\[Username]\AppData\Local\Android\Sdk) -
ANDROID_HOME(e.g. on WindowsC:\Users\[Username]\AppData\Local\Android\Sdk)
