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 · 5 comments
Open

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

hajimehoshi opened this issue May 4, 2018 · 5 comments
Labels
Milestone

Comments

@hajimehoshi
Copy link
Member

@hajimehoshi hajimehoshi 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
Copy link
Member Author

@hajimehoshi hajimehoshi 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
Copy link
Member Author

@hajimehoshi hajimehoshi 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
@hajimehoshi
Copy link
Member Author

@hajimehoshi hajimehoshi commented Aug 15, 2020

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

@timcooijmans
Copy link
Contributor

@timcooijmans timcooijmans commented Mar 31, 2021

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.

@hajimehoshi
Copy link
Member Author

@hajimehoshi hajimehoshi commented Mar 31, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants