简体   繁体   English

将 firebase 添加到 android 工作室时 gradle 不同步

[英]gradle not syncing when adding firebase to android studio

So I'm trying to add the Firebase SDK to my Android Studio app as per the instructions from the Firebase website (see picture). So I'm trying to add the Firebase SDK to my Android Studio app as per the instructions from the Firebase website (see picture).

Adding the line classpath 'com.google.gms:google-services:4.3.2' to the project level gradle works fine.将行classpath 'com.google.gms:google-services:4.3.2'添加到项目级别 gradle 工作正常。 I can sync the gradle files.我可以同步 gradle 文件。

However, adding the line apply plugin: 'com.google.gms.google-services' to the app level gradle file breaks my app.但是,将行apply plugin: 'com.google.gms.google-services'添加到应用程序级别 gradle 文件会破坏我的应用程序。 I get the following error:我收到以下错误:

ERROR: Manifest merger failed: Attribute application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 is also present at [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).错误:清单合并失败:属性 application@appComponentFactory value=(android.support.v4.app.CoreComponentFactory) from [com.android.support:support-compat:28.0.0] AndroidManifest.xml:22:18-91 也是出现在 [androidx.core:core:1.0.0] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory)。 Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:5:5-21:19 to override.建议:将 'tools:replace="android:appComponentFactory"' 添加到 AndroidManifest.xml:5:5-21:19 的元素以覆盖。

So i go to do the suggestion.所以我 go 做这个建议。 I add tools:replace="android:appComponentFactory" to >application< element in AndroidManifest.xml file.我将tools:replace="android:appComponentFactory"添加到 AndroidManifest.xml 文件中的 >application< 元素。 This then gives me a Namespace "tools" not bound error.然后,这给了我一个命名空间“工具”未绑定错误。 I press Alt+Enter to fix which adds xmlns:tools="http://schemas.android.com/tools" to the top of my file.我按 Alt+Enter 修复将xmlns:tools="http://schemas.android.com/tools"添加到文件顶部的问题。

Try sync again.再次尝试同步。 Now i get a similar error: ERROR: Manifest merger failed with multiple errors, see logs .现在我收到类似的错误:错误:清单合并失败并出现多个错误,请参阅日志 And this is where I'm lost.这就是我迷路的地方。 I'm not sure what to search in Google.我不确定要在 Google 中搜索什么。 a search for this specific error brings up a lot of answers that aren't related to my problem.搜索此特定错误会带来很多与我的问题无关的答案。 This territory is completely new to me.这个领域对我来说是全新的。

PS On top of all that, the line implementation 'com.android.support:appcompat-v7:28.0.0' inside my build.grade(app level) file is underlined red after adding the previously mentioned 3 lines. PS 最重要的是,添加前面提到的 3 行后,我的 build.grade(app level) 文件中的行implementation 'com.android.support:appcompat-v7:28.0.0'带有红色下划线。

I noticed there's other questions related to this but none seem to have solid answers.我注意到还有其他与此相关的问题,但似乎都没有可靠的答案。 So I'm taking the risk that this might be a repost.所以我冒着这可能是转发的风险。

EDIT:: Here are my gradle depencies.编辑:: 这是我的 gradle 依赖项。

Project Gradle项目Gradle

dependencies {
        classpath 'com.android.tools.build:gradle:3.5.2'
        classpath 'com.google.gms:google-services:4.3.2'
    }

App Gradle应用Gradle

dependencies {
    implementation 'com.google.firebase:firebase-analytics:17.2.0'
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.1'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
}

EDIT 2::编辑 2::

Ok after migrating to androidx, I updated all my import statements to use androidx.好的,在迁移到 androidx 后,我更新了所有导入语句以使用 androidx。 It still kept saying merger failed with multiple errors.它仍然一直说合并失败并出现多个错误。 I had to delete the line tools:replace="android:appComponentFactory" that it had previously suggested to add.我不得不删除它之前建议添加的行tools:replace="android:appComponentFactory" Finally I was able to sync and run my app.最后我能够同步并运行我的应用程序。 Except now the app just starts and stops immediately.除了现在,应用程序只是立即启动和停止。 I suppose that's progress?我想这就是进步? Any ideas on this?对此有什么想法吗?

EDIT 3:: I finally got it to work.编辑 3:: 我终于让它工作了。 The last issue was a RunTimeError "Error inflating class androidx.constraintlayout.ConstraintLayout".最后一个问题是 RunTimeError “错误膨胀 class androidx.constraintlayout.ConstraintLayout”。 This is caused because all my xml files had the line androidx.constraintlayout.ConstraintLayout .这是因为我所有的 xml 文件都有androidx.constraintlayout.ConstraintLayout行。 replaced this with the line androidx.constraintlayout.widget.ConstraintLayout and everything is gravy now.将其替换为androidx.constraintlayout.widget.ConstraintLayout行,现在一切都很顺利。 Gradle syncs, app loads, doesn't crash and everything works as it's supposed to Gradle 同步,应用程序加载,不会崩溃,一切正常

Any help would be greatly appreciated.任何帮助将不胜感激。 Thx in advance,提前谢谢,

Vlad弗拉德

在此处输入图像描述

The problem is your app is using android.support libraries while the Firebase sdk you added to your project uses AndroidX .问题是您的应用程序正在使用android.support库,而您添加到项目中的 Firebase sdk 使用AndroidX

Note : AndroidX and android.support are not compatible.注意:AndroidX 和 android.support 不兼容。

So you have to either migrate your project to AndroidX or use a support version of Firebase.因此,您必须将项目迁移到 AndroidX 或使用 Firebase 的支持版本。

  1. Migrate code to use AndroidX迁移代码以使用 AndroidX

    • From Refactor option choose Migrate to Androidx .Refactor选项中选择Migrate to Androidx
    • Sync the project and you should be fine.同步项目,你应该没问题。
  2. Use a Non-Androidx firebase.使用非 Androidx firebase。

    • Go to Firebase release notes Go 到Firebase 发行说明
    • Find the last version that is before migrating library to AndroidX查找将库迁移到 AndroidX 之前的最后一个版本
      (For instance Firebase messaging 19 uses androidx and lower versions don't) (例如Firebase messaging 19 使用 androidx 而更低版本不使用)

TL;DR TL;博士

Migrate to AndroidX or use a lower version library of Firebase.迁移到AndroidX或使用 Firebase 的较低版本库。

In your application tag add tools:replace="android:appComponentFactory"在您的应用程序标签中添加 tools:replace="android:appComponentFactory"

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:replace="android:appComponentFactory"
>

then gradle.properties add然后 gradle.properties 添加

android.enableJetifier=true
android.useAndroidX=true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM