Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does android.arch.navigation cause Program type already present: android.support.v4.os.ResultReceiver$1?

I am using Android Studio version

Android Studio 3.2 Canary 14
Build #AI-181.4668.68.32.4763614, built on May 4, 2018
JRE: 1.8.0_152-release-1136-b02 x86_64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Mac OS X 10.11.6

While investigating the new Architectural navigation components android.arch.navigation I have encountered this build failure.

AGPBI: {"kind":"error","text":"Program type already present: android.support.v4.os.ResultReceiver$1","sources":[{}],"tool":"D8"}
:app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
> com.android.builder.dexing.DexArchiveMergerException: Error while merging dex archives: ...
...
  Program type already present: android.support.v4.os.ResultReceiver$1

My app gradle build resembles:-

apply plugin: 'com.android.application'
apply plugin: "androidx.navigation.safeargs"

android {
    compileSdkVersion 'android-P'
    defaultConfig {
        applicationId "com.research.frager"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    def nav_version = "1.0.0-alpha01"

    implementation "android.arch.navigation:navigation-fragment:$nav_version"
    implementation "android.arch.navigation:navigation-ui:$nav_version"

    // optional - Test helpers
    androidTestImplementation "android.arch.navigation:navigation-testing:$nav_version"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'

    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'
    testImplementation 'junit:junit:4.12'

    androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'
}

and project level gradle build:-

buildscript {

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0-alpha14'
        classpath "android.arch.navigation:navigation-safe-args-gradle-plugin:1.0.0-alpha01"
    }
}

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

task clean(type: Delete) {
    delete rootProject.buildDir
}

I have tried refactoring to AndroidX, however I get a message stating No usages found in project, so why is this "v4" class still being mentioned?

like image 610
Hector Avatar asked May 16 '18 08:05

Hector


1 Answers

I've been looking at this issue and I have sorted the main issue here by excluding the support package when adding the dependency to the library, doing this:

implementation("android.arch.navigation:navigation-fragment:$nav_version") {
  exclude group: 'com.android.support'
}

That would allow you to run the application. However, this artifact is using the support artifacts rather than the androidx artifacts. Looking at the documentation, we can see that NavHostFragment is extending support.v4.Fragment https://developer.android.com/reference/androidx/navigation/fragment/NavHostFragment

So, in short, you are presented with three options, as far as I can see. The first one is to drop the androidx artifacts and use the support ones which eventually depends on how big your app is.

The second option is to drop the navigation library and go back to the classic way of dealing with navigation which, I guess, is probably undesirable for you.

The third option is to implement a navigation host of your own which I don't know how much work it would be.

This response will remain accurate until Google releases the androidx version of the library which I am surprised they haven't already.

Hope that was helpful.

like image 173
Pablo Sánchez Avatar answered Nov 06 '22 00:11

Pablo Sánchez