添加 Firestore 依赖项会导致 Flutter 应用程序出错

Posted

技术标签:

【中文标题】添加 Firestore 依赖项会导致 Flutter 应用程序出错【英文标题】:Adding Firestore dependency causes error in Flutter app 【发布时间】:2020-03-05 18:33:17 【问题描述】:

在我的 Flutter 项目中添加 cloud_firestore: ^0.12.10 依赖项会出错。

这些是我的项目所具有的依赖项:

dependencies:
  url_launcher: ^5.0.0
  flutter_launcher_icons: ^0.7.4

  cloud_firestore: ^0.12.10

  flutter:
    sdk: flutter

  # The following adds the Cupertino Icons font to your application.
  # Use with the CupertinoIcons class for ios style icons.
  cupertino_icons: ^0.1.2
  firebase_messaging: ^5.1.6

dev_dependencies:
  flutter_test:
    sdk: flutter

flutter_icons:
  android: "launcher_icon"
  ios: true
  image_path: "assets/launcher_icon/LauncherIcon.png"

我尝试使用不同的 cloud_firestore 版本,但都不起作用。

这是flutter run 命令在终端中的输出:

Launching lib\main.dart on SM A505F in debug mode...
Initializing gradle...                                              1.1s
Resolving dependencies...                                          16.3s
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)
registerResGeneratingTask is deprecated, use registerGeneratedResFolders(FileCollection)

FAILURE: Build failed with an exception.

* What went wrong:
Failed to notify dependency resolution listener.
> The library com.google.android.gms:play-services-measurement-base is being requested by various other libraries at [[17.2.1,17.2.1]], but resolves to 16.5.0. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.
> The library com.google.firebase:firebase-iid is being requested by various other libraries at [[20.0.0,20.0.0]], but resolves to 17.1.2. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.

* 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 11s
Running Gradle task 'assembleDebug'...
Running Gradle task 'assembleDebug'... Done                        11.8s
Gradle task assembleDebug failed with exit code 1

这是flutter doctor -v的输出:

[√] Flutter (Channel stable, v1.9.1+hotfix.6, on Microsoft Windows [Version 10.0.18362.418], locale en-IN)
    • Flutter version 1.9.1+hotfix.6 at D:\flutter_windows_v1.9.1+hotfix.2-stable\flutter
    • Framework revision 68587a0916 (8 weeks ago), 2019-09-13 19:46:58 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0


[!] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\aditl\AppData\Local\Android\sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    ! Some Android licenses not accepted.  To resolve this, run: flutter doctor --android-licenses

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 41.0.2
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] VS Code (version 1.40.0)
    • VS Code at C:\Users\aditl\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.6.0

[√] Connected device (1 available)
    • SM A505F • RZ8M60LFVPP • android-arm64 • Android 9 (API 28)

! Doctor found issues in 1 category.

【问题讨论】:

【参考方案1】:

您记录错误

* What went wrong:
Failed to notify dependency resolution listener.
> The library com.google.android.gms:play-services-measurement-base is being requested by various other libraries at [[17.2.1,17.2.1]], but resolves to 16.5.0. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.
> The library com.google.firebase:firebase-iid is being requested by various other libraries at [[20.0.0,20.0.0]], but resolves to 17.1.2. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.

如果您已将 firebase 核心添加到 gradle 文件,则需要将其更新到 17.2.1,

虽然这是您的项目未启用 androidX 的问题 see how to migrate to androidX

更新

    Cannot fit requested classes in a single dex file (# methods: 81876 > 65536)
com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives:
The number of method references in a .dex file cannot exceed 64K.
Learn how to resolve this issue at https://developer.android.com/tools/building/multidex.html

您需要为您的应用程序启用multidex 如下

    android 
    defaultConfig 
        ...
        minSdkVersion 16
        targetSdkVersion 28
        multiDexEnabled true
    
    ...


dependencies 
  implementation "androidx.multidex:multidex:2.0.0"

【讨论】:

我按照页面上给出的步骤进行操作,但出现No usages found in the project 错误。所以,我跟着this answer手动迁移到AndroidX。我将所有依赖项更新为最新版本。没有 cloud_firestore 依赖,一切正常。当我添加 cloud_firestore: ^0.12.10 依赖项时,我得到了 this error。 @MasterAdit 我更新了我的答案以适应 multidex,如果您没有正确地将 firestore 添加到您的应用程序中,请考虑查看firebase.google.com/docs/flutter/setup【参考方案2】:

在底部的 android/build.gradle 文件中添加这行代码。我认为您错过了添加服务插件的步骤。 试试这个

并将cloud_firestore: ^0.12.10 dependency 放在firebase_messaging dependency 下方。

代码:

apply plugin: 'com.google.gms.google-services'

【讨论】:

我在firebase_messaging 下方添加了cloud_firestore: ^0.12.10,但仍然出现错误。我已经在android/app/build.gradle 文件中有apply plugin: 'com.google.gms.google-services'(不在android/build.gradle 中)。我也尝试将apply plugin: 'com.google.gms.google-services' 添加到android/build.gradle,但它给了我一个不同的错误。【参考方案3】:

在你的 Podfile 中的目标 'Runner' do block 中添加这一行,例如:

# ...
target 'Runner' do
  pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '8.9.0'
# ...
end

【讨论】:

虽然此代码可能会解决问题,including an explanation 关于如何以及为什么解决问题将真正有助于提高您的帖子质量,并可能导致更多的赞成票。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人。请edit您的回答添加解释并说明适用的限制和假设。

以上是关于添加 Firestore 依赖项会导致 Flutter 应用程序出错的主要内容,如果未能解决你的问题,请参考以下文章

为啥在 Maven 中添加依赖项会触发功能?

添加xuggler依赖项会产生错误

在我的春季批处理中添加 Maven 依赖项会出现特定错误

从 JavaScript 添加 asp.net 下拉列表项会导致页面回发错误

使用object-sizeof依赖项会对内存产生什么影响

更改 UIToolbar 项会导致工具栏变为空白