Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/mobile/cmd/gomobile: RunOnJVM is not available from init functions #25255

Open
hajimehoshi opened this issue May 4, 2018 · 2 comments

Comments

@hajimehoshi
Copy link
Contributor

commented May 4, 2018

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.go

I realized that current_vm is not initialized yet when init functions are called. IIUC, current_vm is initialized at SetCurrentContext when the activity's onCreate is called, and this onCreate is called after all init functions are called and before main is called.

Wouldn't it be possible to make JVM available even from init functions? For gomobile-bind, as init functions are automatically called before calling SetCurrentContext, 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.

@hajimehoshi

This comment has been minimized.

Copy link
Contributor Author

commented Aug 5, 2018

golang/mobile@56e3592 makes it impossible to access current_vm, which means that calling JNI functions directly is no longer available... :-/

@hajimehoshi

This comment has been minimized.

Copy link
Contributor Author

commented Oct 11, 2018

The above problem was solved #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)

@hajimehoshi hajimehoshi changed the title x/mobile/cmd/gomobile: JVM is not available from init functions x/mobile/cmd/gomobile: RunOnJVM is not available from init functions Jan 6, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.