You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter运行报错:Could not initialize class com.android.build.gradle.internal.TaskManager

Flutter真机部署构建失败:Could not initialize class com.android.build.gradle.internal.TaskManager

报错详情

FAILURE: Build failed with an exception.

  • Where:
    Build file 'C:\Users\hp\Documents\flutter-mobile-app\android\app\build.gradle' line: 24
  • What went wrong:
    A problem occurred evaluating project ':app'.
    Could not initialize class com.android.build.gradle.internal.TaskManager

相关配置信息

app/build.gradle 配置

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'com.google.gms.google-services'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

    lintOptions {
        disable 'InvalidPackage'
        disable "Instantiatable"
        checkReleaseBuilds false
        abortOnError false
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID
        applicationId "com.otobucks.app"
        minSdkVersion 21
        targetSdkVersion 31
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        multiDexEnabled true
    }
    dexOptions {
        preDexLibraries = false
        javaMaxHeapSize "4g"
    }
    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'com.android.support:multidex:1.0.3'
    implementation platform('com.google.firebase:firebase-bom:26.8.0')
    implementation 'com.google.firebase:firebase-messaging'
}

android/build.gradle 配置

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:7.1.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.3.0'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

Java环境

使用JDK 1.8.0_291,JDK路径:
Java Home路径

gradle.properties 配置

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip

解决方案

这个错误核心原因是Android Gradle Plugin(AGP)、Gradle与JDK版本不兼容,按以下步骤修复:

  1. 升级JDK版本
    AGP 7.x系列要求JDK 11及以上版本,你当前使用的JDK 8不满足要求:

    • 安装JDK 11或JDK 17(推荐LTS稳定版)
    • 修改系统环境变量JAVA_HOME指向新JDK路径
    • 在VS Code中切换Java环境:打开命令面板(Ctrl+Shift+P),输入Java: Configure Java Runtime选择安装的高版本JDK
  2. 匹配AGP与Gradle版本
    当前AGP 7.1.2对应Gradle最低版本为7.2,你使用的7.4符合要求,但可以升级到更稳定的配对组合:

    • 修改android/build.gradle中的AGP版本为7.4.2
    • 修改gradle.properties中的distributionUrl为https\://services.gradle.org/distributions/gradle-7.5-all.zip
  3. 升级Google Services插件
    将android/build.gradle中的Google Services插件版本升级到稳定版:

    classpath 'com.google.gms:google-services:4.4.0'
    
  4. 清理项目缓存

    • 执行命令:flutter clean
    • 删除项目中android/.gradle和build文件夹
    • 重新执行flutter run

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 14:20:36