androidkotlin-multiplatformsqldelight

How to make SQLDelight use sqlite3 directly via JNI on Android?


We have database with big blobs (images) ~3.5mb which we cannot read on Android versions below 9 (API 28) because we cannot set a larger custom cursor window size (see https://github.com/cashapp/sqldelight/pull/4804).

For Android SQLDelight has AndroidSqliteDriver class which uses Cursor under the hood with window size limitations, while there are no such limitations for native sqlite3 queries.

How can I make SQLDelight use sqlite3 API directly via my custom JNI wrapper? Should I write a custom driver class? If yes how can I do this? My thought was to write my own driver based on AndroidSqliteDriver just replacing Cursor calls with JNI calls, but unfortunately I do not know how to do it properly, can you give an example of such implementation?

SQLDelight discussion https://github.com/cashapp/sqldelight/discussions/5250


Solution

  • Android (last I checked) does not let you link directly to the sqlite3 on the device. That's why there's no "SQLiter" for Android (disclaimer: I wrote SQLiter, which provides the native driver for SqlDelight).

    You might want to look at SqlCipher.

    https://github.com/sqlcipher/android-database-sqlcipher.

    It provides its own Android driver. If you don't give it a key, it'll act like the regular sqlite. I don't know if they have a way to specify the cursor window size, but if not, you'd have a much easier time forking that than starting over.

    Alternatively, the KMP Room implementation seems to have a new driver on the Android side, although I haven't had much time to dig into it and confirm that.

    https://developer.android.com/kotlin/multiplatform/room