Skip to content

Set up Supabase API key and Secret

Hieu Vu edited this page Apr 8, 2023 · 2 revisions

Set up API key and Secret securely

API key and secret from any services are credential information, we should not want to expose these to others because some strangers will use it for bad purpose

1. Create local environment secret

Create or edit local.properties file at top level (same level at build.gradle of the project).

Note: This file should not be pushed directly to the repository, meaning that you should include this in .gitignore file

API_KEY=YOUR_SUPABASE_API_KEY
SECRET=YOUR_SUPABASE_SECRET
SUPABASE_URL=YOUR_SUPABASE_URL

2. Read and set to BuildConfig

In build.gradle (app), create Properties object and read value from local.properties set value for BuildConfig by calling buildConfigField method

defaultConfig {
   applicationId "com.hieuwu.groceriesstore"
   minSdkVersion 22
   targetSdkVersion 33
   versionCode 5
   versionName "1.0"
   testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

   // Set value part
   Properties properties = new Properties()
   properties.load(project.rootProject.file("local.properties").newDataInputStream())
   buildConfigField("String", "API_KEY", "\"${properties.getProperty("API_KEY")}\"")
   buildConfigField("String", "SECRET", "\"${properties.getProperty("SECRET")}\"")
   buildConfigField("String", "SUPABASE_URL", "\"${properties.getProperty("SUPABASE_URL")}\"")
}

4. Use value from BuildConfig

Read value from BuildConfig (Remember to import the right class, it is one from your project). For example:

val url = BuildConfig.SUPABASE_URL
val apiKey = BuildConfig.API_KEY