跪拜 Guibai
← All articles
Android

AI Makes Code Cheap But Judgment Expensive: The Coroutine Mistakes That Break Android Apps

By 潜龙勿用之化骨龙 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Teams that ship AI-generated Android code will hit these exact failures: invisible coroutine ownership, blocked threads, swallowed cancellation, and leaked exceptions. The rules here are what separate a demo from a production system.

Summary

AI has pushed the marginal cost of writing code to near zero, but it has not made the code correct. The coroutine and architecture pitfalls that trip up generated Android code are now well catalogued: launch is used as a thread-switch, repositories launch their own coroutines, and CancellationException gets swallowed in a generic catch block. The result is code that compiles, passes unit tests, and then ANRs or leaks once it hits production.

The fix is a set of design contracts. Data layers must be main-safe, meaning they own their blocking work and expose suspend functions or cold flows. Repositories expose suspend, not launch, so the caller controls lifecycle and cancellation. Blocking Java primitives like BlockingQueue and ReentrantLock have no place in suspend chains; channel and mutex suspend instead. Exceptions are mapped at the edge of the data layer, and ViewModel only turns results into UI states. Cold flows are the default, with stateIn reserved for cases that genuinely share a hot source.

The deeper point is that AI produces locally correct code. What it cannot produce is the judgment about ownership, lifecycle, and error boundaries. Those decisions still fall on the engineer, and they are exactly the ones that determine whether the system survives real use.

Takeaways
— launch creates a new coroutine task; withContext switches the dispatcher of the current coroutine. Using launch(Dispatchers.IO) just to call a suspend function conflates task creation with thread switching.
— A main-safe Data layer owns its own blocking work, so callers can invoke suspend functions from the main thread without needing Dispatchers.IO.
— Repository should expose suspend functions instead of launching internally; silent launch leaves task ownership, cancellation, and exception handling undefined.
— Java blocking primitives like BlockingQueue, ReentrantLock, and synchronized block threads in suspend chains; Channel and Mutex suspend instead.
— suspend does not imply non-blocking: Thread.sleep blocks the thread while delay suspends the coroutine.
— CancellationException is a control signal in structured concurrency and must be rethrown, not swallowed by a generic catch.
— A global event bus, even one built on SharedFlow, keeps implicit dependencies; use StateFlow, SharedFlow, Channel, Shared ViewModel, or Repository+Flow per scenario.
— Data layer should convert technical exceptions (IOException, HttpException) into stable errors; ViewModel maps only business results to UI states.
— Cold flows are the default for Data layer interfaces; stateIn is a lifecycle and resource decision best left to the ViewModel.
— runBlocking blocks the current thread; coroutineScope suspends the coroutine. The bridge is not a daily passage.
Conclusions

AI-generated coroutine code most often fails on semantic conflations, not syntax: launch is used as a thread-switch and suspend is treated as a guarantee of non-blocking, both of which pass compilation and unit tests.

The ownership principle is the real architectural contract: any design that hides who cancels a coroutine or who waits for its result is a flaw, even if it uses the same API names.

Local correctness is a trap: code that compiles and passes tests can still violate lifecycle and error-boundary rules that only surface under load or after a screen is destroyed.

Concepts & terms
Local Correctness
Code that compiles, runs, and passes unit tests but still violates architectural invariants like ownership, lifecycle, and error propagation.
Main-safe
A Data layer guarantee that suspend functions can be called from the main thread without blocking it; the layer owns its thread dispatch internally.
Structured Concurrency
A coroutine model where tasks have a defined parent-child hierarchy; children must complete before parents, and cancellation propagates down the tree.
Cold flow vs hot flow
Cold flows emit on demand for each collector; hot flows (StateFlow, SharedFlow) exist independently of collectors and share data across subscribers.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗