@@ -24,6 +24,12 @@ def __init__(self, DEBUG: bool, target: Target, **kwargs):
2424
2525 self .bindings_header = """package org.ldk.impl;
2626import org.ldk.enums.*;
27+ import java.io.File;
28+ import java.io.InputStream;
29+ import java.io.IOException;
30+ import java.nio.file.Files;
31+ import java.nio.file.Path;
32+ import java.nio.file.StandardCopyOption;
2733
2834public class bindings {
2935 public static class VecOrSliceDef {
@@ -35,7 +41,24 @@ def __init__(self, DEBUG: bool, target: Target, **kwargs):
3541 }
3642 }
3743 static {
38- System.loadLibrary(\" lightningjni\" );
44+ try {
45+ // Try to load natively first, this works on Android and in testing.
46+ System.loadLibrary(\" lightningjni\" );
47+ } catch (UnsatisfiedLinkError _ignored) {
48+ // Otherwise try to load from the library jar.
49+ File tmpdir = new File(System.getProperty("java.io.tmpdir"), "ldk-java-nativelib");
50+ tmpdir.mkdir(); // If it fails to create, assume it was there already
51+ tmpdir.deleteOnExit();
52+ String libname = "liblightningjni_" + System.getProperty("os.name").replaceAll(" ", "") +
53+ "-" + System.getProperty("os.arch").replaceAll(" ", "") + ".nativelib";
54+ try (InputStream is = bindings.class.getResourceAsStream("/" + libname)) {
55+ Path libpath = new File(tmpdir.toPath().toString(), "liblightningjni.so").toPath();
56+ Files.copy(is, libpath, StandardCopyOption.REPLACE_EXISTING);
57+ Runtime.getRuntime().load(libpath.toString());
58+ } catch (IOException e) {
59+ throw new IllegalArgumentException(e);
60+ }
61+ }
3962 init(java.lang.Enum.class, VecOrSliceDef.class);
4063 init_class_cache();
4164 if (!get_lib_version_string().equals(get_ldk_java_bindings_version()))
0 commit comments