java.lang.UnsatisfiedLinkError couldn't find "libLiteCoreJNI.so" when initializing CBLite

For the past couple of weeks, I have noticed that my init() call is causing my application to crash roughly 50% of the time; if it does crash the first time, then simply restarting the app gets it to work…

I have not touched this method for quite some time now.

Here is the entire method:

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

      
        CouchbaseLite.init(getApplicationContext());//error occurs here

        DatabaseConfiguration config = new DatabaseConfiguration();

        try {
            Log.d("DEBUG", "searching for/creating DB...");
            database = new Database("flash_me_db", config);
            if (database.getIndexes().size() == 0){
                database.createIndex(CardKeyName.ENGLISH_KEY.getValue(),
                        IndexBuilder.valueIndex(ValueIndexItem.property(CardKeyName.ENGLISH_KEY.getValue())));
                database.createIndex(CardKeyName.SWEDISH_KEY.getValue(),
                        IndexBuilder.valueIndex(ValueIndexItem.property(CardKeyName.SWEDISH_KEY.getValue())));
                database.createIndex(CardKeyName.TYPE_KEY.getValue(),
                        IndexBuilder.valueIndex(ValueIndexItem.property(CardKeyName.TYPE_KEY.getValue())));
            }
            Log.d("DEBUG", "created DB in path: " + database.getPath());

        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
    }

Here is the full stacktrace:

2021-01-07 19:49:43.968 21564-21564/com.boredofnothing.flashcard E/System: Unable to open zip file: /data/app/com.boredofnothing.flashcard-2/base.apk
2021-01-07 19:49:43.969 21564-21564/com.boredofnothing.flashcard E/System: java.util.zip.ZipException: File too short to be a zip file: 0
        at java.util.zip.ZipFile.<init>(ZipFile.java:208)
        at java.util.zip.ZipFile.<init>(ZipFile.java:147)
        at java.util.jar.JarFile.<init>(JarFile.java:161)
        at java.util.jar.JarFile.<init>(JarFile.java:98)
        at libcore.io.ClassPathURLStreamHandler.<init>(ClassPathURLStreamHandler.java:47)
        at dalvik.system.DexPathList$Element.maybeInit(DexPathList.java:532)
        at dalvik.system.DexPathList$Element.findNativeLibrary(DexPathList.java:546)
        at dalvik.system.DexPathList.findLibrary(DexPathList.java:480)
        at dalvik.system.BaseDexClassLoader.findLibrary(BaseDexClassLoader.java:84)
        at java.lang.Runtime.loadLibrary0(Runtime.java:966)
        at java.lang.System.loadLibrary(System.java:1567)
        at com.couchbase.lite.CouchbaseLite.init(CouchbaseLite.java:63)
        at com.boredofnothing.flashcard.MainActivity.onCreate(MainActivity.java:100)
        at android.app.Activity.performCreate(Activity.java:6955)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
2021-01-07 19:49:43.969 21564-21564/com.boredofnothing.flashcard D/AndroidRuntime: Shutting down VM
2021-01-07 19:49:43.970 21564-21564/com.boredofnothing.flashcard E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.boredofnothing.flashcard, PID: 21564
    java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.boredofnothing.flashcard-2/base.apk"],nativeLibraryDirectories=[/data/app/com.boredofnothing.flashcard-2/lib/arm64, /data/app/com.boredofnothing.flashcard-2/base.apk!/lib/arm64-v8a, /system/lib64, /vendor/lib64]]] couldn't find "libLiteCoreJNI.so"
        at java.lang.Runtime.loadLibrary0(Runtime.java:972)
        at java.lang.System.loadLibrary(System.java:1567)
        at com.couchbase.lite.CouchbaseLite.init(CouchbaseLite.java:63)
        at com.boredofnothing.flashcard.MainActivity.onCreate(MainActivity.java:100)
        at android.app.Activity.performCreate(Activity.java:6955)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3045)
        at android.app.ActivityThread.-wrap14(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1642)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6776)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)

I am using couchbase-lite-android:2.7.0

That’s a weird one. As you probably are aware, most of the Couchbase product is written in C/C++. The “init” method loads the native library. It is, apparently, failing to do so, here, because it sees the apk as being 0-length. That is pretty weird. It is even weirder that it would happen intermittently.

The only thing I can think of is that, perhaps the apk is being corrupted, somehow?

FWIW, I would suggest initializing CouchbaseLite in Application.onCreate.

@blake.meike:I am getting the same error while initializing Couchbase. I have initialized CouchbaseLite in Application.onCreate still, I am facing a similar issue.
details:->
couldn’t find “libLiteCoreJNI.so”
arm64-v8a,armeabi-v7a,armeabi
Can you please help with what can be done?

Did you find any solution to this issue?

I really can’t be much help, with so very little information. If either of you can provide any of the following, it would be most helpful.

  • the exact application being used on the failing device.
  • the exact make and model of the failing device.
  • logcat from a minute or so, before the failure, up until the time of failure.
  • the code that causes the crash.
  • a description of how you created your app and how you included the couchbase library in it.


Please find the attached details of the device.
I am trying to use Couchbase in an application that is a Launcher to the device. Currently, I am trying to set this Launcher application as System-app. When the application starts it gets crashed with the following error:->

2022-01-31 10:40:19.870 2911-2911/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.actimirror.amretaillauncher, PID: 2911
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file “/system/priv-app/amRetailLauncher.apk”],nativeLibraryDirectories=[/system/lib64/amRetailLauncher, /system/lib64, /vendor/lib64, /system/lib64, /vendor/lib64]]] couldn’t find “libLiteCoreJNI.so”
at java.lang.Runtime.loadLibrary0(Runtime.java:1011)
at java.lang.System.loadLibrary(System.java:1657)
at com.couchbase.lite.CouchbaseLite.init(CouchbaseLite.java:63)
at com.actimirror.amretaillauncher.AppsApplication.getDatabase(AppsApplication.kt:62)
at com.actimirror.amretaillauncher.dataservices.CouchbaseDocumentService.getDocument(CouchbaseDocumentService.kt:38)

/*****************************************/

Code:-
fun getDatabase(): Database {
try {
CouchbaseLite.init(mInstance!!)
val dbConfig = DatabaseConfiguration()
db = Database(“AMRL”, dbConfig)
} catch (e: Exception) {
Log.d(“getDatabase”, " Exception: ${e.localizedMessage}")
}
return db!!
}
build.gradle:->
implementation ‘com.couchbase.lite:couchbase-lite-android:2.7.0’

When the Launcher is used as a normal application(not as a system app) the Couchbase works perfectly.

Is your application, by any chance, named “amRetailLauncher”?

Would you check to see whether the apk contains libLiteCoreJNI.so?

The package name is com.am.amretailLauncher, the .apk is named as amRetailLauncher.
How can I check that?
I tried searching for libLiteCoreJNI in the project but did not find anything relevant.

An APK is just a zip file. You can rename it (change “.apk” to “.zip”) and unzip it. Among the things in that come out of the archive should be ./lib/x86/libLiteCoreJNI.so

yes, it contains libLiteCoreJNI.so

FYI:- I tried a few things but did not help me.
created a jnilibs under src/main and created armeabi-v7a added libLiteCoreJNI.so

in gradle.properties added this flag → android.useDeprecatedNdk=true
in build.gradle added these lines-> ndk {
abiFilters “arm64-v8a”,“x86”, “x86_64”,“armeabi-v7a”
}

sourceSets {
main {
jni.srcDirs = [‘src/main/jniLibs’]
jniLibs.srcDir ‘src/main/jniLibs’
}
}

@blake.meike Any suggestions?

Does your app use the NDK?

If you can tell me how to obtain a copy of your application, I might be able to figure something out.

No, it does not use NDK.
I just found out a few scenarios, I hope this will help you to figure out something.

  1. When the application is installed as a system app the application tries to find out the libLiteCoreJNI.so files in the folder “system/libs” and “system/libs64” but fails to find it.
    When I tried to manually put the libLiteCoreJNI.so file in “system/libs64” the application was working fine.
  2. When the application is installed as a user app it is able to fetch the library from the folder “lib/arm64-v8a” and the application works perfectly.
    The only problem here is the app when installed as a system app is unable to find the “libLiteCoreJNI.so” library in /system/lib,/system/libs64,vendor/lib,vendor/libs64.

In the error message that you include above, I see:

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file “/system/priv-app/amRetailLauncher.apk”],nativeLibraryDirectories=[/system/lib64/amRetailLauncher, /system/lib64, > /vendor/lib64, /system/lib64, /vendor/lib64]]] couldn’t find “libLiteCoreJNI.so”
at java.lang.Runtime.loadLibrary0(Runtime.java:1011)

This corresponds, exactly, to what I see in the Android source code. The loader is trying to load the library from:

  • /system/priv-app/amRetailLauncher.apk (zip file)
  • /system/lib64/amRetailLauncher (app dir)
  • /system/lib64 (system dir)
  • /vendor/lib64 (system dir)

… and not finding it (libLiteCoreJNI.so) at least in than one instance.

I could guess that, perhaps, your app is not named “amRetailLauncher.apk”, or that is is not stored in the directory “/system/priv-ap”?

Since it is a privileged app, perhaps it would make sense to pull the native library out and to put it in “/vendor/lib64” or “/system/lib64”.

One way or another, you are going to have to put the library where the VM is looking for it.

… actually, one more possibility. Unless I am mistaken, the library path is obtained by parsing the value of the system property “java.library.path”. If the native lib is somewhere odd, perhaps you could add the location to the property list with which the class is started (something you’d have to do at system startup)

1 Like

I have the same error. I’m doing a final year project on android.

When I use couchbase lite and inicialize on the first window, I have no problem, but when it is inicialized after, I have that error. I have no idea about put libLiteCoreJNI.so in /system. Android Studio said:
“There were errors uploading files and/or directories: couldn’t create file: Read-only file system” and I don’t know how to change this so that it allows me to put the file inside this directory.

Anybody can help me please?
Thanks for all.

Hey @Adrian_Solis_Melcon Does the above answer not help you? Unless you provide details of your problem we aren’t going to be much help, here.

Thanks for your answer.

I have the same Monika’s error:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file “/data/app/com.smartway.app-glJ1Fya7KVMlJeX3Gl_PiA==/base.apk”],nativeLibraryDirectories=[/data/app/com.smartway.app-glJ1Fya7KVMlJeX3Gl_PiA==/lib/arm64, /system/lib64, /system/product/lib64]]] couldn’t find “libLiteCoreJNI.so” when I init Couchbase database as a result of clicking a button in another window (I click the button, which takes me to another window).
When i init Couchbase database with a single window in the app, this problem does not occur.

I have tried to follow your solution, but when trying to manually enter the library “libLiteCoreJNI.so” on /system/lib64 on device file explorer, android studio tells me the following:

  • transfer error: couldn’t create file: Read-only file system

  • Device File Explorer
    There were errors uploading files and/or directories:
    couldn’t create file: Read-only file system

I don’t know how to enter that library manually in the sites aplication or android studio try to search.

Thank you in advance for your help.

1 Like

@Adrian_Solis_Melcon , I think it would help a lot if you started at the beginning, described the steps you are taking, what you expect to happen and what happens instead.

This is a normally installed Android application? What versions of Android? What version of CBL Mobile are you using? Can I see the logs around the time that the error happens?

Of course, I tell you from the beginning.

I’m making an Android app in Android Studio where when launched it goes to a single window and launches the Couchbase database. In said window, by clicking on a button, it begins to introduce geographical values ​​of the user’s location into said database. To do this, I used Google’s .com.google.android.gms.location.sample.locationupdatesforegroudservice package, which has two classes MainActivity.java and LocationUpdatesService.java. I have the line “CouchbaseLite.init(context)” in the onCreate method of the LocationUpdatesService.java class.

Once this works for me, I improve my Android application.

Once the application starts, it shows a window with a button to sign in through Google. Once the account is selected, it takes me to another window where my account data appears (only informative for me) and a button, which leads to the window that I have talked about in the previous paragraph and in which it should be initialize the Couchbase database. This is where the error that I have mentioned in the previous comments occurs and I do not understand why.

What I was hoping for is that, when I hit that last button, it would work as described in the previous paragraph, and I don’t understand why this error is due. It is curious because the application does not break, but it returns me to the previous window and continues the execution of the application, without stopping.

I use Android Studio Bumblebee version 2021.1.1. When I run my app in Android Studio, it gets installed on the phone (Xiaomi Redmi Note 7 with API 29). As for Couchbase, I have in build.gradle the following: implementation ‘com.couchbase.lite:couchbase-lite-android:3.0.0’.

Anything else you need, let me know. What I get in the logcat (in reference to the error when pressing the button) is the following:

2022-06-30 23:31:36.556 21026-21026/? E/om.smartway.ap: Unknown bits set in runtime_flags: 0x8000
2022-06-30 23:31:37.039 21026-21026/com.smartway.app E/libc: Access denied finding property “ro.vendor.df.effect.conflict”
2022-06-30 23:31:37.045 21026-21065/com.smartway.app E/Perf: Fail to get file list com.smartway.app
2022-06-30 23:31:37.045 21026-21065/com.smartway.app E/Perf: getFolderSize() : Exception_1 = java.lang.NullPointerException: Attempt to get length of null array.

I am very grateful for your attention and sorry for the inconvenience.
Thank you very much in advance.

This appears to be a problem common to Xiaomi devices (google it). I do not know what causes it.

I can make some suggestions:

  • Most applications initialize CBL in the Application object. While I don’t think that is the problem here, it might be worth doing.
  • the error you describe here is definitely not “the same Monika’s error”. It is something to do with the file system.
  • try your application in an emulator to see if the problem is, as I suspect, platform related.
  • There is a second init signature:
    CouchbaseLite.init(@NonNull Context ctxt, boolean debug, @NonNull File rootDbDir, @NonNull File scratchDir)
    It appears that Xiaomi devices have different constraints for using the file system. You might try using this method to specify root and scratch directories different from Context.getFilesDir() and Context.getExternalFilesDir, which are what the default signature uses.

Hope that helps!