This is a very quick-and-dirty guide to get SDL2 up and running with CHICKEN. However, there is unfortunately currently no SDL2 egg for CHICKEN (there is a SDL1 egg but that doesn't help us here - we want SDL2 since it has official Android support and it has a nice touch API).

To debug you can use adb logcat, but note that stdout is ignored by Android! This means CHICKEN-errors may get dumped on the floor.

Hopefully, the following script-guide may be helpful. Also, if things go wrong, perhaps this simpler guide might help.

Let's start off with the official SDL2 android template. We've only tested against SDL 2.0.0.

# put the official SDL2 tar under ~/opt/SDL2-2.0.0 or something
$ cd ~/projects
$ cp -a ~/opt/SDL2-2.0.0/android-project/ sdl2chick
$ cd sdl2chick/
$ cd jni/
$ ln -s ~/opt/SDL2-2.0.0/ SDL
$ cd ..
$ ndk-build 
# it should error with missing YourSourceHere.c. that's good!
$ android update project -p . -t android-14

This is just following SDL2 android instructions. Let's change the package name which must be done before building our cross-chicken:

	Modified   AndroidManifest.xml
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 27db418..20c80a1 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -5,3 +5,3 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-      package="org.libsdl.app"
+      package="com.adellica.sdl2-testapp"
       android:versionCode="1"
@@ -23,3 +23,3 @@
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
-        <activity android:name="SDLActivity"
+        <activity android:name="org.libsdl.app.SDLActivity"
                   android:label="@string/app_name">
# 'attach' a chicken to your android project
$ cd jni/
$ git clone https://github.com/chicken-mobile/android-chicken.git -b chicken.mk chicken
$ cd chicken
$ make # wait forever (clones chicken-core, bootstraps and cross-compiles it). 

this should build a cross-chicken for you under ./jni/chicken/target and host. note that you probably have to rm ./jni/chicken/target ./jni/chicken/host whenever your app package name is changed.

This build gives a cross-chicken which we'll be needing:

$ PATH=$PWD/jni/chicken/host/com.adellica.sdl2-testapp/bin/:$PATH

Next, let's do a bunch of stuff:

	New        Makefile
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..e773d7b
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,7 @@
+
+all:
+	android-csc -t jni/src/YourSourceHere.scm # generates YourSourceHere.c as requested by ndk-build
+	ndk-build
+	make -C jni/chicken libs # copies eggs/units with lib prefix (sigh ...), run this before ndk-build
+	ant clean debug
+	adb install -r bin/SDLActivity-debug.apk
	Modified   jni/src/Android.mk
diff --git a/jni/src/Android.mk b/jni/src/Android.mk
index 70ca7dc..1e40e31 100644
--- a/jni/src/Android.mk
+++ b/jni/src/Android.mk
@@ -14,3 +14,3 @@ LOCAL_SRC_FILES := $(SDL_PATH)/src/main/android/SDL_android_main.c \
 
-LOCAL_SHARED_LIBRARIES := SDL2
+LOCAL_SHARED_LIBRARIES := SDL2 chicken
 
	New        jni/src/YourSourceHere.scm
diff --git a/jni/src/YourSourceHere.scm b/jni/src/YourSourceHere.scm
new file mode 100644
index 0000000..7e4c845
--- /dev/null
+++ b/jni/src/YourSourceHere.scm
@@ -0,0 +1,8 @@
+(foreign-declare "#include <SDL.h>")
+
+(with-output-to-file "/sdcard/sdl2-log"
+        (lambda () (print "hello world!")))
+
+(return-to-host)
	Modified   res/values/strings.xml
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 9bce51c..dd478c7 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -2,3 +2,3 @@
 <resources>
-    <string name="app_name">SDL App</string>
+    <string name="app_name">Sokotar</string>
 </resources>
	Modified   src/org/libsdl/app/SDLActivity.java
diff --git a/src/org/libsdl/app/SDLActivity.java b/src/org/libsdl/app/SDLActivity.java
index ed6f4ef..42e7ea0 100644
--- a/src/org/libsdl/app/SDLActivity.java
+++ b/src/org/libsdl/app/SDLActivity.java
@@ -55,2 +55,3 @@ public class SDLActivity extends Activity {
         System.loadLibrary("SDL2");
+        System.loadLibrary("chicken");
         //System.loadLibrary("SDL2_image");

note that we've got a chicken-csc in there which has access to this apps' own egg-repository which we'll be using soon. all this will hopefully give you a running android app:

$ make
# wait and start app from phone when done
# it should exit
$ adb shell cat /sdcard/sdl2-log
hello-world!

it's not all that useful, but it has chicken and SDL2 in it so let's try and expose some of this! and all errors get dumped on the group because adb logcat doesn't show stderr output. to solve this, we add a couple tricks to YourSourceHere.scm:

(let ((o (let ((p (open-output-file "/sdcard/log")))
           (make-output-port (lambda (str) (display str p)
                                (flush-output p))
                             (lambda () (close-output-port p))))))
  (current-error-port o)
  (current-output-port o))

this should make any errors visible with adb shell cat /sdcard/log. the next step is to be able to load egg extensions. building these is surprisingly easy! all we need to to add android-chicken-install binary to our path and use it as a normal chicken-install:

$ android-chicken-install matchable nrepl

now re-run make and check that libnrepl.so is somewhere under ./libs. nrepl.so has been prefixed with lib because otherwise the android-build-process won't include it in the final apk file. to make it available at runtime, we need to hack chicken. so we hack chicken to make it include the prefix:

(include "./jni/chicken/find-extension.scm")

now, we should be able to use eggs like normal. your YourSourceHere.scm could now look like this:


(foreign-declare "#include <SDL.h>") ;; so that main() becomes SDL_main

(use ports) ;; for make-output-port and friends
(let ((o (let ((p (open-output-file "/sdcard/log")))
           (make-output-port (lambda (str) (display str p)
                                (flush-output p))
                             (lambda () (close-output-port p))))))
  (current-error-port o)
  (current-output-port o))

;; make (use sdl2) work properly (.so lib prefix path hack)
(include "./jni/chicken/find-extension.scm")

(use nrepl)
(nrepl 1234)

now build and run. you should see a black screen that "hangs". if it exits, try adb shell cat /sdcard/log and see what it says. if we're lucky, your nrepl is listening to connections:

$ make
$ adb forward tcp:1234 tcp:1234
$ nc localhost 1234
#;> (use posix)
#;> (with-input-from-pipe "ls -ld /data" read-string)
"drwxrwx--x system   system            2014-10-24 16:00 data\n"

I hope you've had fun so far because there's still a lot of paint to overcome. We now need SDL2 bindings to Chicken. These have been started, but not completed. These are a little tricky to build because they have the SDL2 third-party dependency which is inside the current app. The location of SDL2 can be supplied with CSC_OPTIONS.

$ SDLPRJ=$PWD # where your AndroidManifest.xml is
$ cd ~/projects
$ git clone https://github.com/Adellica/chicken-sdl2.git
$ cd chicken-sdl2
$ CSC_OPTIONS="-I$SDLPRJ/jni/SDL/include/ -L$SDLPRJ/libs/armeabi -lSDL2" android-chicken-install

now you can add this snippet to your YourSourceHere.scm file just before you start your nrepl:

(use sdl2)
(define w (sdl-create-window "hello world" 20 30 400 300 0))
(define r (sdl-create-renderer w -1 (+ SDL_RENDERER_ACCELERATED)))

(begin
  (sdl-set-render-draw-color r 0 0 0 0)
  (sdl-render-clear r)
  (sdl-set-render-draw-color r 255 0 255 255)
  (sdl-render-fill-rect r (make-sdl-rect 100 200 120 400))
  (sdl-render-present r))

if all goes well, you can run this and see a purple rectangle on your screen:

$ cd $SDLPRJ
$ make # start app after successful deploy