Add JNI example.
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package st.wow.git.jni;
|
||||
|
||||
public class AClass {
|
||||
public int Num() {
|
||||
return 17;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,130 @@
|
||||
package main
|
||||
|
||||
/*
|
||||
#cgo LDFLAGS: -landroid
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <jni.h>
|
||||
#include <android/log.h>
|
||||
|
||||
static jobject gClassLoader;
|
||||
static jmethodID gFindClassMethod;
|
||||
|
||||
void
|
||||
SetLoader(JNIEnv* env, jobject context) {
|
||||
jclass cclass = (*env)->GetObjectClass(env, context);
|
||||
jmethodID gcl_id = (*env)->GetMethodID(env, cclass, "getClassLoader", "()Ljava/lang/ClassLoader;");
|
||||
jobject loader = (*env)->CallObjectMethod(env, context, gcl_id);
|
||||
gClassLoader = (*env)->NewGlobalRef(env, loader);
|
||||
jclass lclass = (*env)->GetObjectClass(env, loader);
|
||||
gFindClassMethod = (*env)->GetMethodID(env, lclass, "findClass", "(Ljava/lang/String;)Ljava/lang/Class;");
|
||||
}
|
||||
|
||||
jclass
|
||||
FindClass(JNIEnv* env, char* name) {
|
||||
jstring strClassName = (*env)->NewStringUTF(env, name);
|
||||
return (*env)->CallObjectMethod(env, gClassLoader, gFindClassMethod, strClassName);
|
||||
}
|
||||
|
||||
JNIEXPORT jobject
|
||||
CreateObject(JNIEnv* env, jclass cls) {
|
||||
jmethodID init = (*env)->GetMethodID(env, cls, "<init>", "()V");
|
||||
return (*env)->NewObject(env, cls, init);
|
||||
}
|
||||
|
||||
void CallVoidMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
|
||||
(*env)->CallVoidMethod(env, obj, methodID);
|
||||
}
|
||||
|
||||
jint CallIntMethod(JNIEnv *env, jobject obj, jmethodID methodID) {
|
||||
return (*env)->CallIntMethod(env, obj, methodID);
|
||||
}
|
||||
|
||||
jmethodID GetMethodID(JNIEnv *env, jclass clazz, const char *name, const char *sig) {
|
||||
return (*env)->GetMethodID(env, clazz, name, sig);
|
||||
}
|
||||
|
||||
jint GetEnv(JavaVM *vm, JNIEnv **env, jint version) {
|
||||
return (*vm)->GetEnv(vm, (void **)env, version);
|
||||
}
|
||||
|
||||
jint AttachCurrentThread(JavaVM *vm, JNIEnv **p_env, void *thr_args) {
|
||||
return (*vm)->AttachCurrentThread(vm, p_env, thr_args);
|
||||
}
|
||||
|
||||
jint DetachCurrentThread(JavaVM *vm) {
|
||||
return (*vm)->DetachCurrentThread(vm);
|
||||
}
|
||||
|
||||
*/
|
||||
import "C"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"runtime"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
var theJVM *C.JavaVM
|
||||
type JNIEnv = C.JNIEnv
|
||||
|
||||
func SetJVM(jvm, context uintptr) {
|
||||
log.Print("set theJVM")
|
||||
theJVM = (*C.JavaVM)(unsafe.Pointer(jvm))
|
||||
log.Printf("theJVM = %d", uintptr(unsafe.Pointer(theJVM)))
|
||||
RunInJVM(func(env *C.JNIEnv) {
|
||||
C.SetLoader(env, (C.jobject)(context))
|
||||
})
|
||||
}
|
||||
|
||||
func FindClass(env *C.JNIEnv, name string) C.jclass {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
return C.FindClass((*C.JNIEnv)(env), cname)
|
||||
}
|
||||
|
||||
func JniCallVoidMethod(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID) {
|
||||
C.CallVoidMethod(env, obj, methodID)
|
||||
}
|
||||
|
||||
func JniCallIntMethod(env *C.JNIEnv, obj C.jobject, methodID C.jmethodID) int {
|
||||
return (int)(C.CallIntMethod(env, obj, methodID))
|
||||
}
|
||||
|
||||
func JniGetMethodID(env *C.JNIEnv, cls C.jclass, name, sig string) C.jmethodID {
|
||||
cname := C.CString(name)
|
||||
defer C.free(unsafe.Pointer(cname))
|
||||
csig := C.CString(sig)
|
||||
defer C.free(unsafe.Pointer(csig))
|
||||
return C.GetMethodID(env, cls, cname, csig)
|
||||
}
|
||||
|
||||
func CreateObject(env *C.JNIEnv, cls C.jclass) C.jobject {
|
||||
return C.CreateObject(env, (C.jclass)(cls))
|
||||
}
|
||||
|
||||
func RunInJVM(f func(env *C.JNIEnv)) {
|
||||
runtime.LockOSThread()
|
||||
defer runtime.UnlockOSThread()
|
||||
var env *C.JNIEnv
|
||||
var detach bool
|
||||
log.Printf("RunInJVM(): theJVM = %d", uintptr(unsafe.Pointer(theJVM)))
|
||||
if res := C.GetEnv(theJVM, &env, C.JNI_VERSION_1_6); res != C.JNI_OK {
|
||||
if res != C.JNI_EDETACHED {
|
||||
panic(fmt.Errorf("JNI GetEnv failed with error %d", res))
|
||||
}
|
||||
if C.AttachCurrentThread(theJVM, &env, nil) != C.JNI_OK {
|
||||
panic(errors.New("runInJVM: AttachCurrentThread failed"))
|
||||
}
|
||||
detach = true
|
||||
}
|
||||
|
||||
if detach {
|
||||
defer func() {
|
||||
C.DetachCurrentThread(theJVM)
|
||||
}()
|
||||
}
|
||||
f(env)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
// +build darwin linux
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"gioui.org/app"
|
||||
"gioui.org/io/system"
|
||||
"gioui.org/layout"
|
||||
"gioui.org/unit"
|
||||
"gioui.org/widget/material"
|
||||
|
||||
_ "gioui.org/font/gofont"
|
||||
)
|
||||
|
||||
var (
|
||||
labchan chan string
|
||||
)
|
||||
|
||||
func main() {
|
||||
labchan = make(chan string)
|
||||
log.Print("Staring event loop")
|
||||
go eventloop()
|
||||
app.Main()
|
||||
log.Print("app.Main() returned")
|
||||
}
|
||||
|
||||
func diffInsets(x, y system.Insets) bool {
|
||||
return x.Top != y.Top ||
|
||||
x.Bottom != y.Bottom ||
|
||||
x.Left != y.Left ||
|
||||
x.Right != y.Right
|
||||
}
|
||||
|
||||
func eventloop() {
|
||||
w := app.NewWindow(
|
||||
app.Size(unit.Dp(400), unit.Dp(400)),
|
||||
app.Title("Hello"))
|
||||
th := material.NewTheme()
|
||||
gtx := &layout.Context{Queue: w.Queue()}
|
||||
|
||||
sysinset := &layout.Inset{}
|
||||
margin := layout.UniformInset(unit.Dp(10))
|
||||
labels := []material.Label{}
|
||||
list := &layout.List{Axis: layout.Vertical}
|
||||
|
||||
resetSysinset := func(x system.Insets) {
|
||||
sysinset.Top = x.Top
|
||||
sysinset.Bottom = x.Bottom
|
||||
sysinset.Left = x.Left
|
||||
sysinset.Right = x.Right
|
||||
}
|
||||
go func() {
|
||||
labchan <- "Starting"
|
||||
callJni()
|
||||
}()
|
||||
for {
|
||||
select {
|
||||
case x := <-labchan:
|
||||
labels = append(labels, th.Body1(x))
|
||||
case e := <-w.Events():
|
||||
switch e := e.(type) {
|
||||
case system.DestroyEvent:
|
||||
return
|
||||
case system.FrameEvent:
|
||||
gtx.Reset(e.Config, e.Size)
|
||||
resetSysinset(e.Insets)
|
||||
sysinset.Layout(gtx, func() {
|
||||
margin.Layout(gtx, func() {
|
||||
list.Layout(gtx, len(labels), func(i int) {
|
||||
labels[i].Layout(gtx)
|
||||
})
|
||||
})
|
||||
})
|
||||
e.Frame(gtx.Ops)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
//go:generate javac -bootclasspath $ANDROID_HOME/platforms/android-29/android.jar AClass.java
|
||||
//go:generate jar cf AClass.jar AClass.class
|
||||
//go:generate rm AClass.class
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"gioui.org/app"
|
||||
)
|
||||
|
||||
func callJni() {
|
||||
time.Sleep(time.Second/5)
|
||||
h := app.PlatformHandle()
|
||||
labchan <- fmt.Sprintf("JVM = %d", h.JVM)
|
||||
labchan <- fmt.Sprintf("Context = %d", h.Context)
|
||||
SetJVM(h.JVM, h.Context)
|
||||
|
||||
RunInJVM(func(env *JNIEnv) {
|
||||
log.Print("finding class")
|
||||
cls := FindClass(env, "st/wow/git/jni/AClass")
|
||||
log.Print("getting methodID")
|
||||
mth := JniGetMethodID(env, cls, "Num", "()I")
|
||||
if mth == nil {
|
||||
labchan <- "nil method"
|
||||
return
|
||||
}
|
||||
log.Print("creating object")
|
||||
inst := CreateObject(env, cls)
|
||||
log.Print("calling method")
|
||||
val := JniCallIntMethod(env, inst, mth)
|
||||
log.Print("got result")
|
||||
labchan <- fmt.Sprintf("val = %d", val)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
// +build !android
|
||||
|
||||
package main
|
||||
|
||||
func callJni() {
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user