Android gradle 包含 firebase 和 gms 服务时构建失败并出现异常

Android gradle build failed with an exception when include firebase and gms service

我刚刚创建了一个空的 android 项目,同时包含项目所需的依赖项 我只有在添加 com.google.firebasecom.google.android.gms 时才会出错,它会在下面生成错误。

/Users/macbookair/Desktop/ANDROID/APPTEST/app/src/main/AndroidManifest.xml:22:18-91 Error: 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). Suggestion: add 'tools:replace="android:appComponentFactory"' to element at AndroidManifest.xml:5:5-21:19 to override.

谁能帮帮我,我已经尝试了很长时间来修复这个错误。

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.fexample.android.appz"
        minSdkVersion 18
        targetSdkVersion 28
        versionCode 2
        versionName "2.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    implementation 'org.apache.httpcomponents:httpcore:4.4.10'
    implementation 'com.squareup.okhttp3:okhttp:3.13.1'
    implementation 'com.facebook.android:facebook-login:4.41.0'

     //If I uncomment the below line it will trigger error
    //implementation 'com.google.android.gms:play-services-safetynet:17.0.0'
    //implementation 'com.google.android.gms:play-services-auth:17.0.0'
    //implementation 'com.google.firebase:firebase-core:17.2.0'
    //implementation 'com.google.firebase:firebase-messaging:20.0.0'

}
android {
    useLibrary 'org.apache.http.legacy'
}
apply plugin: 'com.google.gms.google-services'

android { sourceSets { main { assets.srcDirs = ['src/main/assets', 'src/main/assets/public'] } } }

从下面的屏幕截图来看,这个 implementation 'com.android.support:appcompat-v7:28.0.0' 也有一个错误,但是当我构建 apk 时它创建成功,我认为这不是很好。

Dependencies using groupId com.android.support and androidx.* can not be combined but found IdeMavenCoordinates{myGroupId='com.android.support', myArtifactId='support-fragment', myVersion='28.0.0', myPacking='aar', myClassifier='null'} and IdeMavenCoordinates{myGroupId='androidx.swiperefreshlayout', myArtifactId='swiperefreshlayout', myVersion='1.0.0', myPacking='aar', myClassifier='null'}

我建议您尝试以下方法之一:

1) 迁移整个项目以使用 androidx 库,您可以在 Refactor > Migrate to AndroidX 下执行此操作从菜单栏。

2) 将 com.google.firebasecom.google.android.gms 的版本从 17.x.x 降级到较低版本