← Back to list

Think Twice : Before You Ship AI-Generated Code to Production

A real story about Android Keystore, 4 million users, and why “looks correct” isn’t the same as “is safe.”

Romman Sabbir in Make Android · 2026-07-26 08:26 · 0 claps · 4.7 min read paywalled
#andorid #security #ai-code #android-keystore #learning
Open on Medium ↗
Wiki topics: AI · AI · General EDU · Education & Learning

Think Twice : Before You Ship AI-Generated Code to Production

A real story about Android Keystore, 4 million users, and why “looks correct” isn’t the same as “is safe.”

A few days ago, I found myself staring at a production crash that looked innocent enough:

java.security.InvalidKeyException: Keystore operation failed

SECURE_HW_COMMUNICATION_FAILED

Nothing unusual. Android Keystore. Hardware-backed encryption. One of those bugs that every Android engineer eventually meets.

Like many engineers in 2026, I did what most of us do nowadays — I asked AI.

Actually, I asked two AIs.

  • Both were incredibly confident.
  • Both sounded convincing.
  • Both were wrong.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

The Crash

The crash happened while initializing a Cipher using an AES key stored inside Android Keystore.

Cipher.init(...)

The underlying error was:

SECURE_HW_COMMUNICATION_FAILED

The application itself wasn’t doing anything exotic. It simply encrypted and decrypted a startup cache that restores the user’s logged-in state.

One important detail:

This code runs for more than 4 million users.

This isn’t a weekend side project.

The First AI Answer

The recommendation was straightforward:

Catch the InvalidKeyException, delete the Keystore key, generate a new one, and retry encryption.

At first glance, it sounded reasonable.

If the key is broken…

Generate another one.

Problem solved.

Right?

Except…

Deleting an AES key stored in Android Keystore means every single byte encrypted with that key instantly becomes useless forever.

AES isn’t magic.

A newly generated key under the same alias is still a completely different cryptographic key.

The old ciphertext can never be decrypted again.

If I had merged that code without thinking, I could have permanently destroyed local encrypted data for users whose devices hit this edge case.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

Then I Asked Another AI

The second AI didn’t recommend deleting the key immediately.

Instead, it proposed a much larger redesign.

  • Custom exceptions.
  • Recovery policies.
  • New architecture.
  • Startup state management.

While technically much safer, it also introduced complexity that simply wasn’t needed for the problem at hand.

Again, it sounded intelligent.

Again, it wasn’t the production fix I actually needed.

So What Was the Real Fix?

I stopped asking:

“How do I fix Android Keystore?”

Instead, I asked:

“What exactly failed?”

The stack trace already contained the answer.

Failed to begin operation
SECURE_HW_COMMUNICATION_FAILED

Notice what it didn’t say.

It never said:

  • key corrupted
  • key missing
  • key permanently invalidated

It simply failed to start communicating with secure hardware.

That’s a completely different problem.

The safest production patch turned out to be surprisingly boring.

Retry the operation.

That’s it.

  • No deleting keys.
  • No regenerating keys.
  • No changing aliases.
  • No changing encryption algorithms.
  • No redesigning the architecture.

Just retry the transient hardware failure.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

The Dangerous Part About AI

The scary thing wasn’t that AI made a mistake.

Humans make mistakes too.

The scary part was how confident it sounded.

There was no hesitation.

No uncertainty.

No warning saying:

“Deleting this key will permanently make existing encrypted data unreadable.”

Instead, the recommendation sounded almost official.

If I had been working on a personal side project, I probably would have merged it without thinking twice.

Production changes don’t deserve that level of trust.

Production Is Different

When you’re writing code for millions of users, success isn’t measured by whether the code compiles.

  • It’s measured by what happens six months later.
  • Production engineering isn’t about writing clever code.
  • It’s about minimizing risk.
  • Sometimes the correct fix is a single line.
  • Sometimes the correct fix is to do almost nothing.

Sometimes the correct fix is simply:

“Let’s collect more data before changing behavior.”

That’s engineering.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

AI Is an Amazing Junior Engineer

I’ve started thinking about AI differently.

  • It’s not a senior engineer.
  • It’s not an architect.
  • It’s not a principal engineer.
  • It’s more like an incredibly fast junior engineer.

It can:

  • write code
  • explain APIs
  • summarize documentation
  • generate tests
  • refactor repetitive code

But just like every junior engineer I’ve worked with, it also needs review.

  • Sometimes the review takes thirty seconds.
  • Sometimes it saves millions of users from a production issue.

Trust, But Verify

This experience reminded me of something we’ve always known.

  • Stack traces don’t lie.
  • Logs don’t lie.
  • Telemetry doesn’t lie.
  • Production metrics don’t lie.

AI, however, can confidently fill the gaps between those facts.

And that’s where engineers earn their salary.

My Rule Going Forward

I’ll continue using AI every day.

Probably more than ever.

But I’ve changed one habit.

Whenever AI recommends a production change, especially one involving:

  • encryption
  • authentication
  • databases
  • migrations
  • payments
  • file systems
  • security

I stop.

  • I reread the stack trace.
  • I reread the documentation.
  • I ask myself one question:

“If this recommendation is wrong, what is the worst thing that can happen to my users?”

If the answer is “they lose data,” then no amount of AI confidence is enough.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

Free Short Link and LinkInBio Page. COUPON — MEDIUM50 for 50% discount on any plan.

Final Thoughts

AI is one of the greatest engineering tools we’ve ever received.

But tools don’t carry responsibility.

Engineers do.

The next time an AI gives you production-ready code, don’t just ask:

“Will this fix the bug?”

Ask something far more important:

“What else could this break?”

Because in production, fixing the wrong problem can be much more expensive than the original bug.

And sometimes, the smartest thing an engineer can do is think twice before pressing Merge.

That’s it for today. Happy coding…


메타데이터
post_id
b895e32d830b
slug
think-twice-before-you-ship-ai-generated-code-to-production-b895e32d830b
url
https://medium.com/@rommansabbir/think-twice-before-you-ship-ai-generated-code-to-production-b895e32d830b
canonical_url
https://medium.com/@rommansabbir/think-twice-before-you-ship-ai-generated-code-to-production-b895e32d830b
author_url
https://medium.com/@rommansabbir
status
ok
fetched_at
2026-08-03 14:04:03