apps/docs/content/guides/getting-started/quickstarts/kotlin.mdx
<$Partial path="quickstart_db_setup.mdx" />
Select the Android Studio > New > New Android Project menu item.
Supabase's Agent Skills is a curated set of instructions that give your AI agent procedural knowledge about working with Supabase.
Install them so your AI coding agent can produce more accurate, reliable code using current Supabase patterns, such as authentication, server-side rendering, and database migrations, rather than relying solely on training data.
To install, run the following command in the root of your project:
npx skills add supabase/agent-skills
Open build.gradle.kts (app) file and add the serialization plugin, Ktor client, and Supabase client.
Replace the version placeholders $kotlin_version with the Kotlin version of the project, and $supabase_version and $ktor_version with the respective latest versions.
You can find the latest supabase-kt version on GitHub and Ktor in the Ktor documentation.
</Admonition>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")
}
Add the following line to the AndroidManifest.xml file under the manifest tag and outside the application tag.
...
<uses-permission android:name="android.permission.INTERNET" />
...
You can create a Supabase client whenever you need to perform an API call.
For a quick example, create a client at the top of the MainActivity.kt file below the imports.
Replace the supabaseUrl and supabaseKey with your own, which you can get from the helper below, or from the project Connect panel:
import ...
val supabase = createSupabaseClient(
supabaseUrl = "https://xyzcompany.supabase.co",
supabaseKey = "your_publishable_key"
) {
install(Postgrest)
}
...
<$Partial path="api_settings.mdx" variables={{ "framework": "androidkotlin", "tab": "mobiles" }} />
Create a serializable data class to represent the data from the database.
Add the following below the createSupabaseClient function in the MainActivity.kt file.
@Serializable
data class Instrument(
val id: Int,
val name: String,
)
Use LaunchedEffect to fetch data from the database and display it in a LazyColumn.
Replace the default MainActivity class with the following code.
This example application makes a network request from the UI code. In production, you should use a ViewModel to separate the UI and data fetching logic.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SupabaseTutorialTheme {
// 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()) }
LaunchedEffect(Unit) {
withContext(Dispatchers.IO) {
instruments = supabase.from("instruments")
.select().decodeList<Instrument>()
}
}
LazyColumn {
items(
instruments,
key = { instrument -> instrument.id },
) { instrument ->
Text(
instrument.name,
modifier = Modifier.padding(8.dp),
)
}
}
}
Run the app on an emulator or a physical device by clicking the Run app button in Android Studio.