Herry
Herry

Reputation: 7087

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

I am facing below conflict in apk build

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)}

Below is my app 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'

below is project gradle file

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 property I have enabled androidx use:

android.useAndroidX = true
android.enableJetifier = false

I did go through this SO but still not able to resolved issue.

NOTE: If I remove data binding from gradle then it's working fine.

Upvotes: 0

Views: 711

Answers (1)

Sam
Sam

Reputation: 5392

I see a bunch of out dated items in your gradle.

You need to move your Android Studio to 3.2. (it is stable

Also your kotlin version should be 1.2.70+ (don't use 1.2.71) it has bugs. Seriously, it will cause Android Studio to freeze a lot.

your Gradle build tools should also be in the 3.2.0+ range.

Ensure your packaged in gradle-wrapper.properties has distributionUrl of 4.10-all or later

Your jre dependency should be removed in favor or jdk dependency

Your jre version is 7, you should update to 8.

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

Constraint layout has 2.0.0-alpha2 out if you are ok going a little ahead on that one. Not required though.

example of androidx current dependencies as of date of posting.

 //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"

Also sounds like you used already did, but confirm you have the gradle.properties with the

android.enableJetifier=true
android.useAndroidX=true

The jetifier will attempt to make your dependencies compatible with androidX. Lastly, if you are having too many issues, with the update to androidX, scrap your changes, get it compiling from version control at a good spot. Then use the migration tool in 3.2. It does a pretty good job, and only leaves a few minor errors to cleanup.

Also worth noting, that you may have to go to each xml file and confirm the new namespaces are referenced properly like RecyclerView should show the new androidx namespace in your xml. Same when you are referencing them in your code.

I recently updated all my projects to androidX and the first project took some trial and error, the second one I used the migration tool and it made my life a lot smoother.

The reason removing databinding works for you is because it is attempting to build "incorrect namespace" objects into your databinding generated files. You need to review and fix every xml file to use correct namespaces on your Android widgets and Framework elements.

I'll provide an example of a drawer 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>

Upvotes: 1

Related Questions