When I added an AI assistant to this portfolio, the first version I wrote in my head was the simple one. Put the Gemini API key in a local properties file, read it into BuildConfig, call the Gemini API straight from the app. It would have worked. It also would have been a mistake, and it is a mistake I see in a lot of side projects and even a few production apps.
The problem is not that Android hides things badly. The problem is that Android does not hide things at all. Anything you compile into an APK ships to every single person who installs the app. That includes your resources, your strings, and yes, your BuildConfig fields. A release build with ProGuard or R8 will rename your classes and strip unused code, but it does not encrypt string constants sitting in the bytecode. Anyone with the APK file, which is just a file, can run it through a decompiler like jadx and get back readable source in a few minutes. String search for "key" or "api" and you are usually done.
This is not a theoretical risk. Researchers pull hardcoded keys out of shipped apps often enough that it barely makes news anymore, and it has happened to teams with far more security review than a solo developer working on a portfolio project. If it can happen to apps built by large teams, it was certainly going to happen to mine.
What a hardcoded key actually costs you
Once a key is extracted, whoever has it can call the API as you, using your quota and your billing. With a free tier API like the one I was using, the immediate cost is small, but the failure mode is not. Someone finds the key, writes a script, and burns through your daily quota in minutes, and your own app stops working for real users until the quota resets. If the key belongs to a paid API, the cost is a lot less abstract.
Restricting the key by package name or by a signing certificate helps a little on some platforms, but it is not a real fix. It slows down casual extraction, it does not stop someone who is actually trying. The key still exists in plain text inside the app, and plain text inside the app is not a secret.
The fix: never ship the key at all
The only real fix is to stop shipping the secret to the client in the first place. The Android app should never hold a key that lets it talk to a paid or rate limited API directly. Instead, the app talks to a small backend that you control, and that backend holds the key. The client sends a request with no credentials attached beyond whatever session or app level auth you choose to add, your backend attaches the real API key server side, calls the third party API, and hands back only the result.
This sounds like it requires standing up a server, which is usually the part that makes people skip it. It does not, not anymore. I used a Cloudflare Worker, and it is a good fit for this specific problem for a few reasons. It is free at the scale a personal project or early stage app runs at. It deploys in seconds from the command line. It does not need a server to manage, patch, or pay for every month. And the API key lives as an encrypted environment secret on Cloudflare's side, never in your git history, never in your APK.
Here is roughly what that Worker looks like, stripped down to the part that matters:
// Cloudflare Worker API proxy
export default {
async fetch(request, env) {
const { message } = await request.json();
const response = await fetch(
`https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash-lite:generateContent?key=${env.GEMINI_API_KEY}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
contents: [{ role: "user", parts: [{ text: message }] }],
}),
}
);
const data = await response.json();
return new Response(JSON.stringify(data));
},
};
The Android app never sees GEMINI_API_KEY. It never sees any key at all. It sends a plain message to my Worker's URL, the Worker attaches the secret, calls Gemini, and returns the answer. If someone decompiles the app, all they find is a URL pointing at my Worker. There is nothing to steal there worth stealing, because the Worker itself has its own protections, rate limiting and request validation, that I control independently of anything shipped to a device.
This same pattern applies past API keys
Once you start thinking about it this way, a lot of other things you might be tempted to put in an app fall under the same rule. Payment processor secret keys, admin level Firebase credentials, third party service tokens with write access, none of these belong on a device you do not control after it leaves your hands. The rule I use now is simple. If a credential can spend money, delete data, or impersonate a real backend, it stays on a server I control, never in the client.
For read only, low sensitivity API keys, like a public maps key that is already restricted by domain or package and has no real cost if abused, hardcoding is usually fine. The line is not "is this an API key," it is "what happens if a stranger gets this specific key." If the answer involves your quota, your bill, or your users' data, put a server between the app and the key.
Building the proxy took me an afternoon, most of it spent on Cloudflare's docs rather than actual coding. It has been running without issue since, and it means I never have to think about someone quietly draining my API quota through a decompiled APK sitting on a file sharing site somewhere. That trade felt worth a few hours of setup.