diff --git a/bind/java/Seq.java b/bind/java/Seq.java index f71eb37..5ac8250 100644 --- a/bind/java/Seq.java +++ b/bind/java/Seq.java @@ -15,6 +15,14 @@ import java.util.concurrent.Executors; // Used by automatically generated language bindings to talk to Go. public class Seq { static { + // Look for the shim class auto-generated by gomobile bind. + // Its only purpose is to call System.loadLibrary. + try { + Class.forName("go.LoadJNI"); + } catch (ClassNotFoundException e) { + // Ignore, assume the user will load JNI for it. + } + initSeq(); new Thread("GoSeq") { public void run() { Seq.receive(); } diff --git a/bind/java/SeqTest.java b/bind/java/SeqTest.java index ff8a724..fee922f 100644 --- a/bind/java/SeqTest.java +++ b/bind/java/SeqTest.java @@ -15,7 +15,6 @@ import go.testpkg.Testpkg; public class SeqTest extends AndroidTestCase { public SeqTest() { - System.loadLibrary("gojni"); } public void testAdd() { diff --git a/cmd/gomobile/bind.go b/cmd/gomobile/bind.go index 82246b8..4a98edd 100644 --- a/cmd/gomobile/bind.go +++ b/cmd/gomobile/bind.go @@ -150,8 +150,13 @@ func runBind(cmd *command) error { return err } + dst := filepath.Join(androidDir, "src/main/java/go/LoadJNI.java") + if err := ioutil.WriteFile(dst, []byte(loadSrc), 0664); err != nil { + return err + } + src := filepath.Join(repo, "bind/java/Seq.java") - dst := filepath.Join(androidDir, "src/main/java/go/Seq.java") + dst = filepath.Join(androidDir, "src/main/java/go/Seq.java") rm(dst) if err := symlink(src, dst); err != nil { return err @@ -160,6 +165,15 @@ func runBind(cmd *command) error { return buildAAR(androidDir, bindPkg) } +var loadSrc = `package go; + +public class LoadJNI { + static { + System.loadLibrary("gojni"); + } +} +` + type binder struct { files []*ast.File fset *token.FileSet