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

连接Firebase后Flutter项目Gradle构建失败报错排查

Gradle构建错误修复:无法找到apply()方法的正确用法

问题根源

你的错误出在两个地方:

  1. apply插件语法错误:第6行的apply plugins "com.google.gms.google-services"写法不符合Gradle规范,正确语法是apply plugin: "插件ID"(注意用单数plugin,且需要冒号分隔)
  2. 冗余依赖:dependencies块里的implementation 'com.google.gms:google-services:4.3.10'完全多余,google-services是Gradle插件,不是Android依赖库,不需要在这里声明。

修正后的完整代码

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
}
apply plugin: "com.google.gms.google-services"

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

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

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

android {
    namespace "com.example.final_project"
    compileSdkVersion flutter.compileSdkVersion
    ndkVersion flutter.ndkVersion

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    defaultConfig {
        applicationId "com.example.final_project"
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}
dependencies {
    // Import the Firebase BoM
    implementation(platform("com.google.firebase:firebase-bom:32.4.0"))

    // TODO: Add the dependencies for Firebase products you want to use
    // When using the BoM, don't specify versions in Firebase dependencies
    implementation("com.google.firebase:firebase-analytics")

    // Add the dependencies for any other desired Firebase products
}

额外优化提示

推荐用plugins块统一声明插件的写法,把google-services插件移到plugins块里,更符合现代Gradle规范:

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id "com.google.gms.google-services" // 直接在这里声明,不需要apply语句
}

这样就可以完全删掉apply plugin: "com.google.gms.google-services"这一行了。

内容的提问来源于stack exchange,提问作者Nguyễn Thanhvăn

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 19:50:27