使用IntelliJ Idea开发Flutter时build.gradle报错,Properties()与GradleException()异常求助
Hey there, let's fix those Properties() and GradleException() errors in your Flutter project's android/app/build.gradle file—this is a super common issue after Gradle updates, so you're not alone!
Properties() and GradleException() Errors in Flutter's Android Build.gradle Why This Happens
Starting with Gradle 7.0+, the old Properties class (from java.util) and GradleException have been deprecated or shadowed by newer Gradle API equivalents. Most Flutter projects hit this when upgrading Flutter, Gradle, or the Android Gradle Plugin (AGP).
Step 1: Resolve the Properties() Error
You probably have code like this in your app-level build.gradle:
def localProperties = new Properties() def localPropertiesFile = rootProject.file('local.properties') if (localPropertiesFile.exists()) { localPropertiesFile.withReader('UTF-8') { reader -> localProperties.load(reader) } }
The fix is to explicitly reference the full path to the Java Properties class to avoid conflicts with Gradle's own Properties type:
def localProperties = new java.util.Properties()
This is the quickest, safest fix for Flutter projects—no need to overhaul your setup.
Step 2: Fix the GradleException() Error
Anywhere you're throwing a GradleException, you have two options:
Option 1: Use the full class path
Replace instances of GradleException with org.gradle.api.GradleException:
// Old code throw new GradleException("Failed to load local properties") // Fixed code throw new org.gradle.api.GradleException("Failed to load local properties")
Option 2: Add an import
Add this line at the very top of your build.gradle file, then use GradleException normally:
import org.gradle.api.GradleException // Now you can use this as before throw new GradleException("Failed to load local properties")
Step 3: Verify Gradle & AGP Version Compatibility (If Needed)
Sometimes these errors pop up if your AGP and Gradle versions are mismatched. Check your top-level android/build.gradle and gradle/wrapper/gradle-wrapper.properties to ensure they're compatible:
- AGP 7.0+ requires Gradle 7.0+
- AGP 8.0+ requires Gradle 8.0+
For example, update your top-level build.gradle dependencies:
buildscript { ext.kotlin_version = '1.9.0' // Use a version compatible with your AGP repositories { google() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:8.1.0' // Match your Gradle version classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } }
And your gradle-wrapper.properties:
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1-bin.zip
Step 4: Clean & Rebuild
After making these changes:
- Run
flutter cleanin your terminal - Run
flutter pub get - Rebuild your project in IntelliJ or Android Studio
That should wipe out those exceptions! If you still run into issues, double-check that you didn't miss any instances of Properties() or GradleException() in both the top-level and app-level build.gradle files.
内容的提问来源于stack exchange,提问作者Devansh Baldwa

