Skip to content

Securing api key on jetpack compose by putting it on local.properties

License

maronworks/compose-secure-api-key

Repository files navigation

🔐 Compose Secure Api Key

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.

License Platform UI Kotlin Gradle Android SDK


🔗 Repository

GitHub: https://github.com/ralphmarondev/compose-secure-api-key


✨ Features

  • 🔐 Securely inject API keys using local.properties
  • 🧪 Use BuildConfig to 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

🛠️ Getting Started

1. Clone the Repository

git clone https://github.com/ralphmarondev/compose-secure-api-key.git
cd compose-secure-api-key

2. Set Up local.properties

In the root of your project, edit or create a local.properties file:

API_KEY=YourSecureKeyHere

⚠️ Do not add quotes. The build script will wrap it properly for BuildConfig.

Example:

API_KEY=Hello

3. Open in Android Studio

  • Open the root project folder
  • Wait for Gradle to sync
  • Run the app on your connected Android device or emulator

💬 Usage in Code

val apiKey = BuildConfig.API_KEY

This allows you to securely use your API key anywhere in your Compose app without hardcoding it in source files.


🧩 Gradle Setup Snippet

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")}\"")

📄 License

This project is licensed under the MIT License. See the LICENSE file for full details.


👤 Author

Ralph Maron Eda GitHub: @ralphmarondev


🤝 Contributing

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!

About

Securing api key on jetpack compose by putting it on local.properties

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages