Keywords: ADB | Factory Reset | Android Recovery | Automation Script | Bulk Device Management
Abstract: This technical paper addresses the need for automated factory reset in Android device management by thoroughly analyzing the recovery command mechanism through ADB. Based on Android open-source code, it details the working principles of core commands like --wipe_data and --wipe_cache, with comprehensive code examples demonstrating complete automation implementations. The paper also compares different reset methods, providing reliable technical references for large-scale device administration.
Problem Background and Requirements Analysis
In the context of bulk Android device management, automated factory reset presents a common yet challenging requirement. Users face the task of wiping data from 200+ phones running stock Android, followed by installing new ROMs and additional applications. While other operations can be automated through batch files, the factory reset phase becomes a technical bottleneck.
Limitations of Traditional Approaches
During the exploration of solutions, users attempted various conventional methods, all with significant drawbacks:
- Manual File Deletion: Using
rm -r *commands to directly remove data, cache, and other directories. While this clears files, it causes system boot failures and bootloops due to improper handling of system state and configuration integrity. - Key Event Simulation: Attempting to use
adb input keyeventto simulate physical button presses. However, in recovery mode, the system operates in a special UNIX shell environment lacking standard input processing mechanisms, rendering this approach ineffective. - Script Discovery: Searching for built-in wipe scripts within the system, but locating specific execution files proves difficult in both standard recovery environments and custom recoveries like CWM.
Solution Based on Recovery Commands
Through in-depth analysis of Android recovery system source code, we discovered that recovery mode actually supports executing specific operations via command-line arguments. In the Android recovery system implementation, the recovery command accepts various parameters to control device state:
// Supported parameters for recovery command
* --send_intent=anystring // Write text to recovery.intent file
* --update_package=path // Verify and install OTA update package
* --wipe_data // Erase user data and cache, then reboot
* --wipe_cache // Wipe cache only, preserving user data
* --set_encrypted_filesystem=on|off // Enable or disable encrypted filesystem
Implementation Methodology
Based on these findings, factory reset can be achieved by directly executing recovery commands through ADB shell:
adb shell
recovery --wipe_data
This command triggers the same operational flow as when a user selects "Wipe Data/Factory Reset" in the recovery interface, ensuring proper system state reset.
Complete Automation Script Example
The following is a complete batch script example demonstrating how to integrate factory reset into an automated workflow:
@echo off
echo Starting automated device reset process...
:: Check device connection
adb devices | findstr "device" >nul
if errorlevel 1 (
echo No Android devices found. Please check connection.
exit /b 1
)
:: Perform factory reset
echo Performing factory reset...
adb shell recovery --wipe_data
:: Wait for device reboot
echo Waiting for device to reboot...
timeout /t 30
:: Check device status
adb wait-for-device
echo Device is ready for ROM installation.
:: Subsequent code for installing new ROM and applications
echo Proceeding with ROM and APK installation...
Technical Details and Considerations
Recovery System Variations: Different recovery systems (such as ClockworkMod, TWRP, etc.) may support varying command sets. It's advisable to verify compatibility with target devices before actual deployment.
Command File Location: In some recovery systems, available command options can be understood by examining the /cache/recovery/command file.
Alternative Method Comparison: The am broadcast approach mentioned in reference articles can also achieve reset but requires the system to be booted and have root privileges. In contrast, the recovery command runs directly in recovery mode, offering greater reliability.
Practical Application Recommendations
For large-scale device management scenarios, the following best practices are recommended:
- Conduct thorough testing on a small number of devices before deployment to verify command compatibility and effectiveness
- Implement error handling and retry mechanisms to address potential execution failures
- Maintain execution logs for troubleshooting and process optimization
- Account for device model and Android version differences by preparing multiple reset strategies
Conclusion
Executing recovery --wipe_data via ADB provides a reliable and automated factory reset solution. This method leverages Android's native recovery mechanism, ensuring complete data erasure and proper system state reset. For scenarios involving management of numerous Android devices, this approach significantly enhances efficiency while reducing the costs and error rates associated with manual operations.