lightningdevkit / ldk-garbagecollected

LDK Bindings for Garbage-Collected Languages
Other
52 stars 27 forks source link

Failed to load LDK native library. (ldk-java jar from MavenCentral repository) #105

Closed takaya-imai closed 2 years ago

takaya-imai commented 2 years ago

I got an error message when launching a simple android app with ldk-java from MavenCentral repository. Do I need to put liblightningjni.so by hand?

Codes about ldk are very simple, it just calls Logger in org.ldk.structs. Building the app and installing to an emulator device are successful but launching is failed.

Enviroment

Error message

2022-06-30 14:02:39.650 6653-6653/com.example.test_ldk_java W/System.err: Failed to load LDK native library.
2022-06-30 14:02:39.650 6653-6653/com.example.test_ldk_java W/System.err: System LDK native library load failed with: java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.test_ldk_java-mjFe7aYsOrS-s4ZaofGTbA==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.test_ldk_java-mjFe7aYsOrS-s4ZaofGTbA==/lib/x86, /system/lib]]] couldn't find "liblightningjni.so"
2022-06-30 14:02:39.650 6653-6653/com.example.test_ldk_java W/System.err: Resource-based LDK native library load failed with: java.lang.NullPointerException
2022-06-30 14:02:39.650 6653-6653/com.example.test_ldk_java D/AndroidRuntime: Shutting down VM
2022-06-30 14:02:39.652 6653-6653/com.example.test_ldk_java E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.test_ldk_java, PID: 6653
    java.lang.ExceptionInInitializerError
        at org.ldk.impl.bindings.LDKLogger_new(Native Method)
        at org.ldk.structs.Logger.<init>(Logger.java:18)
        at org.ldk.structs.Logger.new_impl(Logger.java:36)
        at com.example.test_ldk_java.MainActivity.<init>(MainActivity.java:28)
        at java.lang.Class.newInstance(Native Method)
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
     Caused by: java.lang.IllegalArgumentException: java.lang.NullPointerException
        at org.ldk.impl.bindings.<clinit>(bindings.java:39)
        at org.ldk.impl.bindings.LDKLogger_new(Native Method) 
        at org.ldk.structs.Logger.<init>(Logger.java:18) 
        at org.ldk.structs.Logger.new_impl(Logger.java:36) 
        at com.example.test_ldk_java.MainActivity.<init>(MainActivity.java:28) 
        at java.lang.Class.newInstance(Native Method) 
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) 
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) 
        at android.app.Instrumentation.newActivity(Instrumentation.java:1215) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
     Caused by: java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:203)
        at java.nio.file.Files.copy(Files.java:2984)
        at org.ldk.impl.bindings.<clinit>(bindings.java:33)
        at org.ldk.impl.bindings.LDKLogger_new(Native Method) 
        at org.ldk.structs.Logger.<init>(Logger.java:18) 
        at org.ldk.structs.Logger.new_impl(Logger.java:36) 
        at com.example.test_ldk_java.MainActivity.<init>(MainActivity.java:28) 
        at java.lang.Class.newInstance(Native Method) 
        at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69) 
        at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45) 
        at android.app.Instrumentation.newActivity(Instrumentation.java:1215) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:193) 
        at android.app.ActivityThread.main(ActivityThread.java:6669) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

File Diff from activity base

$ diff build.gradle.org build.gradle
42a43,44
>
>     implementation 'org.lightningdevkit:ldk-java:0.0.106.0'
$ diff MainActivity.java.org MainActivity.java
20a21,22
> import org.ldk.structs.Logger;
>
25a28,29
>     final private Logger logger = Logger.new_impl(System.out::println)
>

Code Files I changed from activity base

plugins {
    id 'com.android.application'
}

android {
    compileSdk 32

    defaultConfig {
        applicationId "com.example.test_ldk_java"
        minSdk 22
        targetSdk 32
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    buildFeatures {
        viewBinding true
    }
}

dependencies {

    implementation 'androidx.appcompat:appcompat:1.4.2'
    implementation 'com.google.android.material:material:1.6.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
    implementation 'androidx.navigation:navigation-fragment:2.4.2'
    implementation 'androidx.navigation:navigation-ui:2.4.2'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

    implementation 'org.lightningdevkit:ldk-java:0.0.106.0'
}
package com.example.test_ldk_java;

import android.os.Bundle;

import com.google.android.material.snackbar.Snackbar;

import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.example.test_ldk_java.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;

import org.ldk.structs.Logger;

public class MainActivity extends AppCompatActivity {

    private AppBarConfiguration appBarConfiguration;
    private ActivityMainBinding binding;

    final private Logger logger = Logger.new_impl(System.out::println);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        setSupportActionBar(binding.toolbar);

        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        appBarConfiguration = new AppBarConfiguration.Builder(navController.getGraph()).build();
        NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

        binding.fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment_content_main);
        return NavigationUI.navigateUp(navController, appBarConfiguration)
                || super.onSupportNavigateUp();
    }
}
TheBlueMatt commented 2 years ago

We currently don't distribute android libraries via Maven, you have to use the AAR. We probably should eventually, but that's #92. Closing as dup.

takaya-imai commented 2 years ago

Oh, okay. Thanks.

takaya-imai commented 2 years ago

I download the following AAR from github to use on Android studio. However, the same error as the issue above is occurred at https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/src/main/java/org/ldk/impl/bindings.java#L33 .

It seems that the error relates to LD_LIBRARY_PATH but the top-level directory of this repository ? https://github.com/lightningdevkit/ldk-garbagecollected/blob/main/README.md?plain=1#L13

I set LD_LIBRARY_PATH as follows for trial but the error message is not changed. How do you use AAR to work well?

$ cd
$ git clone https://github.com/lightningdevkit/ldk-garbagecollected.git
$ cd ldk-garbagecollected/
$ pwd
/Users/xxx/ldk-garbagecollected
$ export LD_LIBRARY_PATH=/Users/xxx/ldk-garbagecollected
$ echo $LD_LIBRARY_PATH
/Users/xxx/ldk-garbagecollected
Screenshot 2022-08-29 16 08 50
TheBlueMatt commented 2 years ago

LD_LIBRARY_PATH isn't going to impact the simulator, so that shouldn't change anything. Can you paste the error you get when using the aar, as well as additional logcat context a few lines up (or is it exactly the same as the above?). What platform are you running for the simulator? To keep the AAR size within reason we currently don't include binaries for x86, only x86-64.