Can Android Keystore protect a free LLM API key inside a released APK?
No. If a shared free LLM API key is compiled into an Android app, Android Keystore cannot make that developer key private. Keystore can protect a key stored on a device, but a provider credential that a public app must use can still be extracted from the app package or observed when the app sends it. Keep a shared provider key on your server and have the app call an authenticated endpoint you control.
A free tier changes the amount you can spend, not who can use a key embedded in a public client. A copied key can consume the same account quota or trigger abuse rules. OpenAI says not to deploy an API key in mobile client environments and recommends routing requests through a backend. Android's security guide separately explains that compiled app resources can be decompiled and found. See the dated sources below.
Keystore and a Gradle secret solve different problems
Android Keystore is useful for device-specific cryptographic keys and data stored at rest. It is not a hiding place for one developer API key shared by every installation. If the app needs the shared key to make a provider request, the key or a usable equivalent must reach code the device controls.
Moving a value to a local Gradle properties file can keep it out of Git. That does not keep it out of the released APK if the build copies it into a generated BuildConfig field, a resource, or another package entry. Repository secrecy and binary secrecy are separate checks.
The same distinction applies to remote configuration. If an endpoint returns the shared provider key to every public app, an attacker can request it too. Encryption in transit protects the connection from some observers; it does not turn a reusable master key delivered to a public client into a server-only secret.
A fake-canary test for a release APK
Use a made-up marker, never a live key. This local check opens the APK as a ZIP archive and searches each unpacked entry for the exact test string. It makes no network request and does not print, upload, or inspect a real credential.
For a deliberately bad test build, place this fake value into a generated field and reference that field from a test screen so the compiler keeps it:
android {
buildFeatures {
buildConfig = true
}
defaultConfig {
val demoKey = "DEMO_FREE_LLM_API_KEY_NOT_A_REAL_SECRET"
buildConfigField("String", "LLM_API_KEY", "\"$demoKey\"")
}
}
Build the test APK, then run this scanner against its path:
from pathlib import Path
from zipfile import ZipFile
import sys
marker = b"DEMO_FREE_LLM_API_KEY_NOT_A_REAL_SECRET"
apk_path = Path(sys.argv[1])
hits = []
with ZipFile(apk_path) as apk:
for name in apk.namelist():
if not name.endswith("/") and marker in apk.read(name):
hits.append(name)
if hits:
print("FAIL: fake canary found in a packaged entry")
for name in hits:
print(" " + name)
raise SystemExit(1)
print("PASS: fake canary not found")
The deliberately bad build should fail and name at least one package entry. Now remove the field, build a second test APK, and run the same scanner with the same fake marker. The clean build should pass. If it does not, trace the reported entry before changing the release pipeline.
This fixture tests the scanner's ability to catch one known value. It does not prove that an APK contains no other secret, and a missing marker does not prove a backend is protected. Keep the marker synthetic, do not paste a real key into a shell command or report, and do not upload an APK for this check.
What the local result proves
A failed canary scan proves that this test string crossed the build boundary into at least one packaged file. It does not tell you whether a real key has been stolen. A passing scan proves only that the exact fake string was not found in the entries the scanner opened. It cannot detect a transformed, encoded, split, or different credential.
Use the artifact check as one release gate, not as a security claim. Also inspect the Android source and Gradle configuration for provider credentials, check generated resources and build constants, and make sure the production app has no direct Authorization header containing the shared provider key. Test with a fake marker in a fixture before relying on a release pipeline check.
If a real provider key was already shipped, remove the client-side use and rotate or revoke that provider key. A new APK does not invalidate a copy already extracted. Review provider usage and limits after rotation.
A safer request path for a public app
Use this flow:
- The app signs in the user to your service or obtains a scoped app session.
- The app sends the prompt to your endpoint over HTTPS without a provider key.
- Your endpoint checks the user, request size, allowed model route, and per-user rate or token budget.
- The server reads the provider key from a server-side secret store and forwards a bounded request.
- The endpoint returns only the result and safe metadata the app needs.
Do not build an open relay that accepts any URL, model, or unbounded prompt. Authentication and per-user limits protect your endpoint from being used as a free public proxy. They do not guarantee that a provider's free quota is available or that a request will succeed.
If direct client traffic is required for a provider feature, check whether that provider supports short-lived, scoped client credentials designed for mobile use. Use those only within their documented scope. Do not substitute the long-lived account key. Android's package and signing-certificate restrictions can reduce misuse for services that explicitly support them, but they do not make a general bearer key private inside every distributed app.
Quick answers
Does Keystore hide a key in BuildConfig?
No. Keystore can protect device-held cryptographic material; it cannot erase a shared provider key already compiled into the application package.
Does a Gradle secrets plugin solve APK exposure?
It can help keep secrets out of source control during development. If the secret is copied into a field or resource that ships in the APK, the package still contains it.
Can the app download the key after installation?
If a public app can request a reusable provider key from an endpoint, other callers can try the same endpoint. Prefer authenticated requests to your backend; keep the provider key on that backend.
Can I run the check without a real free API key?
Yes. The canary test uses only the fake marker shown above. It does not call a provider or consume quota.
Sources
- OpenAI API key safety, checked 2026-11-10.
- Android Developers security checklist: API key management, checked 2026-11-10.
- React Native developer question about retrieving and storing an API key in a mobile app, used as a question sample only; not used to claim prevalence or security behavior.