Open DongXinyu opened 1 year ago
What is "abnormal"?
@kpgalligan I'm sorry to have troubled you. What I'm trying to say is not working properly.
In what way? Doesn't compile? It's not encrypting the data?
@kpgalligan It compiles normally. When running on an iPhone, the data is not encrypted. When running in the emulator, the database cannot be opened using the key in the code. I downloaded the generated database to my computer and then tried to open it with DB Browser for SQLite.
That has nothing to do with sqliter. That's how you're linking to SQLCipher (or whatever you're using).
I installed SQLCipher correctly.
Call DatabaseConfiguration encryption is normal when Kotlin is set to 1.7.10.
// Project/build.gradle.kts
plugins {
//trick: for the same plugin versions in all sub-modules
id("com.android.application").version("7.4.1").apply(false)
id("com.android.library").version("7.4.1").apply(false)
kotlin("android").version("1.7.10").apply(false)
kotlin("multiplatform").version("1.7.10").apply(false)
}
DatabaseConfiguration encryption goes wrong when Kotlin is set to 1.8.0.
// Project/build.gradle.kts
plugins {
//trick: for the same plugin versions in all sub-modules
id("com.android.application").version("7.4.1").apply(false)
id("com.android.library").version("7.4.1").apply(false)
kotlin("android").version("1.8.0").apply(false)
kotlin("multiplatform").version("1.8.0").apply(false)
}
Android Studio prompts DatabaseConfiguration here:
You mean SQLCipher does not support Kotlin 1.8.0, not related to DatabaseConfiguration, am I understanding correctly?
I mean that if you link to sqlite on iOS, you can pass in an encryption key but it won't matter. You need to bundle and link to SQLCipher. It is very hard to figure anything out without seeing your whole config, but basically you need to confirm that you aren't linking to the included sqlite.
How and when do you include SQLCipher on iOS?
My project uses Kotlin Mulitplatform Mobile and SqlDelight. SQLCipher is used to enhance SQLite for encryption.
I think touchlabSQLiter is the equivalent of middleware, am I getting that right?
Kotlin Mulitplatform Mobile
├── androidMain
├── commonMain
└── iosMain
└── SqlDelight
└── touchlabSQLiter
└── SQLCipher
└── SQLite
Some of my configurations are as follows:
# project/bulid.gradle.kts
plugins {
id("com.android.application").version("7.4.1").apply(false)
id("com.android.library").version("7.4.1").apply(false)
kotlin("android").version("1.7.10").apply(false)
/* The encryption problem occurred in 1.8.0 */
kotlin("multiplatform").version("1.7.10").apply(false)
}
buildscript {
dependencies {
classpath("com.squareup.sqldelight:gradle-plugin:1.5.5")
}
}
# shared/build.gradle.kts
plugins {
kotlin("multiplatform")
kotlin("native.cocoapods")
id("com.android.library")
id("com.squareup.sqldelight")
}
cocoapods {
summary = "Some description for the Shared Module"
homepage = "Link to the Shared Module homepage"
version = "1.0.1"
ios.deploymentTarget = "14.1"
podfile = project.file("../iosApp/Podfile")
framework {
baseName = "shared"
}
pod("SQLCipher") { /* Call Cocoapods pod install here to install SQLCipher4.5.3 */
version = "~> 4.5.3"
}
}
sourceSets {
val iosArm64Main by getting
val iosX64Main by getting
val iosSimulatorArm64Main by getting
val iosMain by creating {
dependsOn(commonMain)
iosArm64Main.dependsOn(this)
iosX64Main.dependsOn(this)
iosSimulatorArm64Main.dependsOn(this)
dependencies {
implementation("com.squareup.sqldelight:native-driver:1.5.5")
implementation("co.touchlab:sqliter-driver:1.2.1")
}
}
Test equipment:
# shared/iosMain/Kotlin/actual.kt
package me.dxy.common.repository
import co.touchlab.sqliter.DatabaseConfiguration
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import com.squareup.sqldelight.drivers.native.wrapConnection
import me.dxy.common.di.MyTestDatabaseWrapper
import me.dxy.mytest.shared.db.MyTestDB
import org.koin.dsl.module
actual fun platformModule() = module {
single {
val schema = MyTestDB.Schema
val databaseConfiguration = DatabaseConfiguration(
name = "testEncrypt.db",
version = schema.version,
create = { connection ->
wrapConnection(connection) { schema.create(it) }
},
upgrade = { connection, oldVersion, newVersion ->
wrapConnection(connection) {
schema.migrate(it, oldVersion, newVersion)
}
},
encryptionConfig = DatabaseConfiguration.Encryption("123")
)
val driver = NativeSqliteDriver(databaseConfiguration)
MyTestDatabaseWrapper(MyTestDB(driver))
}
}
# iosApp/iOSApp.swfit
import SwiftUI
import shared
import SQLCipher
@main
struct iOSApp: App {
init() {
KoinKt.doInitKoin()
}
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
# iosApp/ContentView.swfit
import SwiftUI
import shared
import SQLCipher
struct ContentView: View {
var body: some View {
Text("Oh yeah!")
Button("Click me") {
MyTestRepository().savePeople()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
The following code works fine when kotlin("multiplatform").version("1.7.10"), and the encryption part is abnormal when kotlin("multiplatform").version("1.8.0").
The main problem is the following code, and the rest of the code works fine.