kohlschutter / junixsocket

Unix Domain Sockets in Java 7 and newer (AF_UNIX), AF_TIPC, AF_VSOCK, and more
Apache License 2.0
438 stars 114 forks source link

Add support for Android #123

Closed shenzhigang closed 1 year ago

shenzhigang commented 1 year ago

Describe the bug Error occurs when start the nanoHttpdServerDemo. No implementation found for void org.newsclub.net.unix.NativeUnixSocket.initServerImpl Version: 2.6.1 junixsocket-common-2.6.1, junixsocket-native-common-2.6.1, junixsocket-server-2.6.1 was imported.

To Reproduce Steps to reproduce the behavior:

try {
    SocketAddress addr = parseAddress(
            new String[]{}, 
            AFUNIXSocketAddress.of(new File("/data/local/tmp/junixsocket-http-server.sock"))
    );
    NanoHttpdServerDemo  nanoHttpdServerDemo = new NanoHttpdServerDemo (addr);
    nanoHttpdServerDemo.start(NanoHTTPD.SOCKET_READ_TIMEOUT, false);
} catch (IOException e) {
    e.printStackTrace();
    Logger.e(e, e.getMessage());
}

Output/Screenshots image

Environment (please complete the following information):

kohlschuetter commented 1 year ago

Thanks for reporting!

I haven't really looked much into supporting Android yet. By the looks of the screenshot, I think junixsocket can't find or can't load the native library.

Can you please provide an Android project setup that exposes this bug? This would speed up debugging.

shenzhigang commented 1 year ago

Here is a sample android project that exposes this bug. JunixsocketAndroidSample.zip

In addition, I tried to manually load libjunixsocket-national-2.6.1. so, and it says that libc cannot be found. It may be necessary to separately compile native library for Android.

kohlschuetter commented 1 year ago

Thanks!

I'm having some beginner's problems with Android Studio (I'm getting "INSTALL_FAILED_NO_MATCHING_ABIS: Failed to extract native libraries, res=-113'" with this project, which probably just means that my IDE/device setup is somewhat wrong. (I don't have much experience with Android Studio).

In addition, I tried to manually load libjunixsocket-national-2.6.1. so, and it says that libc cannot be found. It may be necessary to separately compile native library for Android.

Try the -nodeps library variant, which should not depend on libc. If that doesn't work, then you may have to compile the native library part (junixsocket-native/src/...) against whatever libc equivalent Android provides.

You could also try loading libc from Java (maybe something like System.loadLibrary("c") or System.load with absolute path to libc location)

shenzhigang commented 1 year ago

After further reading the source code, I found that on the Android platform, it is not only necessary to recompile native library. For example, java.rmi imported by AFUNIXSocketCredentials is not supported on Android. It is meaningful to make this excellent library compatible with Android. I will continue to work on this.

kohlschuetter commented 1 year ago

@shenzhigang Heads up: I now can compile and run selftest on an Android emulator. It will take a little longer to get it fully working, but we're getting there...

kohlschuetter commented 1 year ago

@shenzhigang The Android integration is getting close to being shippable.

Would you mind testing the following APK? Please unzip first junixsocket-selftest-android-2.7.0-SNAPSHOT.apk.zip

See junixsocket-selftest-android for the source code.

kohlschuetter commented 1 year ago

@shenzhigang Could you please verify that it works for you? Thanks for your help!

shenzhigang commented 1 year ago

@shenzhigang Could you please verify that it works for you? Thanks for your help!

Sorry to keep you waiting for so long. Here is the logs: logs.txt

kohlschuetter commented 1 year ago

@shenzhigang Great, thanks for providing the selftest logs! Looks like it worked :-)

kohlschuetter commented 1 year ago

junixsocket 2.7.0 has been released. Please verify with this version, and re-open if necessary. Thanks again for reporting!

Of note, when building junixsocket-selftest-android, please make sure to use commit 77be9933a0e93c03d5e06b8e968741d828b8391d or newer (where the dependency versions have been updated to 2.7.0)