There is rarely anything more frustrating in Android development than a reliable build failing to deploy due to OEM-specific restrictions. You hit "Run" in Android Studio, wait for Gradle, and are met with INSTALL_FAILED_USER_RESTRICTED in the Logcat or a cryptic toast message on your Xiaomi device: "The device is temporarily restricted."
This guide details exactly why MIUI (and the newer HyperOS) blocks ADB installations by default and provides the step-by-step technical solution to bypass these security layers.
The Root Cause: MIUI Security vs. ADB
To fix the issue, you must understand what is blocking you. In stock Android (AOSP), enabling "USB Debugging" generally grants the ADB daemon (adbd) permission to execute pm install commands to push packages to the /data/app/ directory.
Xiaomi devices running MIUI or HyperOS add a proprietary security middleware layer between the ADB daemon and the Package Manager. This layer is designed to prevent malware from hijacking a device via USB connection when a user unknowingly enables debugging.
When Android Studio attempts to push the APK, the transaction follows this path:
- ADB Server (Host) sends the
installcommand. - ADB Daemon (Device) receives the command.
- MIUI Security Guard intercepts the intent.
- Rejection: If specific proprietary flags ("Install via USB") are not enabled, the Security Guard kills the process, returning
INSTALL_FAILED_USER_RESTRICTED.
The error "Device is temporarily restricted" is a server-side check from Xiaomi. To toggle the necessary permissions, the device attempts to validate the request against Xiaomi's servers, requiring a specific combination of network environment and account binding.
Prerequisites for the Fix
Xiaomi’s permissions framework is strict. Before attempting the settings changes, ensure the following environment state. If you skip these, the toggles described below will likely be grayed out or throw network errors.
- SIM Card Required: You must have a SIM card inserted into the device. The OS uses the cellular identifier to validate the device against the Xiaomi account servers.
- WiFi Disabled (Temporarily): Disabling WiFi and forcing the request through 4G/5G data often bypasses local network restrictions that cause the "temporarily restricted" error.
- Xiaomi Account: You must be signed into an active Mi Account on the device.
Step-by-Step Technical Solution
Follow this sequence precisely. The order matters because the underlying permissions have dependencies.
1. Enable Developer Options
If you haven't already:
- Go to Settings > About Phone.
- Tap MIUI Version (or OS Version) 7 times rapidly until the toast "You are now a developer" appears.
2. Configure Basic ADB
Navigate to Settings > Additional Settings > Developer Options.
- Toggle On
USB Debugging. - Accept the "Danger" warning (usually a 10-second countdown).
3. The Critical Fix: Install via USB
This is the specific toggle responsible for the INSTALL_FAILED_USER_RESTRICTED error.
- Stay in Developer Options.
- Find Install via USB.
- Action: Toggle this On.
Note: When you tap this, the OS will ping Xiaomi servers. If you see "The device is temporarily restricted," verify you are on Mobile Data (not WiFi) and have a valid SIM inserted.
4. Enable Security Settings
This step is required if you want to inspect UI hierarchies or simulate user input (e.g., using Espresso or Appium tests).
- Find USB Debugging (Security Settings) just below the previous option.
- Toggle this On.
- The Three Warnings: Xiaomi will present three consecutive 5-second countdown warnings. You must acknowledge all of them.
5. Disable MIUI Optimization (The "Last Resort" Fix)
If you are still experiencing issues—specifically if Gradle syncs but the app crashes immediately due to resource linking errors—you must disable MIUI Optimization. This forces the OS to use standard Android APIs for resource handling rather than Xiaomi's optimized proprietary implementations.
- Scroll to the very bottom of Developer Options.
- Look for Turn on MIUI optimization.
- Hidden Menu: If this option is missing, find the Reset to default values option (usually near the bottom) and tap it repeatedly (5-10 times). The "Turn on MIUI optimization" toggle will appear.
- Set it to Off.
- Warning: This will reset some system permissions (like floating windows) and may reboot the device.
Verifying the Connection via ADB
Do not rely solely on Android Studio's UI to confirm the fix. Verify the capability using your terminal to ensure the ADB bridge has the correct authorization levels.
Open your terminal and run the following commands:
# 1. Kill any stale server instances
adb kill-server
# 2. Start server
adb start-server
# 3. Check device authorization status
adb devices
Expected Output:
List of devices attached
a1b2c3d4 device
If the status is unauthorized, check your phone screen for an RSA key fingerprint prompt and accept it.
Next, attempt a manual install of a dummy APK or an existing project build output to test the pm install pipeline directly, bypassing Android Studio's Gradle overhead:
# Navigate to your build output
cd app/build/outputs/apk/debug/
# Attempt manual install
adb install -r app-debug.apk
Success Output:
Performing Streamed Install
Success
Failure Output (If fix failed):
Performing Streamed Install
adb: failed to install app-debug.apk: Failure [INSTALL_FAILED_USER_RESTRICTED: Install canceled by user]
Common Pitfalls and Edge Cases
The "Network Error" Loop
If enabling "Install via USB" constantly fails with a network error despite having a SIM card:
- Disconnect USB: Unplug the phone from the computer.
- Sign Out/In: Sign out of the Mi Account and sign back in.
- VPN: Ensure no VPN is active on the device. Xiaomi's servers often block requests from known VPN IP ranges.
Logcat Buffer Size
Xiaomi devices default to a very small logger buffer (64k or 256k), which causes Logcat to truncate crash traces in Android Studio.
- Fix: In Developer Options, find Logger buffer sizes and set it to 4M or 16M.
Application Termination on Background
MIUI's battery saver kills ADB-installed apps aggressively when the screen turns off.
- Fix: Go to Settings > Apps > Manage Apps > [Your App] > Battery Saver and select No restrictions.
Summary
The INSTALL_FAILED_USER_RESTRICTED error is not a bug in your code or Android Studio; it is a deliberate security feature of the Xiaomi ecosystem. By inserting a SIM card, utilizing mobile data for validation, and explicitly authorizing the "Install via USB" flag, you align the device's security posture with the requirements of a standard Android development environment.