go 语言生产动态库给java或者Android调用
一. 使用开源项目
https://gitee.com/aifuturewell/gojni
二. 使用
package main
import (
"fmt"
"unsafe"
"gitee.com/aifuturewell/gojni/java"
)
func main() {}
func init() {
java.OnLoad(func(reg java.Register) {
reg.WithClass("com.nk.Hello").
BindNative("nice", "void(java.lang.String[])", nice).
Done()
})
}
func nice(ss []string) {
fmt.Println(ss)
}
三. Java class
package com.nk;
public class Hello {
static {
System.loadLibrary("test");
}
public static void main(String[] args) {
nice(new String[] { "come", "from", "gojni" });
}
public static native void nice(String[] sss);
}
三.编译运行或者拷贝IDE
CGO_CFLAGS="-I$JAVA_HOME/include -I$JAVA_HOME/include/linux" go build -buildmode=c-shared -o libtest.so
LD_LIBRARY_PATH=. java com.nk.Hello
|