Flutter项目执行assembleDebug任务时出现Gradle空指针异常
Hey there, I know it’s super frustrating when you’ve tried all the usual Stack Overflow fixes and still hit this Gradle null pointer error. Let’s walk through some targeted steps to get your Flutter project running again.
First, let’s recap the error you’re seeing for clarity:
Launching lib\main.dart on ONEPLUS A3003 in debug mode...
Exception in thread "main" java.lang.NullPointerException
at org.gradle.wrapper.BootstrapMainStarter.findLauncherJar(BootstrapMainStarter.java:34)
at org.gradle.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:25)
at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:129)
at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:61)
This error almost always ties to a broken or missing Gradle wrapper file, or a misconfigured wrapper setup. Here’s what to try next:
Step 1: Nuke and rebuild the Gradle wrapper
- First, delete the existing wrapper jar file in your project: navigate to
[your-project]/gradle/wrapperand deletegradle-wrapper.jar. - Next, clear your local Gradle cache to remove any corrupted downloads:
- On Windows: Delete the folder
C:\Users\<YourUsername>\.gradle\caches - On Mac/Linux: Delete the folder
~/.gradle/caches
- On Windows: Delete the folder
- Now, regenerate the wrapper with your target version. Open a terminal in your project root and run:
Or if you prefer using Flutter commands:gradlew wrapper --gradle-version 5.6.2flutter pub get
Step 2: Double-check your Gradle wrapper config
Open [your-project]/gradle/wrapper/gradle-wrapper.properties and verify these details:
- Make sure the
distributionUrlis complete and correctly formatted (note the escapedhttps\:and the-all.zipsuffix – this includes all necessary components, not just the binaries):distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip - Ensure there are no typos, extra spaces, or missing lines in the file. A single typo here can break the entire wrapper setup.
Step 3: Verify your local Gradle environment (if you have a manual install)
- Check if you have the
GRADLE_HOMEenvironment variable set to the correct path for Gradle 5.6.2. - Confirm that
%GRADLE_HOME%\bin(Windows) or$GRADLE_HOME/bin(Mac/Linux) is added to your systemPATH. - Test the version by running this in your terminal:
It should output version 5.6.2. If not, reinstall Gradle 5.6.2 and double-check your environment variables.gradle -v
Step 4: Clean and rebuild your Flutter project
Sometimes stale build files can cause weird issues. Run these commands in order:
flutter clean flutter pub get flutter run
This will wipe old build artifacts, re-fetch dependencies, and attempt to launch the project fresh.
Step 5: Check directory permissions (especially on Windows)
If your project is stored in a restricted folder (like Program Files or a folder requiring admin access), Gradle might not be able to write or read necessary files. Try:
- Moving your project to a regular user folder (like
Documentsor your Desktop). - Running your terminal or IDE (Android Studio/VS Code) as an administrator, then re-running the build commands.
内容的提问来源于stack exchange,提问作者The Vine

