簡體   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).

將行classpath 'com.google.gms:google-services:4.3.2'添加到項目級別 gradle 工作正常。 我可以同步 gradle 文件。

但是,將行apply plugin: 'com.google.gms.google-services'添加到應用程序級別 gradle 文件會破壞我的應用程序。 我收到以下錯誤:

錯誤:清單合並失敗:屬性 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)。 建議:將 'tools:replace="android:appComponentFactory"' 添加到 AndroidManifest.xml:5:5-21:19 的元素以覆蓋。

所以我 go 做這個建議。 我將tools:replace="android:appComponentFactory"添加到 AndroidManifest.xml 文件中的 >application< 元素。 然后,這給了我一個命名空間“工具”未綁定錯誤。 我按 Alt+Enter 修復將xmlns:tools="http://schemas.android.com/tools"添加到文件頂部的問題。

再次嘗試同步。 現在我收到類似的錯誤:錯誤:清單合並失敗並出現多個錯誤,請參閱日志 這就是我迷路的地方。 我不確定要在 Google 中搜索什么。 搜索此特定錯誤會帶來很多與我的問題無關的答案。 這個領域對我來說是全新的。

PS 最重要的是,添加前面提到的 3 行后,我的 build.grade(app level) 文件中的行implementation 'com.android.support:appcompat-v7:28.0.0'帶有紅色下划線。

我注意到還有其他與此相關的問題,但似乎都沒有可靠的答案。 所以我冒着這可能是轉發的風險。

編輯:: 這是我的 gradle 依賴項。

項目Gradle

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

應用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'
}

編輯 2::

好的,在遷移到 androidx 后,我更新了所有導入語句以使用 androidx。 它仍然一直說合並失敗並出現多個錯誤。 我不得不刪除它之前建議添加的行tools:replace="android:appComponentFactory" 最后我能夠同步並運行我的應用程序。 除了現在,應用程序只是立即啟動和停止。 我想這就是進步? 對此有什么想法嗎?

編輯 3:: 我終於讓它工作了。 最后一個問題是 RunTimeError “錯誤膨脹 class androidx.constraintlayout.ConstraintLayout”。 這是因為我所有的 xml 文件都有androidx.constraintlayout.ConstraintLayout行。 將其替換為androidx.constraintlayout.widget.ConstraintLayout行,現在一切都很順利。 Gradle 同步,應用程序加載,不會崩潰,一切正常

任何幫助將不勝感激。 提前謝謝,

弗拉德

在此處輸入圖像描述

問題是您的應用程序正在使用android.support庫,而您添加到項目中的 Firebase sdk 使用AndroidX

注意:AndroidX 和 android.support 不兼容。

因此,您必須將項目遷移到 AndroidX 或使用 Firebase 的支持版本。

  1. 遷移代碼以使用 AndroidX

    • Refactor選項中選擇Migrate to Androidx
    • 同步項目,你應該沒問題。
  2. 使用非 Androidx firebase。

    • Go 到Firebase 發行說明
    • 查找將庫遷移到 AndroidX 之前的最后一個版本
      (例如Firebase messaging 19 使用 androidx 而更低版本不使用)

TL;博士

遷移到AndroidX或使用 Firebase 的較低版本庫。

在您的應用程序標簽中添加 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"
>

然后 gradle.properties 添加

android.enableJetifier=true
android.useAndroidX=true

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM