Getting Started

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.

1

Set up a Supabase project

Create a new project in the Supabase Dashboard.

After your project is ready, create a table in your Supabase database using the SQL Editor in the Dashboard. Use the following SQL statement to create all tables.

SQL_EDITOR

_10
-- Create the table
_10
CREATE TABLE countries (
_10
id SERIAL PRIMARY KEY,
_10
name VARCHAR(255) NOT NULL
_10
);
_10
-- Insert some sample data into the table
_10
INSERT INTO countries (name) VALUES ('United States');
_10
INSERT INTO countries (name) VALUES ('Canada');
_10
INSERT INTO countries (name) VALUES ('Mexico');

2

Create an Android app with Android Studio

Open Android Studio > New > New Android Project.

3

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.


_11
plugins {
_11
...
_11
kotlin("plugin.serialization") version "$kotlin_version"
_11
}
_11
...
_11
dependencies {
_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
}

4

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
...

5

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

_10
import ...
_10
_10
val supabase = createSupabaseClient(
_10
supabaseUrl = "https://xyzcompany.supabase.co",
_10
supabaseKey = "your_public_anon_key"
_10
) {
_10
install(Postgrest)
_10
}
_10
...

6

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
_10
data class Country(
_10
val id: Int,
_10
val name: String,
_10
)

7

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.


_38
class 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
_38
fun 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
}

8

Start the app

Run the app on an emulator or a physical device by clicking the Run app button in Android Studio.