程序类型已存在:使用 androidx 库与 firebase 和 google 播放服务无法构建 android studio 3.1.1

Program type already present: use of androidx library with firebase and google play service fail to build android studio 3.1.1

我在 apk 构建中面临以下冲突

Program type already present: android.support.v4.media.MediaBrowserCompat$CallbackHandler
Message{kind=ERROR, text=Program type already present: android.support.v4.media.MediaBrowserCompat$CallbackHandler, sources=[Unknown source file], tool name=Optional.of(D8)}

下面是我的应用程序gradle

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'realm-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.himotech"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }

    dataBinding {
        enabled true
    }

    packagingOptions {
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/LICENSE-FIREBASE.txt'
        exclude 'META-INF/NOTICE'
    }
    lintOptions {
        abortOnError false
    }

    realm {
        syncEnabled = false;
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.appcompat:appcompat:1.0.0'
    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'
}
apply plugin: 'com.google.gms.google-services'

下面是项目gradle文件

buildscript {
    ext.kotlin_version = '1.2.30'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "io.realm:realm-gradle-plugin:5.4.1"
        classpath 'com.google.gms:google-services:4.1.0'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

ext{
    // The following are only a few examples of the types of properties you can define.
    compileSdkVersion = 27
    // You can also create properties to specify versions for dependencies.
    // Having consistent versions between modules can avoid conflicts with behavior.
    supportLibVersion = '27.1.1'
    firebaseCoreVersion='16.0.3'
    firebaseDatabaseVersion='16.0.2'
    firebaseLibVersion='15.1.0'
    firebaseMessaging='17.3.1'
}

IN gradle 属性 我启用了 androidx 使用:

android.useAndroidX = true
android.enableJetifier = false

我确实经历了这个 但仍然无法解决问题。

注意: 如果我从 gradle 中删除数据绑定,那么它工作正常。

我在你的 gradle 中看到一堆过时的物品。

您需要将 Android Studio 移动到 3.2。 (稳定

此外,您的 kotlin 版本应该是 1.2.70+(不要使用 1.2.71),它有错误。严重的是,它会导致 Android Studio 大量冻结。

您的 Gradle 构建工具也应该在 3.2.0+ 范围内。

确保您打包在 gradle-wrapper.properties 中的 distributionUrl 为 4.10-all 或更高版本

应删除您的 jre 依赖项以支持或 jdk 依赖项

你的jre版本是7,你应该更新到8。

 implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"

约束布局已推出 2.0.0-alpha2,如果您可以在该布局上稍微提前一点的话。不过不需要。

截至发布日期的 androidx 当前依赖项示例。

 //Android Google Libraries
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation "androidx.constraintlayout:constraintlayout:2.0.0-alpha2"

听起来你已经用过了,但请确认你有 gradle.properties 和

android.enableJetifier=true
android.useAndroidX=true

jetifier 将尝试使您的依赖项与 androidX 兼容。 最后,如果您有太多问题,请更新到 androidX,放弃您的更改,在一个好的位置从版本控制编译它。然后使用3.2中的迁移工具。它做得很好,只留下一些小错误需要清理。

同样值得注意的是,您可能必须转到每个 xml 文件并确认正确引用了新的命名空间,例如 RecyclerView 应该在您的 xml 中显示新的 androidx 命名空间。当您在代码中引用它们时也是如此。

我最近将我所有的项目更新到 androidX,第一个项目经过了一些尝试和错误,第二个项目我使用了迁移工具,它让我的生活变得更加顺利。

删除数据绑定对您有效的原因是它试图将 "incorrect namespace" 对象构建到您的数据绑定生成的文件中。您需要检查并修复每个 xml 文件,以便在 Android 小部件和框架元素上使用正确的命名空间。

我将提供抽屉的示例 activity。

<data>
    <variable name="activity" type="com.a35.activities.MainActivity"/>
    <variable name="iBindingRecyclerView" type="com.a35.interfaces.IBindingRecyclerView"/>
</data>

<androidx.drawerlayout.widget.DrawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="false"
    tools:openDrawer="start">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <!--TOP TOOLBAR-->
        <include
            android:id="@+id/toolbarMain"
            layout="@layout/toolbar_main"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <!--TOP BLACK LINE-->
        <View
            android:id="@+id/vRedLine"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_2"
            android:background="@color/black" />

        <!-- FrameLayout is used to insert fragments to display -->
        <FrameLayout
            android:id="@+id/fragPlaceholder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    </LinearLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        android:background="@color/colorPrimary">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <include layout="@layout/nav_drawer_header"
                     android:id="@+id/navHeader"/>

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/lstMenuItems"
                android:layout_below="@+id/navHeader"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:bindRcvInterface="@{iBindingRecyclerView}"
                app:bindRcvList="@{activity.getDrawerItemList}"/>

            <ImageView
                android:id="@+id/imgBottomLogo"
                android:layout_width="@dimen/dp_160"
                android:layout_height="@dimen/dp_35"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_marginBottom="@dimen/dp_35"
                android:src="@drawable/my_logo" />

        </RelativeLayout>

    </com.google.android.material.navigation.NavigationView>

</androidx.drawerlayout.widget.DrawerLayout>