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 · 6 comments
Open
Labels
mobile Android, iOS, and x/mobile
Milestone

Comments

@hajimehoshi
Copy link
Member

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.

@gopherbot gopherbot added this to the Unreleased milestone May 4, 2018
@gopherbot gopherbot added the mobile Android, iOS, and x/mobile label May 4, 2018
@hajimehoshi
Copy link
Member Author

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

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

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

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

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.

@hajimehoshi
Copy link
Member Author

https://github.com/golang/mobile/blob/35478a0c49da882188b186a3893d45be6ff74327/app/android.go#L72

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mobile Android, iOS, and x/mobile
Projects
None yet
Development

No branches or pull requests

3 participants