Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
takaya-imai opened this issue Jun 30, 2022 · 4 comments

Comments

@takaya-imai
Copy link

takaya-imai commented Jun 30, 2022

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

  • android activity base
    • Basic Activity

Android 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

  • build.gradle
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'
}
  • MainActivity.java
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
Copy link
Collaborator

TheBlueMatt commented Jun 30, 2022

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
Copy link
Author

Oh, okay. Thanks.

@takaya-imai
Copy link
Author

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 .

  • LDK-release.aar
    • 0.0.108.0
    • 0.0.110.2

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
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants