Skip to content

Commit ddab8ac

Browse files
committed
chore: update android file
1 parent b163219 commit ddab8ac

File tree

1 file changed

+67
-39
lines changed
  • sqlite-cloud/sqlite-ai/sqlite-sync/quick-starts

1 file changed

+67
-39
lines changed

sqlite-cloud/sqlite-ai/sqlite-sync/quick-starts/android.md

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
---
1+
r---
22
title: "Android Quick Start Guide"
33
description: SQLite Sync is a multi-platform extension that brings a true local-first experience to your applications with minimal effort.
44
category: platform
55
status: publish
66
slug: sqlite-sync-quick-start-android
7+
78
---
89

910
This guide shows how to integrate sqlite sync extension into your Android application. Since extension loading is disabled by default in Android's SQLite implementation, you need an alternative SQLite library that supports extensions.
@@ -14,71 +15,92 @@ This example uses the [requery:sqlite-android](https://github.com/requery/sqlite
1415

1516
In your `app/build.gradle.kts`:
1617

18+
<details>
19+
<summary>Groovy DSL</summary>
20+
21+
```groovy
22+
repositories {
23+
google()
24+
mavenCentral()
25+
maven { url 'https://jitpack.io' }
26+
}
27+
dependencies {
28+
// ...
29+
// Use requery's SQLite instead of Android's built-in SQLite to support loading custom extensions
30+
implementation 'com.github.requery:sqlite-android:3.49.0'
31+
// Both packages below are identical - use either one
32+
implementation 'ai.sqlite:sync:0.8.39' // Maven Central
33+
// implementation 'com.github.sqliteai:sqlite-sync:0.8.39' // JitPack (alternative)
34+
}
35+
```
36+
</details>
37+
38+
<details>
39+
<summary>Kotlin DSL</summary>
40+
1741
```kotlin
42+
repositories {
43+
google()
44+
mavenCentral()
45+
maven(url = "https://jitpack.io")
46+
}
1847
dependencies {
48+
// ...
49+
// Use requery's SQLite instead of Android's built-in SQLite to support loading custom extensions
1950
implementation("com.github.requery:sqlite-android:3.49.0")
51+
// Both packages below are identical - use either one
52+
implementation("ai.sqlite:sync:0.8.39") // Maven Central
53+
// implementation("com.github.sqliteai:sqlite-sync:0.8.39") // JitPack (alternative)
2054
}
2155
```
56+
</details>
57+
58+
### 2. Update AndroidManifest.xml
2259

23-
### 2. Bundle the Extension
60+
Add `android:extractNativeLibs="true"` to your `<application>` tag:
2461

25-
1. Go to [sqlite-sync releases](https://github.com/sqliteai/sqlite-sync/releases)
26-
2. Download your preferred .zip architecture releases:
27-
- arm64-v8a - Modern 64-bit ARM devices (recommended for most users)
28-
- x86_64 - 64-bit x86 emulators and Intel-based devices
29-
3. Extract and place the `cloudsync.so` file in: `app/src/main/assets/lib/cloudsync.so`
62+
```xml
63+
<application
64+
android:extractNativeLibs="true"
65+
...>
66+
```
3067

3168
### 3. Basic Integration
3269

3370
Here’s a complete example showing how to load the extension, create a table, initialize CloudSync, and perform network sync.
3471

72+
> **Important:** Replace the following placeholders with your actual values:
73+
>
74+
> - `database_name` - Your database name
75+
> - `table_name` - Your table name
76+
> - `<connection-string>` - Your SQLiteCloud connection string
77+
> - `<api-key>` - Your SQLiteCloud API key
78+
3579
```kotlin
36-
import android.content.Context
3780
import android.os.Bundle
3881
import androidx.activity.ComponentActivity
3982
import androidx.lifecycle.lifecycleScope
4083
import io.requery.android.database.sqlite.SQLiteCustomExtension
41-
import io.requery.android.database.sqlite.SQLiteCustomFunction
4284
import io.requery.android.database.sqlite.SQLiteDatabase
4385
import io.requery.android.database.sqlite.SQLiteDatabaseConfiguration
44-
import io.requery.android.database.sqlite.SQLiteFunction
4586
import kotlinx.coroutines.Dispatchers
4687
import kotlinx.coroutines.launch
4788
import kotlinx.coroutines.withContext
48-
import java.io.File
49-
import java.io.FileOutputStream
5089

5190
class MainActivity : ComponentActivity() {
52-
private fun copyExtensionToFilesDir(context: Context): File {
53-
val assetManager = context.assets
54-
val inputStream = assetManager.open("lib/cloudsync.so")
55-
56-
val outFile = File(context.filesDir, "cloudsync.so")
57-
inputStream.use { input ->
58-
FileOutputStream(outFile).use { output ->
59-
input.copyTo(output)
60-
}
61-
}
62-
return outFile
63-
}
64-
6591
override fun onCreate(savedInstanceState: Bundle?) {
6692
super.onCreate(savedInstanceState)
6793

68-
// --- Copy extension from assets to filesystem ---
69-
val extensionFile = copyExtensionToFilesDir(this)
70-
val extensionPath = extensionFile.absolutePath
71-
7294
// --- Create extension configuration ---
73-
val cloudSyncExtension = SQLiteCustomExtension(extensionPath, null)
95+
val cloudsyncExtension = SQLiteCustomExtension(applicationInfo.nativeLibraryDir + "/cloudsync", null)
7496

7597
// --- Configure database with extension ---
7698
val config = SQLiteDatabaseConfiguration(
77-
"${filesDir.path}/database_name.db",
99+
cacheDir.path + "/database_name.db",
78100
SQLiteDatabase.CREATE_IF_NECESSARY or SQLiteDatabase.OPEN_READWRITE,
79-
emptyList<SQLiteCustomFunction>(),
80-
emptyList<SQLiteFunction>(),
81-
listOf(cloudSyncExtension)
101+
emptyList(),
102+
emptyList(),
103+
listOf(cloudsyncExtension)
82104
)
83105

84106
// --- Open database ---
@@ -103,9 +125,9 @@ class MainActivity : ComponentActivity() {
103125
// --- Create test table ---
104126
val createTableSQL = """
105127
CREATE TABLE IF NOT EXISTS $tableName (
106-
id TEXT PRIMARY KEY NOT NULL,
107-
value TEXT NOT NULL DEFAULT '',
108-
created_at TEXT DEFAULT CURRENT_TIMESTAMP
128+
id TEXT PRIMARY KEY NOT NULL,
129+
value TEXT NOT NULL DEFAULT '',
130+
created_at TEXT DEFAULT CURRENT_TIMESTAMP
109131
);
110132
""".trimIndent()
111133
db.execSQL(createTableSQL)
@@ -121,10 +143,16 @@ class MainActivity : ComponentActivity() {
121143
""".trimIndent())
122144

123145
// --- Initialize network connection ---
124-
db.rawQuery("SELECT cloudsync_network_init('<connection-string>');", null).use { it.moveToFirst() }
146+
db.rawQuery(
147+
"SELECT cloudsync_network_init('<connection-string>');",
148+
null
149+
).use { it.moveToFirst() }
125150

126151
// --- Set API key ---
127-
db.rawQuery( "SELECT cloudsync_network_set_apikey('<api-key>');", null).use { it.moveToFirst() }
152+
db.rawQuery(
153+
"SELECT cloudsync_network_set_apikey('<api-key>');",
154+
null
155+
).use { it.moveToFirst() }
128156

129157
// --- Run network sync multiple times ---
130158
// Note: cloudsync_network_sync() returns > 0 if data was sent/received.

0 commit comments

Comments
 (0)