解决 com.android.support:support-compat 和 androidx.core:core 之间的依赖冲突

Solving dependency conflicts between com.android.support:support-compat and androidx.core:core

我还是 Android 的新手,我想创建一个具有导航抽屉并获取位置更新的应用程序。使用导航抽屉创建项目后,我有这样的依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:+'
    implementation 'com.android.support:support-v4:+'
    implementation 'com.android.support:design:+'
    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'}

然后我完成本教程:https://developer.android.com/training/location/receive-location-updates 它需要这些依赖项:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.1', {
    exclude group: 'com.android.support', module: 'support-annotations'
})
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'com.google.android.gms:play-services-location:16.0.0'
    implementation 'com.google.android.material:material:1.0.0'

    testImplementation 'junit:junit:4.12'
}

但是当我把它们放在一起时它们会发生冲突,我找不到解决它们的方法。

我尝试添加排除项、迁移到 androidx,但这一切都无济于事。我要么遇到这样的冲突:

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.1] AndroidManifest.xml:22:18-86 value=(androidx.core.app.CoreComponentFactory).

如果我尝试修复清单,或者一些冲突的包:

Duplicate class android.support.v4.app.INotificationSideChannel found in modules classes.jar (androidx.core:core:1.0.1) and classes.jar (com.android.support:support-compat:26.1.0)

有没有办法解决这个问题,让 UI 和 Location 都能正常工作?

你必须改变

implementation 'com.google.android.gms:play-services-location:16.0.0'

此版本使用了支持库,您不能同时使用 androidx 和支持库。

使用 play-services-locationupdated version 迁移到 androidx。

implementation 'com.google.android.gms:play-services-location:17.0.0'