We need to include SQLCipher in our KMM project without using Cocoapods. This involves building a libSQLCipher.a. I was able to this in terminal with these commands:
./configure --enable-tempstore=yes --with-crypto-lib=commoncrypto CFLAGS="
-DSQLITE_HAS_CODEC=1
-DSQLITE_TEMP_STORE=3
-DSQLCIPHER_CRYPTO_CC
-DSQLITE_SOUNDEX
-DSQLITE_THREADSAFE
-DSQLITE_ENABLE_RTREE
-DSQLITE_ENABLE_STAT3
-DSQLITE_ENABLE_STAT4
-DSQLITE_ENABLE_COLUMN_METADATA
-DSQLITE_ENABLE_MEMORY_MANAGEMENT
-DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_UNLOCK_NOTIFY
-DSQLITE_ENABLE_FTS3_PARENTHESIS
-DSQLITE_ENABLE_FTS4
-DSQLITE_ENABLE_FTS4_UNICODE61
-DSQLITE_ENABLE_JSON1
-DSQLITE_ENABLE_FTS5
-DHAVE_USLEEP=1
-DSQLITE_MAX_VARIABLE_NUMBER=99999"
LDFLAGS="/usr/local/Cellar/openssl@1.1 /1.1.1l_1/lib/libcrypto.a"
make sqlite3.c
`xcrun --sdk iphoneos --find clang` -x c -target arm64-apple-ios8.0 -isysroot `xcrun --sdk iphoneos --show-sdk-path` -fmodules -DSQLITE_HAS_CODEC=1 -fstrict-aliasing -fembed-bitcode-marker -DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_STAT3 -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLCIPHER_CRYPTO_CC -DHAVE_USLEEP=1 -DSQLITE_MAX_VARIABLE_NUMBER=99999 -DNDEBUG -DSQLITE_HAS_CODEC -DSQLITE_TEMP_STORE=2 -DSQLITE_SOUNDEX -DSQLITE_THREADSAFE -DSQLITE_ENABLE_RTREE -DSQLITE_ENABLE_STAT3 -DSQLITE_ENABLE_STAT4 -DSQLITE_ENABLE_COLUMN_METADATA -DSQLITE_ENABLE_MEMORY_MANAGEMENT -DSQLITE_ENABLE_LOAD_EXTENSION -DSQLITE_ENABLE_FTS4 -DSQLITE_ENABLE_FTS4_UNICODE61 -DSQLITE_ENABLE_FTS3_PARENTHESIS -DSQLITE_ENABLE_UNLOCK_NOTIFY -DSQLITE_ENABLE_JSON1 -DSQLITE_ENABLE_FTS5 -DSQLCIPHER_CRYPTO_CC -DHAVE_USLEEP=1 -DSQLITE_MAX_VARIABLE_NUMBER=99999 -fno-objc-arc -fmodule-name=SQLCipher -Oz -fno-common -c sqlite3.c -o sqlite3.o
`xcrun --sdk iphoneos --find libtool` -static -arch_only arm64 -syslibroot `xcrun --sdk iphoneos --show-sdk-path` -o libSQLCipher.a sqlite3.o
I bring the libSQLCipher.a into Android studio, and set it up per the instructions on the KMM website, create the .def file, link it up, add to build.gradle etc. I can access methods such as sql3_open(), which is great. However, I cannot access sqlite3_key(), sqlite3_key_v2(), sqlite3_rekey(), sqlite3_rekey_v2(), sqlite3_activate_see().
I noticed in the .h file, these are here:
/* BEGIN SQLCIPHER */
#ifdef SQLITE_HAS_CODEC
// sqlite3_key(), sqlite3_key_v2(), sqlite3_rekey(), sqlite3_rekey_v2(),
sqlite3_activate_see() methods are defined here
#endif
/* END SQLCIPHER */
SQLCipher website says to include SQLITE_HAS_CODEC in the build config, which it looks like I am doing. So I'm really lost as to why I cannot access these methods.
Bit of a long winded question but really hope somebody here has some advice on it. Thanks!
Kotlin/Native's cinterop
tool should have all compiler options specified explicitly to "see" the header the same way as your compiler did when building the library. To specify the compiler options, please use the DSL as described in the documentation.
In general, something like that should work:
...
kotlin {
iosArm64 { // Replace with a target you need.
compilations.getByName("main") {
val myInterop by cinterops.creating {
...
compilerOpts("-DSQLITE_HAS_CODEC=1")
...
}
}
}
...