An Android app cannot keep a secret that ships inside it: values in BuildConfig, resources or local.properties-generated fields are compiled into the APK and can be recovered by decompiling it. Keep third-party secret keys on a server. Protect tokens received at run time with a key held in the Android Keystore.
Read the key from the environment
import android.security.keystore.KeyProperties
import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
fun decryptToken(iv: ByteArray, encrypted: ByteArray): String {
val keyStore = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
val key = keyStore.getKey("token_key", null) as SecretKey
val cipher = Cipher.getInstance("AES/GCM/NoPadding")
cipher.init(Cipher.DECRYPT_MODE, key, GCMParameterSpec(128, iv))
return String(cipher.doFinal(encrypted), Charsets.UTF_8)
}Local development
Developers often put keys in local.properties (git-ignored) and surface them through buildConfigField or a Gradle plugin. That keeps them out of Git but not out of the shipped app. Use it only for non-secret or restricted client identifiers, such as an API key you have limited by package name and signing certificate in the provider's console. Debug with test-mode keys.
Production
Sign users in to your backend (for example with Credential Manager and OAuth with PKCE) and keep the returned tokens encrypted with a non-exportable key generated in the Android Keystore, which keeps key material in secure hardware where available. Store the ciphertext in app-private storage such as DataStore. Keep powerful provider credentials on the server and verify app integrity with the Play Integrity API when needed.
Mistakes to avoid
- Putting a secret in
BuildConfig,strings.xmlorgradle.properties: it is compiled into the APK and extractable, and obfuscation with R8 does not hide string values. - Storing tokens in plain
SharedPreferences, which is readable on rooted devices and from backups. - Generating Keystore keys without user-authentication or
setUnlockedDeviceRequiredwhere tokens are highly sensitive. - Leaving verbose HTTP logging (for example OkHttp
BODYlevel) enabled in release builds, which logs tokens.
When an AI agent works on your Android (Kotlin) project
Coding agents run inside your project folder, so they can read the same .env file your app reads. BotPassword injects keys at run time instead: the agent calls the run_with intent through the MCP server and the process gets the variables it needs while the agent sees only redacted output. Planned: bpw run, a CLI form of the same.
Frequently asked questions
Is BuildConfig safe for API keys on Android?
No. BuildConfig fields are compiled into the app and can be extracted from the APK. Use them only for non-secret configuration, and keep secret keys on your server.
How do I store a token securely on Android?
Encrypt it with a key generated in the Android Keystore and keep the ciphertext in app-private storage. The key cannot be exported from the Keystore, so a copy of the storage alone is not enough to read it.
Should I keep API keys in a .env file for Android (Kotlin)?
A .env file that is git-ignored is fine for local development, but every process and every AI coding agent running in that folder can read it. Load secrets into the environment at run time from a password manager or secrets manager instead, so no plaintext file sits in the project.