Open hajimehoshi opened 6 years ago
https://github.com/golang/mobile/commit/56e3592fa7a0582e84d783579d2eb8e6114e0312 makes it impossible to access current_vm
, which means that calling JNI functions directly is no longer available... :-/
The above problem was solved https://github.com/golang/go/issues/26815
BTW, to use JNI in init()
function, I also tried to use JNI_GetCreatedJavaVMs, but failed due to the below message. Adding #cgo LDFLAGS: -ljvm
didn't help since libjvm
couldn't be found.
gomobile: go build -buildmode=c-shared -o=/var/folders/b7/w11sqqrx7kx6fqfbn24wdsmh0000gn/T/gomobile-work-277134993/android/src/main/jniLibs/armeabi-v7a/libgojni.so gobind failed: exit status 2
# github.com/hajimehoshi/ebiten/internal/devicescale
../ebiten/internal/devicescale/impl_android.go:25: error: undefined reference to 'JNI_GetCreatedJavaVMs'
clang60: error: linker command failed with exit code 1 (use -v to see invocation)
My understanding is that Seq.setContext
(gomobile-bind case) tries to initialize the context and invokes a Go function, but this invocation provokes all the init
function before setting the context. Then, in order to solve this issue, we'd need to set the context without Go. Is that correct?
CC @hyangah @timcooijmans
Sorry I missed this issue.
Yes that's the way it works at the moment. This change was needed because older versions of gomobile used reflection that was blacklisted by Android. Do you really have to use init() functions? Can't you use your own initX() functions?
You don't have to bind to an activity, you can also do it from a Service or even from the Application. But I guess that doesn't solve your problem.
Hi,
Do you really have to use init() functions? Can't you use your own initX() functions?
I might not, but other people might. I'm developing a library working with gomobile. If a function uses RunOnJVM, the function can be called from main and after but not from init, and this is not a good user experience.
In my library, JNI functions are called from Go side. JVM is accessed via a global C variable
current_vm
. I admit depending this variable is not a good way, but there is no other way to access JNI from Go. Actual code is https://github.com/hajimehoshi/ebiten/blob/master/internal/jni/jni_android.goI realized that
current_vm
is not initialized yet wheninit
functions are called. IIUC,current_vm
is initialized atSetCurrentContext
when the activity'sonCreate
is called, and thisonCreate
is called after allinit
functions are called and beforemain
is called.Wouldn't it be possible to make JVM available even from
init
functions? For gomobile-bind, asinit
functions are automatically called before callingSetCurrentContext
, then it might be impossible. If this is impossible, is there other way to call JNI functions from Go side?As for iOS, Objective-C binding via cgo is available and there is not such problem.