Originally published on Medium:
https://medium.com/@supsabhi/exploring-supabase-for-android-a-modern-alternative-to-firebase-f8a61ddb886b
For years, Firebase has been the go-to solution for Android developers needing user authentication, simplified backend development, and features like push notifications. It’s been so popular that, for many, it was the only real choice. But now, there’s a new contender in the arena: Supabase.
What is Supabase?
Founded in 2020, Supabase is an open-source, serverless platform designed to make backend development easier and more flexible. It serves as a powerful alternative to Firebase, offering a robust set of features that cover everything from authentication to real-time data updates.
If you’re an Android developer, you’ll appreciate that Supabase is built on top of PostgreSQL, a relational database. This means your data is stored in tables with rows and columns, and you can use full-fledged SQL queries — a big difference from Firebase’s document-based, JSON storage.
Key Features of Supabase
Multiple Authentication Options: Supports email/password, social logins, and more.
Real-Time Updates: Perfect for apps that need live data synchronization.
Push Notifications: Easy to set up and integrate.
Strong Security: Leverages PostgreSQL’s Row-Level Security (RLS) for fine-grained access control.
Easy Setup: Designed for quick onboarding and minimal configuration.
Getting Started with Supabase on Android
To use Supabase in your Android project, you’ll need to include the Supabase libraries and a networking client — most commonly, Ktor. If you’re not familiar with Ktor, I’ve written a detailed guide on how to integrate it into your project, which you can find here.
Adding Dependencies
In your app-level build.gradle, add the following:
plugins {
kotlin("plugin.serialization")
}
dependencies {
// Navigation
implementation(libs.androidx.navigation.compose)
// Network Image
implementation(libs.coil.compose)
// Supabase
implementation(platform(libs.supabase.bom))
implementation(libs.realtime.kt)
implementation(libs.postgrest.kt)
implementation(libs.ktor.client.android)
implementation(libs.kotlinx.serialization.json)
}
And in your project-level build.gradle:
plugins {
kotlin("jvm") version "2.0.0"
kotlin("plugin.serialization") version "2.0.0"
}
Setting Up Your Database
Create a Supabase Account:
Go to supabase.com and create a new project.
Create a Table:
For this example, let’s make a simple “vehicle” table with the following columns:
id (Primary key, auto-increment)
name (Text)
model (Text)
year (Integer)
For simplicity, we’ll skip enabling Row-Level Security (RLS) for now, but we’ll enable real-time updates.
Creating the Data Model
Back in your Android project, let’s define a data class Vehicle
data class Vehicle(
val id: String? = null,
val name: String,
val model: String,
val year: Int,
)
Initializing the Supabase Client
In MainActivity, let’s set up the Supabase client
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
SupaVehicleAppTheme {
val supabaseClient = createSupabaseClient(
supabaseKey = "YOUR_SUPABASE_KEY",
supabaseUrl = "YOUR_SUPABASE_URL"
) {
install(Realtime)
install(Postgrest)
httpConfig {
this.install(WebSockets)
}
}
}
}
}
}
You’ll find your project’s API key and URL in the Supabase dashboard under the project API information section.
Performing CRUD Operations
Here’s how you can interact with your Supabase database from your Android app:
Fetch Vehicles:
suspend fun getVehicleList() =
supabase.from("vehicle")
.select()
.decodeList<Vehicle>()
Insert a Vehicle:
suspend fun insertVehicle(vehicleToAdd: Vehicle) {
supabase.from("vehicle").insert(vehicleToAdd)
}
Delete a Vehicle:
suspend fun deleteVehicle(vehicleToDelete: Vehicle) {
supabase.from("vehicle").delete {
filter {
Vehicle::id eq vehicleToDelete.id
}
}
}
Update a Vehicle:
suspend fun updateVehicle(vehicleToUpdate: Vehicle) {
supabase.from("vehicle").update(
{
set("name", vehicleToUpdate.name)
}
) {
filter {
eq("id", vehicleToUpdate.id)
}
}
}
Upsert a Vehicle:
Upsert is a handy operation that either inserts a new row or updates an existing one if it already exists.
suspend fun upsertVehicle(vehicleToUpsert: Vehicle) {
supabase.from("vehicle").upsert(vehicleToUpsert)
}
Wrapping Up
With these basic operations, you can manage your data in Supabase and display it in your app as needed. Supabase is a flexible, open-source alternative to Firebase, especially if you prefer relational databases and full SQL support.From managing real-time data to implementing secure authentication, Supabase offers a modern and developer-friendly backend solution.
I hope this guide helps you get started with Supabase on Android. If you have any questions or want to dive deeper, feel free to reach out!
Top comments (0)