1

I am integrating the dependencies for the mapbox-sdk into my gradle build files and run into the error that I have duplicates in my dependency tree. How can I exclude the necessary dependencies?

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-reflect:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.1.1'
    implementation 'com.android.support:recyclerview-v7:28.1.1'
    implementation 'androidx.constraintlayout:constraintlayout:2.0.0-alpha2'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'

    // android exinterface
    implementation 'com.android.support:exifinterface:28.0.0'
    // mapbox sdk
    implementation 'com.mapbox.mapboxsdk:mapbox-android-sdk:6.7.0'
    // navigation sdk will transitively bring maps-sdk
    implementation 'com.mapbox.mapboxsdk:mapbox-android-navigation-ui:0.24.0-beta.1'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.google.android:support-v4:r7'
    // multidex enabling
    implementation 'com.android.support:multidex:1.0.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'

}

I have multiple entries for androidx.fragment:fragment:1.0.0 (*) which seems to be the issue.

+--- androidx.legacy:legacy-support-v4:1.0.0
|    +--- androidx.core:core:1.0.0 (*)
|    +--- androidx.media:media:1.0.0
|    |    +--- androidx.annotation:annotation:1.0.0
|    |    +--- androidx.core:core:1.0.0 (*)
|    |    \--- androidx.versionedparcelable:versionedparcelable:1.0.0 (*)
|    +--- androidx.legacy:legacy-support-core-utils:1.0.0 (*)
|    +--- androidx.legacy:legacy-support-core-ui:1.0.0 (*)
|    \--- androidx.fragment:fragment:1.0.0 (*)
+--- com.mapbox.mapboxsdk:mapbox-android-sdk:6.7.0
|    +--- com.mapbox.mapboxsdk:mapbox-android-telemetry:3.5.2
|    |    +--- com.mapbox.mapboxsdk:mapbox-android-core:0.2.1
|    |    |    \--- androidx.appcompat:appcompat:1.0.0 (*)
|    |    +--- com.squareup.okhttp3:okhttp:3.10.0 -> 3.11.0
|    |    |    \--- com.squareup.okio:okio:1.14.0
|    |    +--- com.google.code.gson:gson:2.8.2 -> 2.8.5
|    |    +--- androidx.appcompat:appcompat:1.0.0 (*)
|    |    \--- androidx.lifecycle:lifecycle-extensions:2.0.0
|    |         +--- androidx.lifecycle:lifecycle-runtime:2.0.0 (*)
|    |         +--- androidx.arch.core:core-common:2.0.0 (*)
|    |         +--- androidx.arch.core:core-runtime:2.0.0 (*)
|    |         +--- androidx.fragment:fragment:1.0.0 (*)

The exception I get is a DexArchiveMergerException as followed:

BUILD FAILED in 17s
19 actionable tasks: 7 executed, 12 up-to-date
./gradlew build 

> Task :app:transformDexArchiveWithExternalLibsDexMergerForDebug FAILED
D8: Program type already present: androidx.fragment.app.Fragment$InstantiationException

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: androidx.fragment.app.Fragment$InstantiationException
  Learn how to resolve the issue at https://developer.android.com/studio/build/dependencies#duplicate_classes.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

Deprecated Gradle features were used in this build, making it incompatible with Gradle 5.0.
Use '--warning-mode all' to show the individual deprecation warnings.
See https://docs.gradle.org/5.0-milestone-1/userguide/command_line_interface.html#sec:command_line_warnings

BUILD FAILED in 8s
19 actionable tasks: 4 executed, 15 up-to-date
Alica
  • 107
  • 2
  • 9

1 Answers1

0

In gradle.properties add below lines

android.useAndroidX=true
android.enableJetifier=true

This error typically occurs due to one of the following circumstances:

  • A binary dependency includes a library that your app also includes as a direct dependency. For example, your app declares a direct dependency on Library A and Library B, but Library A already includes Library B in its binary. To resolve this issue, remove Library B as a direct dependency.
  • Your app has a local binary dependency and a remote binary dependency on the same library. To resolve this issue, remove one of the binary dependencies.

Please refer to here: https://developer.android.com/studio/build/dependencies#duplicate_classes

shizhen
  • 12,251
  • 9
  • 52
  • 88
  • Hi @shizhen, thank you for the help. I have had added these lines in my gradle.properties. I couldn't solve the issue so far. – Alica Nov 24 '18 at 18:15