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

Flutter打包APK报错:Could not get unknown property 'android' for :firebase_core

Flutter构建APK失败:Could not get unknown property 'android' for project ':firebase_core'

尝试执行flutter build apk构建Android应用时失败,报错信息如下:

FAILURE: Build failed with an exception.

* What went wrong:
A problem occurred configuring project ':firebase_core'.
> Failed to notify project evaluation listener.
   > Could not get unknown property 'android' for project ':firebase_core' of type org.gradle.api.Project.
   > Could not get unknown property 'android' for project ':firebase_core' of type org.gradle.api.Project.

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

BUILD FAILED in 953ms
Running Gradle task 'assembleRelease'...                         1.604ms
Gradle task assembleRelease failed with exit code 1

当前配置文件

settings.gradle

include ':app'
include ':firebase_core'
project(':firebase_core').projectDir = new File(rootProject.projectDir, '../../AppData/Local/Pub/Cache/hosted/pub.dev/firebase_core-3.9.0/android')

项目级build.gradle

buildscript {
    ext {
        compileSdkVersion = 33
        minSdkVersion = 21
        targetSdkVersion = 33
    }

    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.2'
        classpath 'com.google.gms:google-services:4.3.15'
    }
}

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

应用级build.gradle

android {
    compileSdkVersion 33
    defaultConfig {
        applicationId "com.gustaga.app"
        minSdkVersion 21
        targetSdkVersion 33
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            signingConfig signingConfigs.debug
            minifyEnabled true
            shrinkResources true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

flutter {
    source = "../.."
}

dependencies {
    implementation project(':firebase_core')
    implementation 'com.google.firebase:firebase-core:21.1.1'
    implementation 'com.google.firebase:firebase-database:20.0.5'
    implementation 'androidx.core:core-ktx:1.7.0'
    implementation 'androidx.appcompat:appcompat:1.4.1'
    implementation 'com.google.android.material:material:1.5.0'
    implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
    testImplementation 'junit:junit:4.13.2'
    androidTestImplementation 'androidx.test.ext:junit:1.1.3'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
}

firebase_core的build.gradle

buildscript {
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:8.0.2'
    }
}

apply plugin: 'com.android.library'

android {
    compileSdkVersion 33

    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 33
    }

    lintOptions {
        disable 'InvalidPackage'
    }
}

dependencies {
    implementation 'com.google.firebase:firebase-core:21.1.1'
}

解决步骤

  1. 修正settings.gradle:移除手动添加的:firebase_core相关行,恢复为Flutter默认配置,示例如下:

    include ':app'
    apply from: "$flutterSdkPath/packages/flutter_tools/gradle/app_plugin_loader.gradle"
    

    (注:若使用Flutter新版本,settings.gradle可能采用plugins块结构,保持默认即可)

  2. 通过pub管理依赖:确保pubspec.yaml中正确声明firebase_core依赖:

    dependencies:
      flutter:
        sdk: flutter
      firebase_core: ^3.9.0
    

    执行flutter pub get拉取依赖。

  3. 清理应用级build.gradle:移除dependencies中的implementation project(':firebase_core'),Flutter插件会自动处理依赖关联。

  4. 移除自定义firebase_core构建配置:删除手动修改的firebase_core本地build.gradle,使用pub下载的插件自带配置即可。

  5. 重新构建:执行flutter clean后,再运行flutter build apk。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 10:29:53