Skip to content

Instantly share code, notes, and snippets.

@kristianlm
Last active January 2, 2016 01:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kristianlm/8229999 to your computer and use it in GitHub Desktop.
Save kristianlm/8229999 to your computer and use it in GitHub Desktop.
embedding chicken, extending the hello-jni example
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
HELLO_JNI_PATH := $(LOCAL_PATH)
LOCAL_LDLIBS := -llog
LOCAL_SRC_FILES := hello-jni.c
LOCAL_SHARED_LIBRARIES := chicken
include $(BUILD_SHARED_LIBRARY)
$(HELLO_JNI_PATH)/hello-jni.c: $(HELLO_JNI_PATH)/hello-jni.scm move-libs
csc -t $(HELLO_JNI_PATH)/hello-jni.scm
# run chicken's libs target which copies all eggs and units shared
# object into ./libs/armeabi with a "lib" prefix.
move-libs:
make -C $(HELLO_JNI_PATH)/chicken/ libs
include jni/chicken/Android.mk
;; this has saved my butt many times:
(let ((o (open-output-file "/sdcard/log")))
(current-error-port o)
(current-output-port o))
#>
#include <string.h>
#include <jni.h>
#include <chicken.h>
#include <android/log.h>
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "hello-jni", __VA_ARGS__))
int test_command();
jstring
Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
jobject thiz )
{
C_word res;
char foo[100];
CHICKEN_run(C_toplevel); // not the default toplevel anymore!
snprintf(foo, 100, "Evaluation result: %d", test_command());
return (*env)->NewStringUTF(env, foo);
}
<#
(define %##sys#find-extension ##sys#find-extension)
(define (##sys#find-extension p inc?)
(info (string-append "##sys#find-extension: looking for " p ".so or " (string-append "lib" p ".so")))
(or (%##sys#find-extension p inc?)
(%##sys#find-extension (string-append "lib" p) inc?)))
(define info (foreign-lambda* void ((c-string str)) "LOGI(str);"))
(define (square x) (* x x))
(define-external (test_command) int
(info (string-append "repository-path is " (repository-path)))
(square 2))
;; you should be able to install eggs with
;; android-chicken-install from your target-build/.../bin and do:
;; (use matchable), for example. best of luck.
(return-to-host)
# steps along these lines should get you in the right direction
$ cp -r /opt/android-ndk/samples/hello-jni .
# I think you need to "flush" your android project files too:
$ android update project -p . -t android-14
# now apply Andoird.mk and hello-jni.scm from this gist
$ cd hello-jni/jni
$ git clone https://github.com/chicken-mobile/android-chicken.git -b chicken.mk chicken
$ pwd
~/hello-jni/jni
$ ls
chicken src Android.mk # or something like it
$ cd chicken
$ make # wait forever
$ cd ../../
$ pwd
~/hello-jni
$ ndk-build && make -C jni/chicken libs && ant clean debug && adb install -r bin/*-debug.apk
@kristianlm
Copy link
Author

troubleshooting: if you see make[2]: /home/klm/projects/tmp/sdl2chick/jni/chicken/toolchain//bin//arm-linux-androideabi-gcc: Command not found, try to do rmdir toolchain/ and rerun make.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment