Pass JNIEnv or Context from Java to Go

Viewed 622

I've successfully written a go mobile library and used it from an Android Java app.

I've also successfully called a c function from JNI. This one has the JNIEnv pointer.

I'd like to pass the context/JNIEnv from Java to Go.

Here's where it seems to get complicated:

  1. Go Mobile does not seem to use JNIEnv at all.
  2. LoadJNI.java does have a context object and claims to pass it to the Go side. But there it isn't visible.
  3. I've been able to include jni.h using the "C" directive
  4. I can transfer the JNIEnv from JNI/C or store it there as a global variable but that's an ugly kludge.
  5. https://github.com/golang/go/issues/16876 talks about a proposal to do reverse binding but it's unclear what the status is.
  6. https://godoc.org/golang.org/x/mobile/cmd/gobind also talks about it but the following code snippet

import "Java/java/lang/System" t := System.CurrentTimeMillis()

returns

cannot find package "Java/java/lang/System" in any of:
/usr/local/go/src/Java/java/lang/System (from $GOROOT)
/Users/----/go/src/Java/java/lang/System (from $GOPATH)

In C I solved it with a compiler directive: #cgo CFLAGS: -I...

But I'm not sure how to do it for Java

In short, I'm now stuck and would appreciate help. Ideally, I'd just like to pass the object as a parameter, if possible. However, I'd settle for any reverse binding to call Java from Go.

1 Answers
Related