You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Install the latest SDK, run tools/bin/sdkmanager (or tools/android pre-2017) and install one API (>=12; >= 19 for SDL >= 2.0.8; >=26 if for SDL >= 2.0.9)
Configure your environment variables, e.g.:
<syntaxhighlight lang="bash">
PATH="/usr/src/android-ndk-rXXx:$PATH" # for 'ndk-build'
PATH="/usr/src/android-sdk-linux/tools:$PATH" # for 'android'
PATH="/usr/src/android-sdk-linux/platform-tools:$PATH" # for 'adb'
export ANDROID_HOME="/usr/src/android-sdk-linux" # for gradle
export ANDROID_NDK_HOME="/usr/src/android-ndk-rXXx" # for gradle
</syntaxhighlight>
Simple builds
SDL wrapper for simple programs
Compile a sample app (calls ndk-build):
<syntaxhighlight lang="bash">
cd /usr/src/SDL2/build-scripts/
./androidbuild.sh org.libsdl.testgles ../test/testgles.c
</syntaxhighlight>
Follow the instructions to install on your device:
<syntaxhighlight lang="bash">
cd /usr/src/SDL2/build/org.libsdl.testgles/
ant debug install # SDL <= 2.0.7
./gradlew installDebug # SDL >= 2.0.8
</syntaxhighlight>
use OpenJDK 8: execute sudo update-alternatives --config java and select jdk-8 as default; or use JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 ./gradlew
fixed in 2.0.9: in /android-project/build.gradle change (in BOTH places in the file code appears) from
javax/xml/bind/annotation/XmlSchema, Could not initialize class com.android.sdklib.repository.AndroidSdkHandler: check the Android Gradle Plugin version in /android-project/build.gradle, e.g.
classpath 'com.android.tools.build:gradle:3.1.0'
You can customize the Gradle version in /android-project/gradle/wrapper/gradle-wrapper.properties:
ABIs [x86_64, arm64-v8a] are not supported for platform. Supported ABIs are [armeabi, armeabi-v7a, x86, mips]: upgrade to NDK >= 10
Using ant (SDL <= 2.0.7): edit build-scripts/androidbuild.sh, find the $ANDROID update project line, and add --target android-XX to it (replace XX with your installed API number)
TODO: check how we can use the distro's gradle instead of executing stuff from the Internet - apt install gradle libgradle-android-plugin-java
SDL wrapper + SDL_image NDK module
Let's modify SDL2_image/showimage.c to show a simple embedded image (e.g. XPM).
Note: no need to add System.loadLibrary calls in SDLActivity.java, your application will be linked to them and Android's ld-linux loads them automatically.
Install SDL in a GCC toolchain
Now:
Copy the NDK into a traditional GCC toolchain (leave android-14 as-is):
<syntaxhighlight lang="bash">
cd /usr/src/SDL2/build/org.libsdl/
for i in libs/armeabi/*; do ln -nfs $(pwd)/$i $NDK_STANDALONE/sysroot/usr/lib/; done
mkdir $NDK_STANDALONE/sysroot/usr/include/SDL2/
\cp jni/SDL/include/* $NDK_STANDALONE/sysroot/usr/include/SDL2/
\cp jni/*/SDL*.h $NDK_STANDALONE/sysroot/usr/include/SDL2/
</syntaxhighlight>
Install pkg-config and install a host-triplet-prefixed symlink in the PATH (auto-detected by autoconf):
<syntaxhighlight lang="bash">
VERSION=0.9.12
cd /usr/src/
wget http://rabbit.dereferenced.org/~nenolod/distfiles/pkgconf-$VERSION.tar.gz
tar xf pkgconf-$VERSION.tar.gz
cd pkgconf-$VERSION/
mkdir native-android/ && cd native-android/
../configure --prefix=$NDK_STANDALONE/sysroot/usr
make -j$(nproc)
make install
ln -s ../sysroot/usr/bin/pkgconf $NDK_STANDALONE/bin/arm-linux-androideabi-pkg-config
mkdir $NDK_STANDALONE/sysroot/usr/lib/pkgconfig/
</syntaxhighlight>
You can add any other libraries (e.g.: SDL2_gfx, freetype, gettext, gmp...) using commands like:
<syntaxhighlight lang="bash">
mkdir cross-android/ && cd cross-android/
../configure --host=arm-linux-androideabi --prefix=$NDK_STANDALONE/sysroot/usr \
--with-some-option --enable-another-option \
--disable-shared
make -j$(nproc)
make install
</syntaxhighlight>
Static builds (--disable-shared) are recommended for simplicity (no additional .so to declare).
<syntaxhighlight lang="bash">
Example with SDL2_gfx:
VERSION=1.0.3
wget http://www.ferzkopp.net/Software/SDL2_gfx/SDL2_gfx-$VERSION.tar.gz
tar xf SDL2_gfx-$VERSION.tar.gz
mv SDL2_gfx-$VERSION/ SDL2_gfx/
cd SDL2_gfx/
mkdir cross-android/ && cd cross-android/
../configure --host=arm-linux-androideabi --prefix=$NDK_STANDALONE/sysroot/usr \
--disable-shared --disable-mmx
make -j$(nproc)
make install
</syntaxhighlight>
You can compile YOUR application using this technique, with some more steps to tell Android how to run it using JNI.
Build your autotools app
First, prepare an Android project:
Copy and adapt the /usr/src/SDL2/android-project skeleton as explained in README-android.md. You can leave it as-is in a first step.
Make links to the SDL binaries as well:
<syntaxhighlight lang="bash">
mkdir -p libs/armeabi/
for i in /usr/src/SDL2/build/org.libsdl/libs/armeabi/*; do ln -nfs $i libs/armeabi/; done
</syntaxhighlight>
Make your project Android-aware:
Add /usr/src/SDL2/src/main/android/SDL_android_main.c in your project (comment out the line referencing "SDL_internal.h"). Compile it as C (not C++).
In your configure.ac, detect Android:
<syntaxhighlight lang="c">
AM_CONDITIONAL(ANDROID, test "$host" = "arm-unknown-linux-androideabi")
</syntaxhighlight>
In your Makefile.am, tell Automake you'll build executables as libraries, using something like:
Now you can install your pre-built binaries and build the Android project:
Copy your program in android-project/libs/armeabi/libmain.so.
Build your Android .apk:
<syntaxhighlight lang="bash">
android update project --name your_app --path . --target android-XX
ant debug
ant installd
</syntaxhighlight>
You can run the application remotely:
<syntaxhighlight lang="bash">
adb shell am start -a android.intenon.MAIN -n org.libsdl.app/org.libsdl.app.SDLActivity # replace with your app package
</syntaxhighlight>
Your SDL2 Android app is running!
Build your CMake app
(Work In Progress)
You can use our Android GCC toolchain using a simple toolchain file:
<syntaxhighlight lang="cmake">
You then call CMake like this:
<syntaxhighlight lang="bash">
PATH=$NDK_STANDALONE/bin:$PATH
cmake \
-D CMAKE_TOOLCHAIN_FILE=../android_toolchain.cmake \
...
</syntaxhighlight>
Troubleshootings
If ant installd categorically refuses to install with Failure [INSTALL_FAILED_INSUFFICIENT_STORAGE], even if you have free local storage, that may mean anything. Check logcat first:
<syntaxhighlight lang="bash">
adb logcat
</syntaxhighlight>
If the error logs are not helpful (likely ;')) try locating all past traces of the application:
<syntaxhighlight lang="bash">
find / -name "org...."
</syntaxhighlight>
and remove them all.
If the problem persists, you may try installing on the SD card:
<syntaxhighlight lang="bash">
adb install -s bin/app-debug.apk
</syntaxhighlight>
If you get in your logcat:
SDL: Couldn't locate Java callbacks, check that they're named and typed correctly
this probably means your SDLActivity.java is out-of-sync with your libSDL2.so.