On this page
Everything inside an APK can be extracted: strings, BuildConfig fields, native libraries, "obfuscated" constants. If your app holds a provider API key, assume someone will find it. Secure AI integration starts from that assumption.
Two secure architectures
Option A: Firebase AI Logic + App Check
The SDK calls Gemini through Firebase's proxy. App Check, using Play Integrity on Android, attests that requests come from your genuine app on a genuine device, and Firebase documents enforcement for AI Logic from July 2026. Setup is in Gemini API Integration. Additional considerations:
- App Check reduces abuse but isn't user authentication. A genuine app can still be scripted, so add per-user limits where it matters.
- Consider limited-use App Check tokens for extra replay protection, if supported by your SDK version.
- Monitor usage and set budget alerts in Google Cloud.
Option B: your own backend proxy
interface AssistantApi {
@POST("v1/assistant/reply")
suspend fun reply(@Body body: AssistantRequest): AssistantResponse
}
@Serializable data class AssistantRequest(val conversationId: String, val message: String)
@Serializable data class AssistantResponse(val reply: String, val remainingToday: Int)
// Auth header (user ID token) and App Check / Integrity token are added by OkHttp interceptors.The backend's responsibilities:
- Authenticate the user (verify the ID token) and, ideally, the app (App Check or Play Integrity verdict).
- Enforce quotas: requests and tokens per user per day, plus global budgets.
- Own the prompt: system instructions, templates and tool definitions live server-side, not in the APK.
- Validate input: size limits, file types, and content checks.
- Call the provider with keys from a secret manager, with timeouts and retries.
- Filter output: moderation, schema validation, removal of sensitive data.
- Log safely: metadata (tokens, latency, model, user ID hash), not raw personal content by default.
Controls checklist
| Risk | Control |
|---|---|
| Key theft | No provider keys in the app; secret manager on the server; key rotation |
| Bill shock / abuse | Per-user quotas, rate limits, max tokens, budget alerts |
| Fake clients | App Check / Play Integrity; user authentication |
| Prompt injection | Server-owned prompts, delimited user input, tool allow-lists, output validation |
| Data leakage | Minimise data sent; redact; retention settings; disclosure and consent |
| Harmful output | Safety settings, moderation, reporting UI |
Client-side hygiene
- Never log prompts, responses or auth headers in release builds.
- Cap input size in the UI and in the request.
- Don't let model output trigger sensitive actions (payments, deletions, messages) without explicit user confirmation.
- Treat model output as untrusted when rendering (Markdown links, HTML).
Which option when
MVP chat feature with Gemini
Option A: fastest secure path, plus App Check and usage monitoring.
Assistant over private company data
Option B: retrieval and authorisation must happen server-side.
Multi-provider or model routing
Option B: switch providers without app releases.
Frequently asked questions
Isn't obfuscating the key enough?
No. Obfuscation slows attackers slightly, but keys can be recovered from the APK or intercepted at runtime. Keep keys on servers.
Can I restrict a Google API key to my Android app?
Some Google APIs support Android app restrictions (package name and certificate), but these are checks on request headers, not cryptographic attestation. Don't rely on them for costly AI APIs.
Does App Check stop all abuse?
It significantly raises the bar but isn't perfect. Combine it with authentication, quotas and monitoring.
Conclusion
Secure cloud AI on Android means no provider keys in the app, either through Firebase AI Logic with App Check or your own authenticated backend, plus quotas, server-owned prompts, input and output validation, and safe logging.
Next step: choose providers and models with Gemini vs OpenAI vs Open-Source Models.
Official references
๐ฑ Apps built by Yunok Tech
Real Android apps on Google Play, free to download.
Walzi4K wallpapers for lock and home screenGet it โธ
NotiVaultNotification history: recover dismissed notificationsGet it โธ
Ready to build real Android applications?
Every AI feature still runs inside an Android app that has to be fast, reliable and well structured. Strengthen the Kotlin, Jetpack Compose and architecture skills that AI integrations depend on, then apply these AI concepts with confidence.
Move beyond theory and strengthen your Android development skills with Yunoktech's Android course. Learn core concepts, practical implementation, modern Android development techniques, and the reasoning behind building maintainable applications.
Explore the Android course and take your next step as an Android developer.