Compose Secure Api Key is a simple Android project demonstrating how to securely inject and
manage API keys using Jetpack Compose Material 3 and the local.properties file. It follows a
modern Android setup with Kotlin, Gradle 8+, and buildConfigField for runtime access to
secrets—without exposing them in version control.
GitHub: https://github.com/ralphmarondev/compose-secure-api-key
- 🔐 Securely inject API keys using
local.properties - 🧪 Use
BuildConfigto access secrets at runtime - 💡 Jetpack Compose Material 3 setup
- 🛡️ Keeps API keys out of version control (safely ignored by
.gitignore) - ⚙️ Clean and modern Gradle + Kotlin DSL configuration
git clone https://github.com/ralphmarondev/compose-secure-api-key.git
cd compose-secure-api-keyIn the root of your project, edit or create a local.properties file:
API_KEY=YourSecureKeyHere
BuildConfig.
Example:
API_KEY=Hello
- Open the root project folder
- Wait for Gradle to sync
- Run the app on your connected Android device or emulator
val apiKey = BuildConfig.API_KEYThis allows you to securely use your API key anywhere in your Compose app without hardcoding it in source files.
To make this work, you'll need to load the API key from local.properties and pass it to
BuildConfig.
Add the following inside the defaultConfig block of your build.gradle.kts file:
val localProperties = java.util.Properties()
val localPropertiesFile = project.rootProject.file("local.properties")
if (localPropertiesFile.exists()) {
localProperties.load(localPropertiesFile.inputStream())
}
buildConfigField("String", "API_KEY", "\"${localProperties.getProperty("API_KEY")}\"")This reads the API_KEY from your local machine and injects it into the generated
BuildConfig.java, allowing safe access from your Kotlin code via:
val apiKey = BuildConfig.API_KEY✅ Bonus: You can define multiple keys the same way:
buildConfigField("String", "API_KEY_ONE", "\"${localProperties.getProperty("API_KEY_ONE")}\"")
buildConfigField("String", "API_KEY_TWO", "\"${localProperties.getProperty("API_KEY_TWO")}\"")This project is licensed under the MIT License. See the LICENSE file for full details.
Ralph Maron Eda GitHub: @ralphmarondev
Suggestions, feedback, and contributions are welcome! Feel free to fork the project or open a pull request.
⭐ If you found this helpful, consider starring the repo!