frostwire/frostwire-jlibtorrent

Unable to load so

Closed this issue · 3 comments

2021-11-17 18:29:35.261 22431-22431/com.xxxx.demo I/System.out: Trying jlibtorrent.<so|dylib>...
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: java.lang.UnsatisfiedLinkError: dlopen failed: library "libjlibtorrent.so" not found
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at java.lang.Runtime.loadLibrary0(Runtime.java:1087)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at java.lang.Runtime.loadLibrary0(Runtime.java:1008)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at java.lang.System.loadLibrary(System.java:1664)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.frostwire.jlibtorrent.swig.libtorrent_jni.(libtorrent_jni.java:25)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.frostwire.jlibtorrent.swig.libtorrent_jni.new_settings_pack__SWIG_0(Native Method)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.frostwire.jlibtorrent.swig.settings_pack.(settings_pack.java:40)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.apkmatrix.components.ultradownloader.torrentstream.TorrentUtils.defaultSettingsPack(TorrentUtils.kt:175)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.apkmatrix.components.ultradownloader.torrentstream.TorrentUtils.newSessionManager(TorrentUtils.kt:161)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.apkmatrix.components.ultradownloader.torrentstream.TorrentStream$startDownload$1.invokeSuspend(TorrentStream.kt:85)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at android.os.Handler.handleCallback(Handler.java:938)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:99)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at android.os.Looper.loop(Looper.java:223)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:7664)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at java.lang.reflect.Method.invoke(Native Method)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
2021-11-17 18:29:35.263 22431-22431/com.xxxx.demo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-11-17 18:29:35.266 22431-22431/com.xxxx.demo I/System.out: Loaded jlibtorrent-1.2.14.2

make sure you have both the jlibtorrent.jar and jlibtorrent-android-<architecture>-<version>.jar jars in your project and referenced by your gradle build configuration file.

For example, in your build.gradle

...
dependencies {
def jlibtorrent_version = '1.2.14.2'
    implementation files('libs/jlibtorrent-' + jlibtorrent_version + '.jar')

    // att free phones use this and crashes
    implementation files('libs/jlibtorrent-android-arm-' + jlibtorrent_version + '.jar')

    // oneplus hd1905 uses this
    implementation files('libs/jlibtorrent-android-arm64-' + jlibtorrent_version + '.jar')

    // used by android emulator on macos
    implementation files('libs/jlibtorrent-android-x86-' + jlibtorrent_version + '.jar')

    implementation files('libs/jlibtorrent-android-x86_64-' + jlibtorrent_version + '.jar')
}
...

have the jlibtorrent jars in a "libs" folder

I'm seeing this issue in 1.2.15.1 as well (testing with Android 11, arm64 device) on first call to the library upon app startup. It's followed by a log message

I/System.out: Loaded jlibtorrent-1.2.15.1

Subsequent calls don't show this error and download functions as normal. Seems like the '.so' is loaded lazily only if calls to it fail.

So, this is why, but not sure why it's trying to load jlibtorrent first.

static {
        try {
            String path = System.getProperty("jlibtorrent.jni.path", "");
            if ("".equals(path)) {
                try {
                    System.out.println("Trying jlibtorrent.<so|dylib>...");
                    System.loadLibrary("jlibtorrent");
                    System.loadLibrary("Loaded jlibtorrent.<so|dylib> (version=" +  jlibtorrentVersion() + ")");
                } catch (LinkageError e) {
                    // give it a try to the name with version
                    e.printStackTrace();
                    try {
                        System.loadLibrary("jlibtorrent-" + jlibtorrentVersion());
                        System.out.println("Loaded jlibtorrent-" + jlibtorrentVersion());
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            } else {
                System.load(path);
            }
        } catch (LinkageError e) {
            throw new LinkageError(
                "Look for your architecture binary instructions at: https://github.com/frostwire/frostwire-jlibtorrent", e);
        }
    }