[Android] SDL_Init crashes when using the androidbuildlibs.sh libs

I used the “androidbuildlibs.sh” script of the build-scripts directory and compile with success the “libSDL2.so” and “libhidapi.so”. So I want use in my app android, I tried use SDL2 in the Common Lisp with ECL and cffi. I can load the libs and use the simple function to get SDL version:

(format nil "Using SDL Library Version: ~D.~D.~D~%"
      		sdl2-ffi:+sdl-major-version+
      		sdl2-ffi:+sdl-minor-version+
      		sdl2-ffi:+sdl-patchlevel+) => "Using SDL Library Version: 2.0.10"

But when I tried to init SDL with:

(cl:defconstant SDL-INIT-VIDEO #x00000020)
(cffi:foreign-funcall "SDL_Init" :uint32 SDL-INIT-VIDEO :int)
(when (< (cffi:foreign-funcall "SDL_Init" :uint32 SDL-INIT-VIDEO :int) 0)
       	(cffi:foreign-funcall "SDL_GetError" :string))

I get: “Application didn’t initialize properly, did you include SDL_main.h in the file containing your main() function?”

So, I tried:

(cl:defconstant SDL-INIT-VIDEO #x00000020)
(cffi:foreign-funcall "SDL_SetMainReady")
(cffi:foreign-funcall "SDL_Init" :uint32 SDL-INIT-VIDEO :int)
(when (< (cffi:foreign-funcall "SDL_Init" :uint32 SDL-INIT-VIDEO :int) 0)
       	(cffi:foreign-funcall "SDL_GetError" :string))

So, I get: “ERROR in eval”, this means that something can’t evaluate in the ECL, and I see that the problem is with “SDL_Init”. So, I think that the SDL2 need be called in one thread, but I don’t know for sure, because I think that the SDL2 already make it.

  1. Am I need or not init the SDL2 in one thread?

up I tried called “SDL_Init” into one thread created with pthread of C and doesn’t work. The app crashes.

  1. I need include some code Java to SDL2 to work?

up I tried compile my project with the SDL2 Java files of the android-project directory and I get:

src/org/libsdl/app/SDLActivity.java:84: error: cannot find symbol
    protected static Hashtable<Integer, PointerIcon> mCursors;
                                        ^
  symbol:   class PointerIcon
  location: class SDLActivity
src/org/libsdl/app/SDLControllerManager.java:422: error: cannot find symbol
                haptic.vib.vibrate(VibrationEffect.createOneShot(length, vibeValue));
                                   ^
  symbol:   variable VibrationEffect
  location: class SDLHapticHandler_API26
src/org/libsdl/app/SDLControllerManager.java:626: error: cannot find symbol
                    float x = event.getAxisValue(MotionEvent.AXIS_RELATIVE_X);
                                                            ^
  symbol:   variable AXIS_RELATIVE_X
  location: class MotionEvent
src/org/libsdl/app/SDLControllerManager.java:627: error: cannot find symbol
                    float y = event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y);
                                                            ^
  symbol:   variable AXIS_RELATIVE_Y
  location: class MotionEvent
src/org/libsdl/app/SDLControllerManager.java:657: error: cannot find symbol
            return event.getAxisValue(MotionEvent.AXIS_RELATIVE_X);
                                                 ^
  symbol:   variable AXIS_RELATIVE_X
  location: class MotionEvent
src/org/libsdl/app/SDLControllerManager.java:667: error: cannot find symbol
            return event.getAxisValue(MotionEvent.AXIS_RELATIVE_Y);
                                                 ^
  symbol:   variable AXIS_RELATIVE_Y
  location: class MotionEvent
src/org/libsdl/app/SDLControllerManager.java:713: error: cannot find symbol
            case InputDevice.SOURCE_MOUSE_RELATIVE:
                            ^
  symbol:   variable SOURCE_MOUSE_RELATIVE
  location: class InputDevice
src/org/libsdl/app/SDLControllerManager.java:755: error: cannot find symbol
                SDLActivity.getContentView().requestPointerCapture();
                                            ^
  symbol:   method requestPointerCapture()
  location: class View
src/org/libsdl/app/SDLControllerManager.java:758: error: cannot find symbol
                SDLActivity.getContentView().releasePointerCapture();
                                            ^
  symbol:   method releasePointerCapture()
  location: class View
src/org/libsdl/app/SDLControllerManager.java:773: error: cannot find symbol
            SDLActivity.getContentView().requestPointerCapture();
                                        ^
  symbol:   method requestPointerCapture()
  location: class View
src/org/libsdl/app/SDLActivity.java:175: error: cannot find symbol
        mCursors = new Hashtable<Integer, PointerIcon>();
                                          ^
  symbol:   class PointerIcon
  location: class SDLActivity
src/org/libsdl/app/SDLActivity.java:1489: error: cannot find symbol
                mCursors.put(mLastCursorID, PointerIcon.create(bitmap, hotSpotX, hotSpotY));
                                            ^
  symbol:   variable PointerIcon
  location: class SDLActivity
src/org/libsdl/app/SDLActivity.java:1561: error: cannot find symbol
                mSurface.setPointerIcon(PointerIcon.getSystemIcon(SDL.getContext(), cursor_type));
                                        ^
  symbol:   variable PointerIcon
  location: class SDLActivity
src/org/libsdl/app/SDLActivity.java:1776: error: cannot find symbol
                if (SDLActivity.mSingleton.isInMultiWindowMode()) {
                                          ^
  symbol:   method isInMultiWindowMode()
  location: variable mSingleton of type SDLActivity
Note: src/org/libsdl/app/SDLActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: src/org/libsdl/app/SDL.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
14 errors

I’m compiling with:

SDL2_ORG_DIRS=src/org/libsdl/app
javac -source 1.7 -target 1.7 \
      -bootclasspath "${JAVA_HOME}/jre/lib/rt.jar" \
      -classpath "${PLATFORM}/android.jar" \
      -d build/obj \
      build/gen/"${ORG_DIRS}"/R.java \
      "${SDL2_ORG_DIRS}"/HIDDevice.java \
      "${SDL2_ORG_DIRS}"/HIDDeviceBLESteamController.java \
      "${SDL2_ORG_DIRS}"/HIDDeviceManager.java \
      "${SDL2_ORG_DIRS}"/HIDDeviceUSB.java \
      "${SDL2_ORG_DIRS}"/SDL.java \
      "${SDL2_ORG_DIRS}"/SDLAudioManager.java \
      "${SDL2_ORG_DIRS}"/SDLControllerManager.java \
      "${SDL2_ORG_DIRS}"/SDLActivity.java \
      src/"${ORG_DIRS}"/EmbeddedCommonLisp.java \
      src/"${ORG_DIRS}"/HelloEclActivity.java

I also tried with Java8 and doens’t work.

I can now, I compiled with the platform-26 and working, because the PointerIcon and other things need Android API >= 24, see: https://developer.android.com/reference/android/view/PointerIcon. Now I tried:

(cl:defconstant SDL-INIT-VIDEO #x00000020)
(cffi:foreign-funcall "SDL_SetMainReady")
(cffi:foreign-funcall "SDL_Init" :uint32 SDL-INIT-VIDEO :int)

and “SDL_Init” returned 0, so, it were initialized correctly.

I tried too:

(cffi:defcstruct sdl-version
  (major :unsigned-char)
  (minor :unsigned-char)
  (patch :unsigned-char))

(cffi:with-foreign-object (x 'sdl-version)
      (cffi:foreign-funcall "SDL_GetVersion" :pointer x :void)
      (cffi:with-foreign-slots ((major minor patch) x sdl-version)
  	(format nil "SDL Version: ~D.~D.~D" major minor patch)))

And I get: “SDL Version: 2.0.10”, OK.

I had to comment the line wich load “main.so”, because the androidbuildlibs.sh doesn’t created one “main.so”, it created “main.a”,
so I used (cffi:foreign-funcall "SDL_SetMainReady"), Is it will bite me after or not?

Now I tried:

(cl:defconstant SDL-WINDOW-SHOWN #x00000004)

(let ((window (cffi:foreign-alloc :pointer))
      (renderer (cffi:foreign-alloc :pointer)))
  (cffi:foreign-funcall "SDL_CreateWindowAndRenderer"
  			:int 400
  			:int 300
  			:uint32 SDL-WINDOW-SHOWN
  			:pointer window
			:pointer renderer)
  (cffi:foreign-funcall "SDL_GetError" :string))

;; or

(cffi:foreign-funcall "SDL_CreateWindow"
		      :string "My window!"
		      :int 0
		      :int 0
		      :int 400
		      :int 300
		      :uint32 SDL-WINDOW-SHOWN
		      :pointer)
(cffi:foreign-funcall "SDL_GetError" :string)

And I get:

Could not fetch native window

Is this because I commented on loading “main.so”?

I can see that this error comes from here: https://github.com/davidsiaw/SDL2/blob/b90a1cdaa54f998e759debae15f2479de610918a/src/video/android/SDL_androidwindow.c#L74

Wich comes from that function: https://github.com/davidsiaw/SDL2/blob/b90a1cdaa54f998e759debae15f2479de610918a/src/core/android/SDL_android.c#L934

with adb logcat -s SDL I get:

01-15 16:01:06.483  3665  3665 V SDL     : Device: titan_umtsds
01-15 16:01:06.483  3665  3665 V SDL     : Model: XT1068
01-15 16:01:06.483  3665  3665 V SDL     : onCreate()
01-15 16:01:06.483  3665  3665 V SDL     : nativeSetupJNI()
01-15 16:01:06.484  3665  3665 V SDL     : AUDIO nativeSetupJNI()
01-15 16:01:06.484  3665  3665 V SDL     : CONTROLLER nativeSetupJNI()
01-15 16:01:40.853  3665  3665 V SDL     : setOrientation() orientation=6 width=400 height=300 resizable=false hint=
01-15 16:01:40.945  3665  3665 V SDL     : onStart()
01-15 16:01:40.947  3665  3665 V SDL     : onResume()
01-15 16:01:41.060  3665  3665 V SDL     : surfaceCreated()
01-15 16:01:41.060  3665  3665 V SDL     : surfaceChanged()
01-15 16:01:41.060  3665  3665 V SDL     : pixel format RGB_565
01-15 16:01:41.061  3665  3665 V SDL     : Window size: 1196x672
01-15 16:01:41.061  3665  3665 V SDL     : Device size: 1280x720
01-15 16:01:41.068  3665  4041 V SDL     : Running main function SDL_main from library /data/app/ecl.android.skeleton-2/lib/arm/libSDL2.so
01-15 16:01:41.068  3665  4041 V SDL     : nativeRunMain()
01-15 16:01:41.070  3665  4041 E SDL     : nativeRunMain(): Couldn't find function SDL_main in library /data/app/ecl.android.skeleton-2/lib/arm/libSDL2.so
01-15 16:01:41.070  3665  4041 V SDL     : Finished main function
01-15 16:01:41.088  3665  3665 V SDL     : surfaceDestroyed()
01-15 16:01:41.093  3665  3665 V SDL     : onWindowFocusChanged(): true
01-15 16:01:41.094  3665  3665 V SDL     : onWindowFocusChanged(): false
01-15 16:01:41.387  3665  3665 V SDL     : onPause()
01-15 16:01:41.656  3665  3665 V SDL     : onStop()
01-15 16:01:41.656  3665  3665 V SDL     : onDestroy()
01-15 16:01:41.687  3665  3665 E SDL     : SDLActivity thread ends (error=Could not fetch native window)

So, How to run SDL2 without the SDL_main function in the android?