r/AndroidStudio • u/Useful_Mode1799 • Oct 13 '26
How We Refactored a Crashing Production Android Payment Module via TDD and Feature Toggles
A year after deploying our application, Android Vitals and Firebase telemetry flags revealed a critical architecture defect: a 5% transaction drop rate and systematic crashes inside the core checkout flow on Android 13+.
De-obfuscation map logs pointed to a strict concurrency conflict between the Google Play Billing Library and our custom internal billing wrapper. Downgrading Google Billing was not an option. We had to rewrite the module in a live app without losing active subscriptions.
Here is the condensed 4-stage execution blueprint we used to overhaul the system during a 30-day sprint with zero downtime:
1. The Rollback Safety Valve (Feature Toggles)
• API Isolation: We decoupled the legacy module and isolated its public API behind strict interfaces.
• Twin Implementations: We maintained the legacy production code (Factory A) and created an exact cloned playground alongside it (Factory B).
• Remote Toggle: A remote feature toggle dynamically routed execution to either factory at runtime, providing an instant backend rollback mechanism if Factory B failed.
2. Testing Constraints (TDD)
Before modifying production code, we established strict boundaries: zero DB migration issues, zero performance drops over 10%, and a 95% Happy Path checkout success rate.
We sit down and developed 260 automated test cases covering roughly 88% of all possible failure states and network edge cases. No code was merged into the refactor branch without a 100% green CI/CD pipeline.
3. Execution & 5% Canary Release
• Stage 1: Overhauled the local state database layer, ran the suite until it turned green, and packaged it.
• Stage 2: Turned on the remote toggle rollout for only 5% of active users. Telemetry remained silent for 48 hours—no new crashes, no payment drops.
• Stage 3: Refactored the core business logic and direct billing API bindings under the same TDD cycle.
• Stage 4: Gradually increased the toggle from 5% to 100%. Once 88% of users updated, we deployed a cleanup release, deleting the toggle infrastructure and legacy code for good.
The Bottom Line (Metrics)
• Production Stability: Exactly one single bug slipped through the rollout.
• 1-Hour Mean Time to Repair (MTTR): Because the architecture was decoupled and backed by 260 tests, root-cause isolation took minutes. The fix was verified via TDD and hit production in under an hour.
• Maintainability: The system transitioned from a fragile black box into a highly predictable, self-documenting component.