Lädt...

🔧 Securing and Injecting Google Maps API Key in an Android App


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

When developing an Android application, securely managing sensitive API keys such as the Google Maps API Key is crucial. Hardcoding API keys in strings.xml or source code poses a security risk, as they can be easily extracted from decompiled APKs. Instead, we use Gradle properties and resValue to inject the API key dynamically into the app while keeping it out of the source code repository.

In this blog post, we'll walk through how to securely manage and inject the Google Maps API key in an Android project, ensuring that it remains safe while still being accessible where needed.

Step 1: Store the API Key in secrets.properties

First, we store the API key in a local properties file that is not included in version control (Git) to prevent it from being leaked.

Create a file named secrets.properties in the root directory of your project.

Add the following line (replace with your actual key):

GOOGLE_MAP_API_KEY=your_actual_google_maps_api_key

Add secrets.properties to .gitignore to ensure it is not committed to the repository:

secrets.properties

Now, the API key is stored securely and will not be pushed to GitHub or shared in version control.

Step 2: Load the API Key in build.gradle.kts

Since we need to use this API key within Gradle, we need to load it from secrets.properties.

Modify the build.gradle.kts file and add the following snippet before the android block:

val localProperties = Properties()
val localPropertiesFile = File(rootDir, "secrets.properties")
if (localPropertiesFile.exists() && localPropertiesFile.isFile) {
    localPropertiesFile.inputStream().use {
        localProperties.load(it)
    }
}

This snippet:

Loads the secrets.properties file at runtime.

Extracts the GOOGLE_MAP_API_KEY value.

Ensures the file exists before trying to read it, preventing crashes.

This must be placed at the top of build.gradle.kts so that the properties are available before any build configurations.

Step 3: Inject the API Key into res/values/strings.xml Using resValue

Instead of manually adding the API key to strings.xml, we use resValue in Gradle to inject it dynamically.

Modify your build.gradle.kts inside the android block:

android {
    buildTypes {
        getByName("debug") {
            val googleMapsApiKey = localProperties.getProperty("GOOGLE_MAP_API_KEY") ?: ""
            resValue("string", "google_maps_key", googleMapsApiKey)
        }

        getByName("release") {
            val googleMapsApiKey = localProperties.getProperty("GOOGLE_MAP_API_KEY") ?: ""
            resValue("string", "google_maps_key", googleMapsApiKey)
        }
    }
}

How resValue Works

It injects the API key into the compiled res/values/strings.xml at build time.

The key never appears in source code or XML files, reducing security risks.

The generated key can be accessed using @string/google_maps_key

Step 4: Use the API Key in AndroidManifest.xml

Since resValue makes google_maps_key available as a string resource, you can now reference it in AndroidManifest.xml:

<meta-data
    android:name="com.google.android.geo.API_KEY"
    android:value="@string/google_maps_key" />

This ensures that Google Maps SDK can access the API key without hardcoding it anywhere in your project.

Step 5: Access the API Key in Kotlin Code (If Needed)

If you need to use the API key in Kotlin (for example, to initialize Google Maps dynamically), you can retrieve it as follows:

val apiKey = context.getString(R.string.google_maps_key)

This allows you to access the API key programmatically while keeping it securely stored.

...

🔧 Securing and Injecting Google Maps API Key in an Android App


📈 51.55 Punkte
🔧 Programmierung

🔧 Securing a .NET API C#: API Key, Basic Authentication, and JWT 🔒


📈 25.78 Punkte
🔧 Programmierung

🕵️ Inspect App sandbox data in desktop Chrome browser by injecting stetho in Android Apps


📈 23.12 Punkte
🕵️ Reverse Engineering

🔧 Injecting Environment Variables in a Deployed React App Without Rebuilding.


📈 21.03 Punkte
🔧 Programmierung

🔧 LLM Models and RAG Applications Step-by-Step - Part III - Searching and Injecting Context


📈 20.87 Punkte
🔧 Programmierung

🕵️ Stripo Inc: Non-revoked API Key Disclosure in a Disclosed API Key Disclosure Report on Stripo


📈 20.79 Punkte
🕵️ Sicherheitslücken

📰 Google Maps's Moat: How Far Ahead of Apple Maps is Google Maps?


📈 20.4 Punkte
📰 IT Security Nachrichten

📰 Google Maps: So schlägt sich das neue Apple Maps Look Around gegen Google Maps Streetview (Video)


📈 20.4 Punkte
📰 IT Nachrichten

📰 Google to Block Third-Party Software from Injecting Code into Chrome Browser


📈 20.18 Punkte
📰 IT Security Nachrichten

📰 Google Will Block Third-Party Software From Injecting Code Into Chrome


📈 20.18 Punkte
📰 IT Security Nachrichten

📰 Malware Spotted Injecting Bing Results Into Google Searches


📈 20.18 Punkte
📰 IT Security Nachrichten

📰 AiroAV Antivirus Identifies Mac OS Malware – Injecting Bing Results into Google Search


📈 20.18 Punkte
📰 IT Security Nachrichten

🔧 MuleSoft API Gateway: Securing and Optimizing API Management


📈 20.06 Punkte
🔧 Programmierung

🔧 [Part 3/3] Securing APIs using JSON Web Token (JWT) in IBM API-Connect v10 using X.509 RSA key pair


📈 19.96 Punkte
🔧 Programmierung

📰 Chinese ISPs Caught Injecting Ads and Malware into Web Pages


📈 19.73 Punkte
📰 IT Security Nachrichten

📰 Chinese ISPs Caught Injecting Ads and Malware into Web Pages


📈 19.73 Punkte
📰 IT Security Nachrichten

📰 ISPs Caught Injecting Cryptocurrency Miners and Spyware In Some Countries


📈 19.73 Punkte
📰 IT Security Nachrichten

🕵️ Developer Pleads Guilty to Injecting Malware and Crippling Company Systems


📈 19.73 Punkte
🕵️ Hacking

🔧 Injecting Real-Time WebSocket Streams into SQLite Using Python and WAL Hooking


📈 19.73 Punkte
🔧 Programmierung

📰 When it comes to technology, securing your future means securing your present


📈 19.13 Punkte
📰 IT Security Nachrichten

⚠️ [papers] - Injecting SQLite Database Based Applications


📈 18.59 Punkte
⚠️ PoC

📰 Fileless, Code-Injecting Ransomware SOREBRECT Emerges


📈 18.59 Punkte
📰 IT Security Nachrichten

📰 Facebook Messenger Globally Tests Injecting Display Ads Into Inbox


📈 18.59 Punkte
📰 IT Security Nachrichten

📰 Chrome to Block Apps from Injecting into Its Processes


📈 18.59 Punkte
📰 IT Security Nachrichten

matomo