mirror of
https://github.com/supabase/supabase.git
synced 2026-12-05 16:09:06 +00:00
Closes DOCS-1616 ## Problem The remaining quickstarts don't follow the [quickstart template](https://github.com/supabase/supabase/blob/docs/quickstart-template/apps/docs/style-guide/templates/quickstart.md) either. ## Solution - **Heading IDs:** each numbered step ends with the template's ID, such as `[#create-the-client]`. - **Heading wording:** eight headings now use the template's wording, such as `Create the Supabase client`. - **Style:** the prose follows the word list and elements rules. **Not changed:** RedwoodJS, Spring Boot, and Refine. DOCS-1624 covers them. **Before and after:** numbered headings that use the template's wording. | Page | Before | After | | --- | --- | --- | | iOS | 5/8 | 6/8 | | Kotlin | 4/8 | 5/8 | | Flask, Flutter, Unity | 5/7 | 6/7 | | Expo | 6/7 | 7/7 | | Reflex | 7/7 | 7/7 | | Laravel | 3/7 | 3/7 | | Rails | 3/6 | 3/6 | **Possible errors, not changed:** - The iOS page opens **Xcode > New Project > iOS > App**. Xcode's menu might be **File > New > Project**. - The Vue page says to drop a type import that `supabaseClient.ts` doesn't have. ## Preview links | Page | Preview | | --- | --- | | Flask | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/flask) | | Reflex | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/reflex) | | Expo React Native | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/expo-react-native) | | Flutter | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/flutter) | | iOS SwiftUI | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/ios-swiftui) | | Kotlin | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/kotlin) | | Unity | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/unity) | | Laravel | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/laravel) | | Ruby on Rails | [Preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/ruby-on-rails) | ## Review category (docs PRs only) <!-- If this PR changes docs and you're on the Supabase team, select one category. PRs from forks are always reviewed as Low, so you can skip this section. --> - [ ] Critical: needs a review as soon as possible - [ ] Urgent: the feature ships soon, or it's blocking something else - [ ] Moderate: good to have soon, not urgent - [ ] Low: get to it when you can ## Review instructions 1. Open the [Kotlin preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/kotlin) and click **Create the Supabase client** in the table of contents. The URL ends in `#create-the-client`. 2. Open the [Laravel preview](https://docs-git-docs-quickstart-rework-other-supabase.vercel.app/docs/guides/getting-started/quickstarts/laravel) and click the **Run the database migrations** link in step 4. The page scrolls to step 7. ## Checklist Check all before review: - [ ] I have read [CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md) - [ ] If I wrote a new docs topic or edited an existing topic, I used the `/write-the-docs` or `/edit-the-docs` skill, which applies the docs [style guide](https://github.com/supabase/supabase/tree/master/apps/docs/style-guide)
201 lines
6.3 KiB
Plaintext
201 lines
6.3 KiB
Plaintext
---
|
|
title: 'Use Supabase with Android Kotlin'
|
|
subtitle: 'Learn how to create a Supabase project, add some sample data to your database, and query the data from an Android Kotlin app.'
|
|
breadcrumb: 'Framework Quickstarts'
|
|
---
|
|
|
|
<AiPrompt id="kotlin" />
|
|
|
|
<$Partial path="quickstart_db_setup.mdx" />
|
|
|
|
## 3. Create an Android app with Android Studio [#create-an-app]
|
|
|
|
Select the **Android Studio > New > New Android Project** menu item.
|
|
|
|
## 4. Set up AI tooling (optional) [#set-up-ai-tooling]
|
|
|
|
<$Partial path="quickstart_ai_tooling.mdx" />
|
|
|
|
## 5. Install dependencies [#install-the-client-library]
|
|
|
|
Open the app module's `build.gradle.kts` file and add the serialization plugin, Ktor client, and Supabase client.
|
|
|
|
Replace `$kotlin_version` with your project's Kotlin version, and replace `$supabase_version` and `$ktor_version` with the latest supabase-kt and Ktor versions. For example, use Kotlin `2.2.21`, supabase-kt BOM `3.5.0`, and Ktor `3.0.3`.
|
|
|
|
<Admonition type="note">
|
|
|
|
If your project uses Kotlin 2.0 or later with Jetpack Compose, declare the Compose compiler plugin in your root `build.gradle.kts`:
|
|
|
|
```kotlin
|
|
id("org.jetbrains.kotlin.plugin.compose") version "$kotlin_version" apply false
|
|
```
|
|
|
|
Then apply it in the app module, without the version and without `apply false`:
|
|
|
|
```kotlin
|
|
id("org.jetbrains.kotlin.plugin.compose")
|
|
```
|
|
|
|
</Admonition>
|
|
|
|
<Admonition type="note">
|
|
|
|
You can find the latest supabase-kt version on the [supabase-kt releases page](https://github.com/supabase/supabase-kt/releases) on GitHub, and the latest Ktor version in the [Ktor documentation](https://ktor.io/docs/welcome.html). Match your Kotlin version to the supabase-kt release requirements.
|
|
|
|
</Admonition>
|
|
|
|
```kotlin
|
|
plugins {
|
|
...
|
|
kotlin("plugin.serialization") version "$kotlin_version"
|
|
}
|
|
...
|
|
dependencies {
|
|
...
|
|
implementation(platform("io.github.jan-tennert.supabase:bom:$supabase_version"))
|
|
implementation("io.github.jan-tennert.supabase:postgrest-kt")
|
|
implementation("io.ktor:ktor-client-android:$ktor_version")
|
|
}
|
|
```
|
|
|
|
## 6. Add internet access permission [#add-internet-access-permission]
|
|
|
|
In the `AndroidManifest.xml` file, add the following line under the `manifest` tag and outside the `application` tag.
|
|
|
|
```xml
|
|
...
|
|
<uses-permission android:name="android.permission.INTERNET" />
|
|
...
|
|
```
|
|
|
|
## 7. Create the Supabase client [#create-the-client]
|
|
|
|
You can create a Supabase client whenever you need to perform an API call.
|
|
|
|
For this example, create a client at the top of the `MainActivity.kt` file below the imports.
|
|
|
|
Replace the `supabaseUrl` and `supabaseKey` values with your own. Get them from the following helper, or from the project [**Connect** panel](/dashboard/project/_?showConnect=true&framework=androidkotlin&connectTab=mobiles):
|
|
|
|
<Button variant="primary" asChild>
|
|
<a href="/dashboard/project/_?showConnect=true&connectTab=mobiles&framework=androidkotlin">
|
|
Open Connect panel
|
|
</a>
|
|
</Button>
|
|
|
|
```kotlin
|
|
import io.github.jan.supabase.postgrest.from
|
|
import ...
|
|
|
|
val supabase = createSupabaseClient(
|
|
supabaseUrl = "YOUR_SUPABASE_URL",
|
|
supabaseKey = "YOUR_SUPABASE_PUBLISHABLE_KEY"
|
|
) {
|
|
install(Postgrest)
|
|
}
|
|
...
|
|
```
|
|
|
|
<$Partial path="api_settings.mdx" variables={{ "framework": "androidkotlin", "tab": "mobiles" }} />
|
|
|
|
<$Partial path="quickstart_mobile_env_note.mdx" />
|
|
|
|
## 8. Create a data model for instruments [#create-a-data-model-for-instruments]
|
|
|
|
Create a serializable data class to represent the data from the database.
|
|
|
|
In the `MainActivity.kt` file, add the following code below the `createSupabaseClient` function.
|
|
|
|
```kotlin
|
|
import kotlinx.serialization.Serializable
|
|
|
|
@Serializable
|
|
data class Instrument(
|
|
val id: Int,
|
|
val name: String,
|
|
)
|
|
```
|
|
|
|
## 9. Query data from the app [#query-data]
|
|
|
|
Use `LaunchedEffect` to fetch data from the database and display it in a `LazyColumn`.
|
|
|
|
Replace the default `MainActivity` class with the following code.
|
|
|
|
<Admonition type="note">
|
|
|
|
This example app makes a network request from the UI code. In production, use a `ViewModel` to separate the UI and data fetching logic.
|
|
|
|
</Admonition>
|
|
|
|
<Admonition type="note">
|
|
|
|
This snippet omits the theme wrapper that Android Studio generates, so it compiles whatever you named your project. The wrapper is named after your project, for example `MyAppTheme`. To apply your project's Material theme, wrap the `Surface` in the theme composable from `ui/theme/Theme.kt`.
|
|
|
|
</Admonition>
|
|
|
|
```kotlin
|
|
class MainActivity : ComponentActivity() {
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContent {
|
|
// A surface container using the 'background' color from the theme
|
|
Surface(
|
|
modifier = Modifier.fillMaxSize(),
|
|
color = MaterialTheme.colorScheme.background
|
|
) {
|
|
InstrumentsList()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@Composable
|
|
fun InstrumentsList() {
|
|
var instruments by remember { mutableStateOf<List<Instrument>>(listOf()) }
|
|
var error by remember { mutableStateOf<String?>(null) }
|
|
LaunchedEffect(Unit) {
|
|
withContext(Dispatchers.IO) {
|
|
try {
|
|
instruments = supabase.from("instruments")
|
|
.select().decodeList<Instrument>()
|
|
} catch (e: Exception) {
|
|
error = e.message
|
|
}
|
|
}
|
|
}
|
|
if (error != null) {
|
|
Text(
|
|
"Error loading instruments: $error",
|
|
modifier = Modifier.padding(8.dp),
|
|
)
|
|
return
|
|
}
|
|
LazyColumn {
|
|
items(
|
|
instruments,
|
|
key = { instrument -> instrument.id },
|
|
) { instrument ->
|
|
Text(
|
|
instrument.name,
|
|
modifier = Modifier.padding(8.dp),
|
|
)
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
## 10. Start the app [#start-the-app]
|
|
|
|
In Android Studio, click **Run app** to run the app on an emulator or a physical device.
|
|
|
|
<$Partial path="quickstart_going_to_production.mdx" />
|
|
|
|
## Next steps
|
|
|
|
Build on this app with the following resources:
|
|
|
|
- Learn how to build a complete user management app with authentication in the [Kotlin tutorial](/docs/guides/getting-started/tutorials/with-kotlin)
|
|
- Explore the [supabase-kt](https://github.com/supabase-community/supabase-kt) library on GitHub
|
|
- [Insert more data](/docs/guides/database/import-data) into your database
|
|
- Upload and serve static files using [Storage](/docs/guides/storage)
|