能否通过快捷方式切换指定构建变体的Instant Run状态?
Absolutely, you can set up shortcuts to toggle Instant Run's state—either globally or tailored to your specific build variant! Here's how to make it work in Android Studio:
1. Global Instant Run Toggle Shortcut
Android Studio has a built-in action for this, it just doesn’t come with a default shortcut. Here’s how to assign one:
- Open Settings (Windows/Linux:
File > Settings; Mac:Android Studio > Settings) - Navigate to the Keymap section
- Search for
Toggle Instant Runin the search bar - Right-click the action, select
Add Keyboard Shortcut, and set a combination that works for you (e.g.,Ctrl+Shift+Ion Windows,Cmd+Shift+Ion Mac) - Hit
ApplyandOKto save the setting
Now you can use that shortcut to instantly flip Instant Run on or off across all variants.
2. Variant-Specific Toggle (Combine with Build Variant Switch)
If you want Instant Run to behave differently for your specific variant, pair a build variant shortcut with Gradle configuration:
Step 1: Configure Instant Run per variant in Gradle
Open your app module's build.gradle file and set the instantRun.enabled flag for your target variant:
android { buildTypes { yourCustomVariant { // Replace with your specific build variant name instantRun.enabled = true // Set to false to disable for this variant } // You can set other variants as needed debug { instantRun.enabled = false } } }
Sync your Gradle project after making changes.
Step 2: Assign a shortcut to switch build variants
- Go back to the Keymap settings
- Search for
Switch Build Variant - Assign a shortcut to this action (e.g.,
Ctrl+Shift+V)
Now you can quickly switch to your custom variant (where Instant Run is enabled) or another variant (where it’s disabled) with the shortcut.
3. Advanced: Custom Macro for One-Click Toggle
For a truly seamless workflow, create a macro that combines switching to your specific variant and toggling Instant Run:
- Go to
Edit > Macros > Start Macro Recording - Perform the actions: switch to your custom build variant, then trigger the
Toggle Instant Runaction - Stop recording, name your macro (e.g., "Toggle Instant Run for Custom Variant")
- Head to Keymap, find your new macro, and assign a shortcut to it
This lets you handle both the variant switch and Instant Run toggle in a single keystroke.
Hope this helps you speed up your development process! 🚀
内容的提问来源于stack exchange,提问作者Jim Clermonts

