添加 firebase 后构建 Flutter apk 时出现 Gradle 错误
Posted
技术标签:
【中文标题】添加 firebase 后构建 Flutter apk 时出现 Gradle 错误【英文标题】:Gradle error when building Flutter apk after adding firebase 【发布时间】:2018-11-11 17:10:49 【问题描述】:我想在我的 Flutter 应用程序中实现 Firebase Cloud Messaging,我开始实现 android 部分。我像在 Firebase 文档中一样添加了依赖项,但是现在我在构建我的应用程序时遇到了 gradle 错误:
The library com.google.firebase:firebase-iid is being requested by various other libraries at [[16.0.0,16.0.0]], but resolves to 15.1.0. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.
我对 gradle 一点经验都没有,所以我希望能得到一些帮助^^
这里是 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.")
apply plugin: 'com.android.application'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
android
compileSdkVersion 27
lintOptions
disable 'InvalidPackage'
defaultConfig
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "com.leodr.pguapp"
minSdkVersion 16
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
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
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.google.firebase:firebase-messaging:17.0.0'
apply plugin: 'com.google.gms.google-services'
这里是项目 build.gradle(如果需要):
buildscript
repositories
google()
jcenter()
dependencies
classpath 'com.android.tools.build:gradle:3.0.1'
classpath 'com.google.gms:google-services:4.0.0'
allprojects
repositories
google()
jcenter()
rootProject.buildDir = '../build'
subprojects
project.buildDir = "$rootProject.buildDir/$project.name"
subprojects
project.evaluationDependsOn(':app')
task clean(type: Delete)
delete rootProject.buildDir
【问题讨论】:
我能看到的第一件事是你的 app/build.gradle 丢失了compile 'com.google.firebase:firebase-core:16.0.0'
当我添加它时抛出另一个错误^^:> The library com.google.android.gms:play-services-measurement-base is being requested by various other libraries at [[15.0.4,15.0.4]], but resolves to 15.0.2. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.
classpath 'com.google.gms:google-services:4.0.0'
到 classpath 'com.google.gms:google-services:4.0.1'
?我可能应该做一个新项目来复制这个问题。
不幸的是,这并没有解决任何错误:(
这听起来像是依赖冲突,但不确定是什么。稍后我会回到这个并尝试这个项目。
【参考方案1】:
将 build.gradle 上的 gms 版本降级为 类路径 'com.google.gms:google-services:3.2.1'
【讨论】:
这不是真正的解决方案。几周以来我一直在与这个问题作斗争,似乎所有人都在说降级谷歌服务......【参考方案2】:把这个写在app.gradle的底部com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true
【讨论】:
【参考方案3】:你好我有同样的问题,但我通过添加buil.gradle修复了它,我希望并解决它
dependencies
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.google.gms:google-services:4.0.1'
classpath 'com.android.tools.build:gradle:3.2.1'
【讨论】:
【参考方案4】:我认为,当您没有最新版本的 GRADLE/SDK/ANDROID STUDIO 并且您使用最新的 firebase 支持版本时,可能会生成此类错误。
因此,对于解决方案,您必须降低 gms 版本才能与您的 SDK 兼容版本一起运行。
【讨论】:
以上是关于添加 firebase 后构建 Flutter apk 时出现 Gradle 错误的主要内容,如果未能解决你的问题,请参考以下文章
如何为 Flutter Web 添加 Firebase 预构建的身份验证 UI?
安装 Firebase 后 Flutter 无法在 Android 上运行
添加flutter_firebase_ui后flutter应用程序崩溃