You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Flutter升级后Gradle初始化失败问题求助:全局升级Gradle方案及问题根源咨询

Answers to Your Flutter/Gradle Questions

Is this issue caused by Gradle? Why did it happen after upgrading Flutter?

Absolutely, this issue boils down to version compatibility conflicts between Gradle, the Android Gradle Plugin (AGP), and the Kotlin Gradle Plugin. Here's a breakdown of what's going on:

  • The error Could not initialize class org.jetbrains.kotlin.gradle.plugin.sources.DefaultKotlinSourceSetKt is a classic red flag that your Kotlin plugin version doesn’t align with your Gradle version.
  • When you upgrade Flutter, the framework updates its default Android project templates to use newer versions of AGP, Kotlin, and Gradle. If your local development environment (like cached Gradle files, or the Kotlin plugin installed in Android Studio) is out of sync with these new template versions, conflicts pop up.
  • Even when you downgraded Flutter to 3.3.0, the problem stuck around likely because your project’s cached Gradle files (in android/.gradle) or local SDK components hadn’t been fully reset to match the older Flutter version’s requirements.
  • You’re far from alone—this is one of the most common issues reported by Flutter developers after version upgrades, especially when jumping across major Flutter releases.

How to globally upgrade Gradle for all new Flutter projects?

To skip manually updating Gradle every time you create a new Flutter project, you can modify Flutter’s default templates or set global Gradle preferences:

1. Edit Flutter’s Android template files

Flutter uses built-in templates to generate new projects. Tweaking these templates will make all future projects use your preferred Gradle/AGP/Kotlin versions automatically:

  • Find your Flutter SDK installation directory. Inside it, navigate to packages/flutter_tools/templates/app/android.
  • Edit the project-level build.gradle (in the root subfolder):
    Update the AGP and Kotlin plugin classpaths to the versions that worked for you, for example:
    dependencies {
        classpath "com.android.tools.build:gradle:7.4.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.20"
        classpath "dev.flutter:flutter-gradle-plugin:$flutterSdkVersion"
    }
    
  • Edit the app-level build.gradle (in the app subfolder):
    Set consistent compileSdkVersion, targetSdkVersion, and other settings that match your working configuration.
  • Save these changes—all new Flutter projects will now inherit your customized Gradle setup.

2. Set a global Gradle wrapper version

You can configure your system to use a specific Gradle version for all projects by editing your global gradle.properties file:

  • On Windows, this file lives at C:\Users\<YourUsername>\.gradle\gradle.properties
  • On macOS/Linux, it’s at ~/.gradle/gradle.properties
  • Add this line (replace the URL with the Gradle version you want to use globally):
    distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
    

Just double-check that this Gradle version is compatible with the AGP version you set in the Flutter templates.

Have other developers faced this same issue?

Yes, this is an extremely common pain point in the Flutter community. You’ll find hundreds of threads on developer forums (including Stack Overflow and the Flutter Discord server) where users report this exact DefaultKotlinSourceSetKt error after upgrading Flutter. Most solutions revolve around aligning the versions of Gradle, AGP, and the Kotlin plugin—exactly the fix that worked for you when upgrading Gradle manually.


内容的提问来源于stack exchange,提问作者nurwandi

火山引擎 最新活动