为Flutter嵌入Android项目添加Android运行/调试配置及问题咨询
Hey there! I’ve dealt with this exact frustration in Flutter hybrid projects where native Android changes don’t sync when using Flutter’s hot reload/hot restart. Let me walk you through setting up the right Android run/debug configuration in Android Studio to resolve this:
Why This Happens
When you run main.dart directly, Android Studio only focuses on rebuilding the Flutter portion of your app. Native Android resources (like theme colors in styles.xml or colors.xml) don’t get included in that workflow—so you have to restart the entire app to see those changes. By targeting the Android app module directly in your run config, you’ll get full sync for both native and Flutter code.
Step-by-Step Configuration Setup
Open Run Configurations
Go to Run > Edit Configurations... in the Android Studio menu bar.Create a New Android App Config
Click the + icon in the top-left corner, then select Android App from the dropdown. Name it something descriptive like "Flutter Hybrid Android App" so you can easily spot it later.Set the Target Module
In the Module dropdown, pick your main Android app module (it’s usually namedapp). Under Launch Options, leave Default Activity selected—this ensures your app starts with the Android activity that hosts theFlutterView.Enable Flutter Debugging Support
Switch to the Flutter tab in the config settings. Check the box labeled Enable Flutter support, and make sure the Flutter module path points to the root of your Flutter project (Android Studio often detects this automatically, but if not, browse to select it manually).Run the New Configuration
Select your new config from the run dropdown in the toolbar, then click the debug button (green bug icon) to launch the app.
How This Fixes the Issue
Now, when you make changes:
- To native Android resources: Android Studio will rebuild the Android module automatically when you run/debug, so theme color updates will show up without a full app restart.
- To Flutter code in
main.dart: You can still use hot reload/hot restart as usual—since Flutter support is enabled, the debugger connects to the Flutter engine running inside your Android app seamlessly.
Bonus Troubleshooting Tips
- If you still don’t see theme changes, try doing a Clean Project (Build > Clean Project) followed by Rebuild Project (Build > Rebuild Project) before running the config.
- Double-check that your Android activity correctly initializes the
FlutterEngineand attaches theFlutterView—any missteps here can break sync between native and Flutter layers.
内容的提问来源于stack exchange,提问作者Ultimo_m

