Need to restart or flutter clean too often
Problem
--- --- --- The following note was added by @tvolkert on Oct 16, 2018 If you are affected by this bug, please fill out the following form! https://goo.gl/forms/eFPT6A3jAD8clU4o2 (Survey to gather information about this bug) --- --- --- Steps to Reproduce When I (or one of my teammates) run flutter `flutter run` (or with the run/debug button on IntelliJ and VSCode) on an AVD or on real devices, the installed version is always behind the newest one and I always need to restart the app or even sometimes `flutter clean`. This is a problem especially when the build on the AVD/real device crashes, and the only solution here is to `flutter clean`, even when I stop the app running and do `flutter run` again. The same happens when building and installing production APKs: When I run `flutter build` and `flutter install`, the installed APK isn't updated. If I run flutter clean, and next, again, `flutter build` and `flutter install`, the installed APK is up-to-date. Flutter Doctor: [code block]
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Implement Gradle Build Cache and Clean Configuration
The issue arises due to Gradle's default behavior of not fully cleaning the build artifacts, which can lead to outdated APKs being installed. This is especially problematic when there are changes in the code or dependencies that require a fresh build. The Gradle build cache may not be properly utilized, causing the need for frequent `flutter clean` commands.
Awaiting Verification
Be the first to verify this fix
- 1
Enable Gradle Build Cache
Modify your `gradle.properties` file to enable the Gradle build cache, which can help reduce build times and ensure that the latest changes are reflected in the APK.
propertiesorg.gradle.caching=true - 2
Clear Build Cache Periodically
Set up a scheduled task or a manual command to clear the Gradle build cache periodically to avoid stale builds. You can do this by running the command below in your project directory.
bash./gradlew cleanBuildCache - 3
Update Flutter and Dependencies
Ensure that you are using the latest version of Flutter and all dependencies. Run the following commands to update Flutter and check for dependency updates.
bashflutter upgrade && flutter pub upgrade - 4
Use `flutter pub get` Before Running
Always run `flutter pub get` before executing `flutter run` to ensure that all dependencies are up to date and properly linked.
bashflutter pub get - 5
Check for Hot Reload Issues
If you are using hot reload, ensure that your changes are compatible with it. Some changes require a full restart of the app. If you notice issues, consider using `flutter run --no-cache`.
bashflutter run --no-cache
Validation
To confirm the fix worked, run `flutter run` and check if the installed APK reflects the latest changes without requiring `flutter clean`. Additionally, monitor the build times and check for any errors during the build process.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep