Trying to use Firebase with AndroidX creates an error: Failed resolution of: Landroid/support/v4/util/ArrayMap;

Trying to use Firebase with AndroidX creates an error: Failed resolution of: Landroid/support/v4/util/ArrayMap;

我试图在我的应用程序中使用 AndroidX 库,但我遇到了问题。我了解与 Firebase 相关的问题。如您所见,我排除了支持 v4 库,我这样做是有原因的。如果我删除这些行,将会出现另一个错误:"Program type already present: android.support.v4.media.MediaBrowserCompat$ConnectionCallback$StubApi21"。那么如何同时使用 AndroidX 和 Firebase 呢?有什么解决办法吗?

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.ignes.androidxtest, PID: 30800
    java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v4/util/ArrayMap;
        at com.google.firebase.FirebaseApp.<clinit>(SourceFile:135)
        at com.google.firebase.FirebaseApp.initializeApp(SourceFile:273)
        at com.google.firebase.provider.FirebaseInitProvider.onCreate(SourceFile:37)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1696)
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1671)
        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(SourceFile:31)
        at android.app.ActivityThread.installProvider(ActivityThread.java:5084)
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4679)
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4619)
        at android.app.ActivityThread.access00(ActivityThread.java:155)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v4.util.ArrayMap" on path: DexPathList[[zip file "/data/app/com.ignes.androidxtest-2/base.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_dependencies_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_0_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_1_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_2_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_3_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_4_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_5_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_6_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_7_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_8_apk.apk", zip file "/data/app/com.ignes.androidxtest-2/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at com.google.firebase.FirebaseApp.<clinit>(SourceFile:135) 
        at com.google.firebase.FirebaseApp.initializeApp(SourceFile:273) 
        at com.google.firebase.provider.FirebaseInitProvider.onCreate(SourceFile:37) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1696) 
        at android.content.ContentProvider.attachInfo(ContentProvider.java:1671) 
        at com.google.firebase.provider.FirebaseInitProvider.attachInfo(SourceFile:31) 
        at android.app.ActivityThread.installProvider(ActivityThread.java:5084) 
        at android.app.ActivityThread.installContentProviders(ActivityThread.java:4679) 
        at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4619) 
        at android.app.ActivityThread.access00(ActivityThread.java:155) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1378) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5343) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700) 
        Suppressed: java.lang.ClassNotFoundException: android.support.v4.util.ArrayMap
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                ... 19 more
     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

我的build.gradle是

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.testng.androidxtest"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

configurations.all {
    resolutionStrategy {
                force 'androidx.legacy:legacy-support-v4:1.0.0-rc01'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-rc01'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-rc01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
    implementation ('com.google.android.gms:play-services-ads:15.0.1') {
        exclude group: 'com.android.support'
        exclude module: 'support-v4'
    }
    implementation ('com.google.firebase:firebase-core:16.0.1') {
        exclude group: 'com.android.support'
        exclude module: 'support-v4'
    }
}

将您的 gradle 版本更改为:classpath 'com.android.tools.build:gradle:3.3.0-alpha07' 并查看是否有效。

我需要将 Android Studio 更新到 3.2.1

版本