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

Flutter项目Build.gradle报错:评估:app项目时出现异常

构建Flutter项目集成Square支付时出现Gradle错误

错误详情

  • 错误位置:C:\Users\kalen\Documents\Development\projects\Good Vibes\shop_app\good_vibes_shop_app\android\app\build.gradle 第28行
  • 错误提示:

FAILURE: Build failed with an exception.
评估项目':app'时出现问题,提示"No signature of method: build_e36rplgrfp57e9uvkfg0oc8u0.android() is applicable for argument types: (build_e36rplgrfp57e9uvkfg0oc8u0$_run_closure2) values: [build_e36rplgrfp57e9uvkfg0oc8u0$_run_closure2@46a9b49]"

背景信息

  • 首次尝试为Flutter项目集成Square内支付功能
  • 已执行./gradlew clean,未解决问题
  • 已升级至mavenCentral,错误突然出现

当前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 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 from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
    compileSdkVersion flutter.compileSdkVersion
    compileSdkVersion 33
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    kotlinOptions {
        jvmTarget = '1.8'
    }

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

    defaultConfig {
        // TODO: Specify your own unique Application ID
        applicationId "com.example.good_vibes_shop_app"
        minSdkVersion 24
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
            useProguard true

            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}
repositories {
   google()
   maven {
     url 'https://sdk.squareup.com/public/android'
   }
 }

dependencies {
  implementation "com.squareup.sdk.in-app-payments:card-entry:4.2.0"
}

解决方案

  1. 合并重复的dependencies块:Gradle不允许存在多个顶级dependencies块,这是导致配置解析失败的核心原因之一。把Square支付依赖合并到同一个dependencies块中。
  2. 调整repositories块位置:将Square的maven仓库配置移至android块之前,确保Gradle在解析依赖前能找到对应的仓库源。
  3. 移除重复的compileSdkVersion设置:同时定义compileSdkVersion flutter.compileSdkVersion和compileSdkVersion 33会导致版本冲突,保留flutter.compileSdkVersion让Flutter自动同步版本即可。

修改后的关键代码片段:

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

repositories {
   google()
   maven {
     url 'https://sdk.squareup.com/public/android'
   }
 }

android {
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    // 其余android配置保持不变
}

flutter {
    source '../..'
}

dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "com.squareup.sdk.in-app-payments:card-entry:4.2.0"
}
  1. 重新执行./gradlew clean后构建项目。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.10 17:52:46