Use Supabase with Android Kotlin
Learn how to create a Supabase project, add some sample data to your database, and query the data from an Android Kotlin app.
Create a Supabase project
Go to database.new and create a new Supabase project.
When your project is up and running, go to the Table Editor, create a new table and insert some data.
Alternatively, you can run the following snippet in your project's SQL Editor. This will create a countries
table with some sample data.
Make the data in your table publicly readable by adding an RLS policy:
Create an Android app with Android Studio
Open Android Studio > New > New Android Project.
Install the Dependencies
Open build.gradle.kts
(app) file and add the serialization plug, 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.
The latest supabase-kt version can be found here and Ktor version can be found here.
_11plugins {_11 ..._11 kotlin("plugin.serialization") version "$kotlin_version"_11}_11..._11dependencies {_11 ..._11 implementation(platform("io.github.jan-tennert.supabase:bom:$supabase_version"))_11 implementation("io.github.jan-tennert.supabase:postgrest-kt")_11 implementation("io.ktor:ktor-client-android:$ktor_version")_11}
Add internet access permission
Add the following line to the AndroidManifest.xml
file under the manifest
tag and outside the application
tag.
_10..._10<uses-permission android:name="android.permission.INTERNET" />_10...
Initialize the Supabase client
You can create a Supabase client whenever you need to perform an API call.
For the sake of simplicity, we will create a client in the MainActivity.kt
file at the top just below the imports.
Replace the supabaseUrl
and supabaseKey
with your own:
Project URL
Anon key
_10import ..._10_10val supabase = createSupabaseClient(_10 supabaseUrl = "https://xyzcompany.supabase.co",_10 supabaseKey = "your_public_anon_key"_10 ) {_10 install(Postgrest)_10}_10...
Create a data model for countries
Create a serializable data class to represent the data from the database.
Add the following below the createSupabaseClient
function in the MainActivity.kt
file.
_10@Serializable_10data class Country(_10 val id: Int,_10 val name: String,_10)
Query data from the app
Use LaunchedEffect
to fetch data from the database and display it in a LazyColumn
.
Replace the default MainActivity
class with the following code.
Note that we are making a network request from our UI code. In production, you should probably use a ViewModel
to separate the UI and data fetching logic.
_38class MainActivity : ComponentActivity() {_38 override fun onCreate(savedInstanceState: Bundle?) {_38 super.onCreate(savedInstanceState)_38 setContent {_38 SupabaseTutorialTheme {_38 // A surface container using the 'background' color from the theme_38 Surface(_38 modifier = Modifier.fillMaxSize(),_38 color = MaterialTheme.colorScheme.background_38 ) {_38 CountriesList()_38 }_38 }_38 }_38 }_38}_38_38@Composable_38fun CountriesList() {_38 var countries by remember { mutableStateOf<List<Country>>(listOf()) }_38 LaunchedEffect(Unit) {_38 withContext(Dispatchers.IO) {_38 countries = supabase.from("countries")_38 .select().decodeList<Country>()_38 }_38 }_38 LazyColumn {_38 items(_38 countries,_38 key = { country -> country.id },_38 ) { country ->_38 Text(_38 country.name,_38 modifier = Modifier.padding(8.dp),_38 )_38 }_38 }_38}
Start the app
Run the app on an emulator or a physical device by clicking the Run app
button in Android Studio.