Skip to content

Commit

Permalink
Add androidx-startup, enable auto CouchbaseLite.init()
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdgr8 committed May 31, 2023
1 parent 6a7b0f6 commit 464d032
Show file tree
Hide file tree
Showing 15 changed files with 198 additions and 66 deletions.
6 changes: 5 additions & 1 deletion couchbase-lite-ee/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ kotlin {
androidMain {
dependencies {
api(libs.couchbase.lite.android.ee)
implementation(libs.androidx.startup)
}
}

Expand All @@ -100,7 +101,10 @@ kotlin {
}
}

android.namespace = "com.udobny.kmp.couchbase.lite"
android {
namespace = "com.udobny.kmp.couchbase.lite"
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
}

// Internal headers required for tests
tasks.named<DefFileTask>("generateDefCouchbaseLite") {
Expand Down
1 change: 1 addition & 0 deletions couchbase-lite-ee/src/androidMain/AndroidManifest.xml
6 changes: 5 additions & 1 deletion couchbase-lite/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,16 @@ kotlin {
androidMain {
dependencies {
api(libs.couchbase.lite.android)
implementation(libs.androidx.startup)
}
}
}
}

android.namespace = "com.udobny.kmp.couchbase.lite"
android {
namespace = "com.udobny.kmp.couchbase.lite"
sourceSets["main"].manifest.srcFile("src/androidMain/AndroidManifest.xml")
}

// Internal headers required for tests
tasks.named<DefFileTask>("generateDefCouchbaseLite") {
Expand Down
13 changes: 13 additions & 0 deletions couchbase-lite/src/androidMain/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application>
<provider android:name="androidx.startup.InitializationProvider"
android:authorities="${applicationId}.androidx-startup"
android:exported="false"
tools:node="merge">
<meta-data android:name="com.couchbase.lite.kmp.internal.CouchbaseLiteInitializer"
android:value="androidx.startup" />
</provider>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,26 @@ package com.couchbase.lite.kmp
import android.content.Context
import com.couchbase.lite.BuildConfig
import com.couchbase.lite.internal.CouchbaseLiteInternal
import kotlinx.atomicfu.atomic
import java.io.File

public object CouchbaseLite {
public actual object CouchbaseLite {

private val initCalled = atomic(false)

/**
* Initialize CouchbaseLite library. This method MUST be called before using CouchbaseLite.
* Initialize CouchbaseLite library. Unlike the Couchbase Lite Android SDK,
* this method is optional to call before using CouchbaseLite. The single-parameter
* `CouchbaseLite.init(Context)` will be called automatically by androidx-startup.
*/
public fun init(ctxt: Context) {
init(ctxt, BuildConfig.CBL_DEBUG)
}

/**
* Initialize CouchbaseLite library. This method MUST be called before using CouchbaseLite.
* Initialize CouchbaseLite library. Unlike the Couchbase Lite Android SDK,
* this method is optional to call before using CouchbaseLite. The single-parameter
* `CouchbaseLite.init(Context)` will be called automatically by androidx-startup.
*/
public fun init(ctxt: Context, debug: Boolean) {
init(
Expand All @@ -40,6 +47,33 @@ public object CouchbaseLite {
* @param scratchDir scratch directory for SQLite
*/
public fun init(ctxt: Context, debug: Boolean, rootDbDir: File, scratchDir: File) {
if (initCalled.getAndSet(true)) return
resetInit()
com.couchbase.lite.CouchbaseLite.init(ctxt, debug, rootDbDir, scratchDir)
}

/**
* Allow default internalInit() to be overridden by manual init() call
*/
private fun resetInit() {
@Suppress("VisibleForTests")
CouchbaseLiteInternal.reset(false)
}

internal actual fun internalInit() {
// no-op
// Android default initialization is handled by androidx-startup
// Content Provider in AndroidManifest.xml with internalInit(Context)
}

/**
* Default init that will auto initialize native Couchbase Lite library
* from androidx-startup Content Provider on app startup.
* Doesn't set [initCalled] to allow a manual [init] call to succeed.
*/
internal fun internalInit(ctxt: Context) {
if (initCalled.value) return
init(ctxt)
initCalled.value = false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.couchbase.lite.kmp.internal

import android.content.Context
import androidx.startup.Initializer
import com.couchbase.lite.kmp.CouchbaseLite

/**
* Initializes CouchbaseLite library automatically on app startup
* from androidx-startup Content Provider.
*/
public class CouchbaseLiteInitializer : Initializer<Unit> {

override fun create(context: Context) {
CouchbaseLite.internalInit(context)
}

override fun dependencies(): List<Class<out Initializer<*>>> = emptyList()
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.couchbase.lite.kmp

public expect object CouchbaseLite {

internal fun internalInit()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ internal constructor(actual: com.couchbase.lite.Database) :

public actual companion object {

init {
CouchbaseLite.internalInit()
}

public actual val log: Log by lazy { Log(com.couchbase.lite.Database.log) }

@Throws(CouchbaseLiteException::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ internal constructor(actual: com.couchbase.lite.DatabaseConfiguration) :
set(value) {
actual.directory = value
}

private companion object {

init {
CouchbaseLite.internalInit()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.couchbase.lite.kmp

import org.junit.Test

class AutoInitTest {

@Test
fun testCreateDatabaseWithoutInit() {
val db = Database("succeed", DatabaseConfiguration())
db.delete()
}

@Test
fun testGetConsoleWithoutInit() {
Database.log.console
}

@Test
fun testGetFileWithoutInit() {
Database.log.file
}

@Test
fun testCreateDBConfigWithoutInit() {
DatabaseConfiguration()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,46 @@
package com.couchbase.lite.kmp

import com.couchbase.lite.internal.CouchbaseLiteInternal
import kotlin.test.*

internal actual fun couchbaseLiteReset(state: Boolean) {
CouchbaseLiteInternal.reset(state)
// native doesn't need initializing
class PreInitTest : BaseTest() {

@BeforeTest
fun setUpPreInitTest() {
CouchbaseLiteInternal.reset(false)
}

@AfterTest
fun tearDownPreInitTest() {
CouchbaseLiteInternal.reset(true)
}

@Test
fun testCreateDatabaseBeforeInit() {
assertFailsWith<IllegalStateException> {
Database("fail", DatabaseConfiguration())
}
}

@Test
fun testGetConsoleBeforeInit() {
assertFailsWith<IllegalStateException> {
Database.log.console
}
}

@Test
fun testGetFileBeforeInit() {
assertFailsWith<IllegalStateException> {
Database.log.file
}
}

@Test
fun testCreateDBConfigBeforeInit() {
assertFailsWith<IllegalStateException> {
DatabaseConfiguration()
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package com.couchbase.lite.kmp

import com.couchbase.lite.internal.CouchbaseLiteInternal
import kotlinx.atomicfu.atomic
import java.io.File

/**
* CouchbaseLite Utility
*/
public object CouchbaseLite {
public actual object CouchbaseLite {

private val initCalled = atomic(false)

/**
* Initialize CouchbaseLite library. This method MUST be called before using CouchbaseLite.
* Initialize CouchbaseLite library. Unlike the Couchbase Lite Java SDK,
* this method is optional to call before using CouchbaseLite. The no-parameter
* `CouchbaseLite.init()` will be called automatically by default.
*
* This method expects the current directory to be writeable
* and will throw an `IllegalStateException` if it is not.
Expand All @@ -19,11 +25,15 @@ public object CouchbaseLite {
*/
@JvmOverloads
public fun init(debug: Boolean = false) {
if (initCalled.getAndSet(true)) return
resetInit()
com.couchbase.lite.CouchbaseLite.init(debug)
}

/**
* Initialize CouchbaseLite library. This method MUST be called before using CouchbaseLite.
* Initialize CouchbaseLite library. Unlike the Couchbase Lite Java SDK,
* this method is optional to call before using CouchbaseLite. The no-parameter
* `CouchbaseLite.init()` will be called automatically by default.
*
* This method allows specifying a default root directory for database files,
* and the scratch directory used for temporary files (the native library, etc).
Expand All @@ -35,6 +45,26 @@ public object CouchbaseLite {
* @throws IllegalStateException on initialization failure
*/
public fun init(debug: Boolean, rootDir: File, scratchDir: File) {
if (initCalled.getAndSet(true)) return
resetInit()
com.couchbase.lite.CouchbaseLite.init(debug, rootDir, scratchDir)
}

/**
* Allow default internalInit() to be overridden by manual init() call
*/
private fun resetInit() {
@Suppress("VisibleForTests")
CouchbaseLiteInternal.reset(false)
}

/**
* Default init that will guarantee to initialize native Couchbase Lite library
* from [Database] and [DatabaseConfiguration] static initializers.
* Doesn't set [initCalled] to allow a manual [init] call to succeed.
*/
internal actual fun internalInit() {
if (initCalled.value) return
com.couchbase.lite.CouchbaseLite.init()
}
}

This file was deleted.

1 change: 1 addition & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version = "1

[libraries]
androidx-lifecycle-runtime-ktx = { module = "androidx.lifecycle:lifecycle-runtime-ktx", version = "2.6.1" }
androidx-startup = { module = "androidx.startup:startup-runtime", version = "1.1.1" }
androidx-test-core-ktx = { module = "androidx.test:core-ktx", version = "1.5.0" }
androidx-test-runner = { module = "androidx.test:runner", version = "1.5.2" }
couchbase-lite-android = { module = "com.couchbase.lite:couchbase-lite-android", version.ref = "couchbase-lite-java" }
Expand Down

0 comments on commit 464d032

Please sign in to comment.