android-go/examples/jni/os_android.go

40 lines
886 B
Go
Raw Normal View History

2019-10-18 14:40:35 -04:00
//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)
})
}