Flutter构建Release版APK无法生成更新版本问题求助
Hey there! Let's troubleshoot why your Flutter 2.0 release build is still churning out the 1.0 APK, even though debug mode runs the new version perfectly. Here are the most common fixes to try, starting with the simplest:
Update your pubspec.yaml version
First things first—open yourpubspec.yamlfile and confirm theversionfield is set to your 2.0 release (e.g.,version: 2.0.0+2). The number after the+is the build number, so make sure that's incremented too. After editing, runflutter pub getto sync the changes.Wipe the build cache completely
Old build artifacts are often the culprit. Run these commands in your project root to start fresh:flutter clean flutter pub get flutter build apk --releaseThis deletes all cached build files and forces Flutter to rebuild everything from scratch.
Confirm you're grabbing the right APK
It sounds silly, but double-check the output path! The release APK lives atbuild/app/outputs/flutter-apk/app-release.apk. Make sure you're not accidentally picking up an old APK from a different folder. Check the file's timestamp to confirm it was just generated.Check for hardcoded versions in Android files
Sometimes the Android build config overrides the pubspec settings. Openandroid/app/build.gradleand look forversionCodeorversionName—if they're hardcoded (not usingflutterVersionCode/flutterVersionName), update them to match your 2.0 version, or remove them so they inherit from pubspec.Invalidate IDE caches
If you're using Android Studio or VS Code, cached IDE data might be stuck on the old version:- For Android Studio: Go to
File > Invalidate Caches...and select "Invalidate and Restart". - For VS Code: Open the command palette (
Ctrl+Shift+P/Cmd+Shift+P), run "Flutter: Clean", then "Flutter: Pub Get", before rebuilding.
- For Android Studio: Go to
Verify build flavors (if you use them)
If your project uses build flavors, you might be building an old flavor by default. Try explicitly specifying your target flavor when building:flutter build apk --release --flavor yourFlavorNameDouble-check your flavor setup in
android/app/build.gradleto ensure the 2.0 code is linked correctly.
If none of these work, double-check that your 2.0 code changes are actually in the branch you're building from (since debug works, this is less likely, but worth confirming!).
内容的提问来源于stack exchange,提问作者nitishk72




