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

Flutter集成Firebase认证时触发:app:checkDebugAarMetadata构建失败

Flutter Firebase认证构建错误::app:checkDebugAarMetadata 执行失败

我在Flutter项目中集成Firebase Authentication(注册/登录)功能时,遇到了:app:checkDebugAarMetadata任务执行失败的构建错误,尝试多种调试方法都没找到根源,代码也因为反复调试变得混乱。

项目配置文件

app/build.gradle

plugins {
    id "com.android.application"
    id "kotlin-android"
    id "dev.flutter.flutter-gradle-plugin"
    id '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.logbook"
    compileSdkVersion flutter.compileSdkVersion
    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 (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example.logbook"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion 30
        targetSdkVersion 31
        versionCode 1
        versionName "1.0"
        multiDexEnabled true
    }

    buildTypes {
        release {
            // TODO: Add your own signing config for the release build.
            // Signing with the debug keys for now, so `flutter run --release` works.
            signingConfig signingConfigs.debug
        }
    }
}

flutter {
    source '../..'
}

dependencies {
    implementation platform('com.google.firebase:firebase-bom:33.2.0')
}

project/build.gradle

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

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.2'
    }
}

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

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

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

错误详情

构建失败,出现异常。

  • 问题原因:任务:app:checkDebugAarMetadata执行失败。执行com.android.build.gradle.internal.tasks.CheckAarMetadataWorkAction时出现异常,检查AAR元数据时发现2个问题:
  1. 依赖androidx.credentials:credentials:1.2.0-rc01要求依赖它的库和应用需针对Android API 34或更高版本编译。当前:app编译目标为android-33。此外,Android Gradle Plugin 7.3.0推荐的最高编译SDK版本为33。建议操作:更新项目的Android Gradle Plugin至支持34的版本,然后将项目的compileSdkVersion更新至至少34。注意:更新库或应用的compileSdkVersion(允许使用更新的API)可与更新targetSdkVersion(启用新的运行时行为)和minSdkVersion(决定应用可安装的设备范围)分开进行。
  2. 依赖androidx.credentials:credentials-play-services-auth:1.2.0-rc01要求依赖它的库和应用需针对Android API 34或更高版本编译。当前:app编译目标为android-33。此外,Android Gradle Plugin 7.3.0推荐的最高编译SDK版本为33。建议操作:更新项目的Android Gradle Plugin至支持34的版本,然后将项目的compileSdkVersion更新至至少34。注意:更新库或应用的compileSdkVersion(允许使用更新的API)可与更新targetSdkVersion(启用新的运行时行为)和minSdkVersion(决定应用可安装的设备范围)分开进行。
  • 建议尝试:使用--stacktrace选项获取堆栈跟踪;使用--info或--debug选项获取更多日志输出;使用--scan选项获取完整分析信息。
  • 获取更多帮助:https://help.gradle.org

构建耗时24秒,异常:Gradle任务assembleDebug执行失败,退出码1

修复方案

方案1:升级编译环境适配API 34

  1. 更新Android Gradle Plugin(AGP)版本
    修改project/build.gradle的buildscript块,添加并指定支持API 34的AGP版本(如7.4.2):

    buildscript {
        ext.kotlin_version = '1.8.0'
        ext.agp_version = '7.4.2'
        repositories {
            google()
            mavenCentral()
        }
    
        dependencies {
            classpath "com.android.tools.build:gradle:$agp_version"
            classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
            classpath 'com.google.gms:google-services:4.4.2'
        }
    }
    

    同时确保gradle/wrapper/gradle-wrapper.properties中的Gradle版本与AGP匹配(7.4.2对应Gradle 7.5):

    distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-all.zip
    
  2. 提升compileSdkVersion和targetSdkVersion
    修改app/build.gradle的android配置块,将编译目标SDK升级到34:

    android {
        namespace "com.example.logbook"
        compileSdkVersion 34
        ndkVersion flutter.ndkVersion
    
        // ... 其他配置保持不变
    
        defaultConfig {
            // ... 其他配置
            targetSdkVersion 34 // 可选,建议同步更新以适配最新系统特性
        }
    }
    

    若希望通过Flutter SDK统一管理版本,可在local.properties中添加:

    flutter.compileSdkVersion=34
    
  3. 清理缓存并重新构建
    执行命令清理项目缓存后重新构建:

    flutter clean
    flutter pub get
    flutter run
    

方案2:降低Firebase BOM版本(临时兼容)

如果暂时不想升级编译环境,可将Firebase BOM版本降级到不依赖API 34的版本(如32.8.1),修改app/build.gradle的依赖:

dependencies {
    implementation platform('com.google.firebase:firebase-bom:32.8.1')
}

之后同样执行清理和重建命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 21:35:06