Securing API Keys in Android with Gradle: Safeguarding Sensitive Information

Abhishek Dubey
4 min readFeb 5, 2020

--

Photo by franckinjapan on Unsplash

Introduction:
When working on Android projects, there are often situations where we need to securely store API keys for third-party SDKs or other resource keys. In this article, we will explore a method to safely store API keys using the gradle.properties file in Android Studio. By leveraging this approach, we can keep sensitive information separate from our codebase and minimize the risk of exposing sensitive data.

Step 1: Adding gradle.properties to .gitignore 👥

To safeguard our API keys and prevent them from being exposed in version control, we need to add the gradle.properties file to the .gitignore configuration. This ensures that the sensitive information remains confidential and is not tracked by Git. Follow these steps to exclude the gradle.properties file from version control:

  1. In your Android Studio project, locate the .gitignore file. It is typically found in the root directory of your project.
  2. Open the .gitignore file in a text editor or directly within Android Studio.
  3. Append the following line to the file: gradle.properties

This line instructs Git to ignore the gradle.properties file when committing changes.

4. Save the .gitignore file.

By adding gradle.properties to the .gitignore file, we ensure that the file is not shared or visible in the version control system. This step helps maintain the confidentiality of our API keys and other sensitive data.

Step 2: Storing API Keys in gradle.properties 🔒

Now that we have taken the necessary precaution to prevent the gradle.properties file from being tracked by Git, we can proceed to store our API keys securely. The gradle.properties file allows us to define custom key-value pairs, making it an ideal place to store sensitive information. Follow these steps to store your API keys:

  1. Locate the gradle.properties file in your Android project. It is usually located in the root directory of the project.
  2. Open the gradle.properties file in a text editor or directly within Android Studio.
  3. Add a new line to the file using the following format: API_KEY="YOUR_API_KEY". Replace YOUR_API_KEY with your actual API key value. You can define multiple keys by adding additional lines in the same format.

Replace YOUR_API_KEY with your actual API key value. You can define multiple keys by adding additional lines in the same format.

4. Save the gradle.properties file.

By storing our API keys in the gradle.properties file, we separate them from our source code, reducing the risk of exposing sensitive information. This approach allows us to easily manage and update our keys without modifying the codebase.

Step 3: Accessing API Keys in the Code 📲

After securely storing our API keys, we need to access them in our code. Follow these steps to retrieve the API keys stored in the gradle.properties file:

  1. Open your app’s build.gradlefile (usually located in the app module).
  2. Inside the android block, add the following code snippet:

This code creates a build configuration field named API_KEY of type String and assigns the value stored in the gradle.properties file.

3. Save the build.gradlefile.

4. To access the API key in your code, use the following:

The BuildConfig class is automatically generated by the Android build system, and it provides access to the build configuration fields defined in the build.gradle file.

By following these steps, we can securely store our API keys and access them in our Android code without exposing them directly. This helps protect our sensitive information and reduces the risk of unauthorized access or misuse.

Conclusion: Securing API keys and other sensitive information is crucial for Android developers. By leveraging the gradle.properties file and properly configuring our project, we can separate our API keys from the codebase and minimize the risk of exposing sensitive data. By following these best practices, we can ensure the security and integrity of our Android applications.

Hit clap if you like what you read. :)

--

--

Abhishek Dubey

Crafting captivating digital experiences through the artistry of programming.