Hi guys, I have been trying to build static library for Android to be used in a Go binding go-sdl2 to produce a shared library that can be loaded by Android but haven’t been successful. I have almost no Android experience so I used the guide here as reference to create the following script that builds the SDL2 static library.
# build-android.sh inside SDL2-2.26.1 directory
mkdir build-android_arm && cd build-android_arm
export TARGET=armv7a-linux-androideabi
export NDK=/opt/android-ndk-r25b
export TOOLCHAIN=$NDK/toolchains/llvm/prebuilt/linux-x86_64
export API=21
export AR=$TOOLCHAIN/bin/llvm-ar
export CC=$TOOLCHAIN/bin/$TARGET$API-clang
export AS=$CC
export CXX=$TOOLCHAIN/bin/$TARGET$API-clang++
export LD=$TOOLCHAIN/bin/ld
export RANLIB=$TOOLCHAIN/bin/llvm-ranlib
export STRIP=$TOOLCHAIN/bin/llvm-strip
export LDFLAGS="-lOpenSLES"
../configure --host $TARGET
make -j`nproc`
However, it ran into an error below which I suppose is related to src/hidapi/android/hid.cpp
not being compiled? It seems to be included in the SDL2-2.26.1/Android.mk
file but I’m not sure how to make use of the make file.
...
CC build/SDL_virtualjoystick.lo
LTLINK build/libSDL2.la
ld: error: undefined symbol: PLATFORM_hid_init
>>> referenced by SDL_hidapi.c:1140 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1140)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_init)
>>> referenced by SDL_hidapi.c:1140 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1140)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_device_change_count)
>>> referenced by SDL_hidapi.c:1140 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1140)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_enumerate)
>>> referenced 2 more times
ld: error: undefined symbol: PLATFORM_hid_exit
>>> referenced by SDL_hidapi.c:1172 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1172)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_exit)
ld: error: undefined symbol: PLATFORM_hid_enumerate
>>> referenced by SDL_hidapi.c:1283 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1283)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_enumerate)
ld: error: undefined symbol: PLATFORM_hid_free_enumeration
>>> referenced by SDL_hidapi.c:1338 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1338)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_enumerate)
>>> referenced by SDL_hidapi.c:1322 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1322)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_enumerate)
ld: error: undefined symbol: PLATFORM_hid_open
>>> referenced by SDL_hidapi.c:1378 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1378)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_open)
ld: error: undefined symbol: PLATFORM_hid_open_path
>>> referenced by SDL_hidapi.c:1412 (/opt/tmp/SDL2-2.26.1/src/hidapi/SDL_hidapi.c:1412)
>>> build/.libs/SDL_hidapi.o:(SDL_hid_open_path)
ld: error: undefined symbol: PLATFORM_hid_write
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_read_timeout
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_read
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_set_nonblocking
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_send_feature_report
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_get_feature_report
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_close
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_get_manufacturer_string
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_get_product_string
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_get_serial_number_string
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_get_indexed_string
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
ld: error: undefined symbol: PLATFORM_hid_error
>>> referenced by SDL_hidapi.c
>>> build/.libs/SDL_hidapi.o:(PLATFORM_Backend)
clang-14: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:161: build/libSDL2.la] Error
The static libraries produced (libSDL2.a
and libSDL2main.a
) will be used in an example Go code when running builds in Go to produce shared library libexample.so
that will be loaded by Android code like this:
package com.example.android;
import org.libsdl.app.SDLActivity;
public class MyGame extends SDLActivity {
@Override
protected String[] getLibraries() {
return new String[] {
"example"
};
}
@Override
protected String getMainFunction() {
return "SDL_main";
}
}
I would be very grateful if there’s someone who could point me in the right direction as I have been trying for hours. I apologize if there’s some missing information but I can share more if needed!