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.