Hi, Priya! Thanks for the answer.
Let’s say that we use Couchbase lite v1.4.1 in the Android app, but currently without encryption. We can’t update a Couchbase lite lib version, but we have created a new application version with the encryption enabled with the same Couchbase lite v1.4.1 in according to the documentation Java | Couchbase Docs Archive
So if Android app version with unencrypted DB was used on the device - we already have a local Couchbase DB created. And if we install a new application version with the encryption enabled on the same device - we have to use changeEncryptionKey method to encrypt already created DB. On this step mentioned issue appears.
interface ICouchDbCreator {
fun init()
fun createDb(name: String): Database?
}
class CouchDbCreator(private val context: Context) : ICouchDbCreator {
private var manager: Manager? = null
override fun init() {
try {
manager = Manager(
AndroidContext(context.applicationContext),
Manager.DEFAULT_OPTIONS
)
showLogs(true)
} catch (e: IOException) {
e.printStackTrace()
}
}
private fun showLogs(isShow: Boolean) {
if (!isShow) return
Manager.enableLogging(Log.TAG, Log.VERBOSE)
Manager.enableLogging(Log.TAG_SYNC_ASYNC_TASK, Log.VERBOSE)
Manager.enableLogging(Log.TAG_SYNC, Log.VERBOSE)
Manager.enableLogging(Log.TAG_QUERY, Log.VERBOSE)
Manager.enableLogging(Log.TAG_VIEW, Log.VERBOSE)
Manager.enableLogging(Log.TAG_DATABASE, Log.VERBOSE)
Manager.enableLogging(Log.TAG_CHANGE_TRACKER, Log.VERBOSE)
Manager.enableLogging(Log.TAG_REMOTE_REQUEST, Log.VERBOSE)
}
override fun createDb(name: String): Database? {
return try {
val key = getKey();
manager?.openDatabase(name, getDatabaseOptions(key))
} catch (e: CouchbaseLiteException) {
if (e.cblStatus.code == 401) {
// Here we understand that we have to change encryption key for the existed DB
return encryptExistingDb(name)
}
null
}
}
// For unencrypted DB
private var oldpass: String? = null
private fun encryptExistingDb(name: String): Database? {
return try {
// Here we open an existed DB successfully
val db = manager?.openDatabase(name, getDatabaseOptions(oldpass))
// Here we receive an issue
db?.changeEncryptionKey(getKey())
db
} catch (e: CouchbaseLiteException) {
e.printStackTrace()
null
}
}
private fun getDatabaseOptions(key: String?): DatabaseOptions {
val options = DatabaseOptions()
options.isCreate = true
options.encryptionKey = key
return options
}
private fun getKey(): String {
// Any key
return "newEncryptionKey";
}
}