diff --git a/net/net.go b/net/net.go index 10fcff7..f753b65 100644 --- a/net/net.go +++ b/net/net.go @@ -7,8 +7,14 @@ package manet import ( "context" + "errors" "fmt" "net" + "runtime" + "strings" + + "git.wow.st/gmp/jni" + "golang.org/x/mobile/app" ma "github.com/multiformats/go-multiaddr" ) @@ -389,7 +395,15 @@ func WrapPacketConn(pc net.PacketConn) (PacketConn, error) { func InterfaceMultiaddrs() ([]ma.Multiaddr, error) { addrs, err := net.InterfaceAddrs() if err != nil { - return nil, err + if runtime.GOOS == "android" { + // To fix [x/mobile: Calling net.InterfaceAddrs() fails on Android SDK 30](https://github.com/golang/go/issues/40569) + addrs, err = getInterfaceAddrsFromAndroid() + if err != nil { + return nil, err + } + } else { + return nil, err + } } maddrs := make([]ma.Multiaddr, len(addrs)) @@ -402,6 +416,65 @@ func InterfaceMultiaddrs() ([]ma.Multiaddr, error) { return maddrs, nil } +func getInterfaceAddrsFromAndroid() ([]net.Addr, error) { + var ifaceString string + + // if use "golang.org/x/mobile/app", use app.RunOnJVM() below + err := app.RunOnJVM(func(vm, env, ctx uintptr) error { + jniEnv := jni.EnvFor(env) + + // cls := jni.FindClass(jniEnv, "YOUR/PACKAGE/NAME/CLASSNAME") + // m := jni.GetMethodID(jniEnv, cls, "getInterfacesAsString", "()Ljava/lang/String;") + // n, err := jni.CallStaticObjectMethod(jniEnv, cls, m) + + // above `YOUR.PACKAGE.NAME` `CLASSNAME.java` sometimes will cause strange [java.lang.ClassNotFoundException: Didn't find class on path: dexpathlist](https://stackoverflow.com/questions/22399572/java-lang-classnotfoundexception-didnt-find-class-on-path-dexpathlist) + // so use below `MainApplication.java` comes from `