Skip to content

Instantly share code, notes, and snippets.

@tenntenn
Created February 19, 2016 02:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenntenn/0751c856ee363182ba76 to your computer and use it in GitHub Desktop.
Save tenntenn/0751c856ee363182ba76 to your computer and use it in GitHub Desktop.
// +build android
package main
/*
#cgo LDFLAGS: -landroid
#include <jni.h>
#include <stdlib.h>
JavaVM* current_vm;
jobject current_ctx;
// 参考:https://github.com/golang/mobile/blob/master/internal/mobileinit/ctx_android.go
char* _lockJNI(uintptr_t* envp, int* attachedp) {
JNIEnv* env;
if (current_vm == NULL) {
return "no current JVM";
}
*attachedp = 0;
switch ((*current_vm)->GetEnv(current_vm, (void**)&env, JNI_VERSION_1_6)) {
case JNI_OK:
break;
case JNI_EDETACHED:
if ((*current_vm)->AttachCurrentThread(current_vm, &env, 0) != 0) {
return "cannot attach to JVM";
}
*attachedp = 1;
break;
case JNI_EVERSION:
return "bad JNI version";
default:
return "unknown JNI error from GetEnv";
}
*envp = (uintptr_t)env;
return NULL;
}
char* _checkException(uintptr_t jnienv) {
jthrowable exc;
JNIEnv* env = (JNIEnv*)jnienv;
if (!(*env)->ExceptionCheck(env)) {
return NULL;
}
exc = (*env)->ExceptionOccurred(env);
(*env)->ExceptionClear(env);
jclass clazz = (*env)->FindClass(env, "java/lang/Throwable");
jmethodID toString = (*env)->GetMethodID(env, clazz, "toString", "()Ljava/lang/String;");
jobject msgStr = (*env)->CallObjectMethod(env, exc, toString);
return (char*)(*env)->GetStringUTFChars(env, msgStr, 0);
}
void _unlockJNI() {
(*current_vm)->DetachCurrentThread(current_vm);
}
void logd(JNIEnv* env, char *s) {
jclass clsLog = (*env)->FindClass(env, "android/util/Log");
jmethodID mthdD = (*env)->GetStaticMethodID(env, clsLog, "d", "(Ljava/lang/String;Ljava/lang/String;)I");
jstring tag = (*env)->NewStringUTF(env, "MyLog");
jstring message = (*env)->NewStringUTF(env, s);
(*env)->CallStaticIntMethod(env, clsLog, mthdD, tag, message);
}
*/
import "C"
import (
"errors"
"runtime"
"unsafe"
)
func runOnJVM(fn func(vm, env, ctx uintptr) error) error {
errch := make(chan error)
go func() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
env := C.uintptr_t(0)
attached := C.int(0)
if errStr := C._lockJNI(&env, &attached); errStr != nil {
errch <- errors.New(C.GoString(errStr))
return
}
if attached != 0 {
defer C._unlockJNI()
}
vm := uintptr(unsafe.Pointer(C.current_vm))
if err := fn(vm, uintptr(env), uintptr(C.current_ctx)); err != nil {
errch <- err
return
}
if exc := C._checkException(env); exc != nil {
errch <- errors.New(C.GoString(exc))
C.free(unsafe.Pointer(exc))
return
}
errch <- nil
}()
return <-errch
}
func logd(s string) {
runOnJVM(func(vm, jniEnv, ctx uintptr) error {
env := (*C.JNIEnv)(unsafe.Pointer(jniEnv))
cs := C.CString(s)
C.logd(env, cs)
C.free(unsafe.Pointer(cs))
return nil
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment