41

I completely new to Android Development and can't seem to resolve this error: "Error: Program type already present: android.support.v4.media.MediaBrowserCompat$CustomActionCallback"

This is my dependencies:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'
implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1'
implementation "android.arch.navigation:navigation-fragment:1.0.0-alpha01"
implementation "android.arch.navigation:navigation-ui:1.0.0-alpha01"


androidTestImplementation 'androidx.test:runner:1.1.0-alpha3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha3'
testImplementation 'junit:junit:4.12'
}

I've googled some and ended up on the developer page about "Resolve duplicate class errors", but I'm still not able to fix this. Help would be very much appriciated!

Michael_Oslo
  • 429
  • 1
  • 5
  • 5
  • @toobsco42, `implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'` and `implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1'` might duplicate some APIs. What happens if you comment out one of those? – Onik Jul 21 '18 at 09:45
  • I don't have the same dependencies that Michael_Oslo has. Also I don't have androidx dependencies. I'm just getting the same error `Error: Program type already present: android.support.v4.media.MediaBrowserCompat$CustomActionCallback` – Etienne Lawlor Jul 21 '18 at 09:48
  • what happens if you remove "implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1'" or downgrade. – Anu Bhalla Jul 24 '18 at 11:47

6 Answers6

29

Option 1

Following worked for me Add the following in your gradle.properties file

android.useAndroidX = true
android.enableJetifier = false

Option 2 (if above does't work)

  1. Android studio -> Navigate -> Class
  2. Check include non-project classes
  3. Copy full class path android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat
  4. See where it is used. You may need to remove, one of them.

Option 3 you might be including package which is including modules as well so exclude the support-v4 module with following method

implementation ('org.eclipse.paho:org.eclipse.paho.android.service:1.0.2') {
      exclude group: 'com.android.support', module:'support-v4'
}

You can analyze the conflicting modules using ./gradlew :YOURPROJECT:dependencies from a command line in your project repository. Check especially your third party libraries for occurences of "com.android.support-":

terminal_output

Then exclude the conflicting modules from these dependencies like:

   implementation ("com.jakewharton:butterknife:8.8.1") {
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'support-annotation'
    exclude group: 'com.android.support', module: 'support-compat'
}
A1Gard
  • 4,070
  • 4
  • 31
  • 55
aanshu
  • 1,602
  • 12
  • 13
  • 1
    Option 1 was the one for me! How would one come to this conclusion? To add this to gradle.properties is fine, but I followed a guide on the developer.google-site and I said nothing about this. – Michael_Oslo Jul 29 '18 at 19:45
  • In my case option 1 worked but with: android.enableJetifier=true. And then I had to invalidate caches / restart – Aegir Sep 15 '18 at 14:16
  • I tried Option 1 and Option 3 (exclude dependency `dagger-android-support`) but after it show error: `Could not find method com.google.dagger:dagger-android-support:2.16() for arguments [build_52wgz5jkz60yl2yx90c3wcy3t$_run_closure2$_closure6@68a7e350] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.` – anber Oct 08 '18 at 14:47
27

Im using flutter, and Im adding some native libraries in android, I tried the solutions posted here, but the trick for me was android.enableJetifier = true instead false

So, adding the following code to the gradle.properties, my apps are running:

android.useAndroidX = true
android.enableJetifier = true
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
  • Also see [D8: Program type already present: android.support.v4.media.MediaBrowserCompat$CustomActionCallback](https://stackoverflow.com/a/54773290/432903) – prayagupa Jun 16 '19 at 20:06
  • 1
    This works for me and after I get another error about **dex enabled**, you can see here [link](https://github.com/flutter/flutter/issues/20747#issuecomment-414126225) – Álvaro Agüero Jul 16 '19 at 18:10
3

if you still getting error after

# gradle.properties
android.useAndroidX = true
android.enableJetifier = false

then you probably forgot about main activity that calling android.support.v7.app.AppCompatActivity change it to androidx.appcompat.app.AppCompatActivity

enter image description here

Muhriddin Ismoilov
  • 380
  • 1
  • 4
  • 11
3

adding following plugins

cordova plugin add cordova-plugin-androidx cordova plugin add cordova-plugin-androidx-adapter

solved the problem for me

2

At least for me the issue was with the implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1' dependency. I went into the menu in Android Studio to create a Blank Fragment in Kotlin just to see what that would look like and the dependency above was added.

Once i removed that dependency the error went away.

Etienne Lawlor
  • 6,817
  • 18
  • 77
  • 89
  • 3
    Another thing that worked for me was Refactor -> Migrate to AndroidX option in Android Studio. This seemed to resolve any things I may have missed when trying to do the AndroidX migration one dependency at a time. https://developer.android.com/jetpack/androidx/migrate – Etienne Lawlor Oct 31 '18 at 01:10
0

Some of your existing dependencies are using older versions of support library, try this

implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1' {  
exclude group: 'com.android.support'  
exclude module: 'support-v4'
}
Navneet Krishna
  • 5,009
  • 5
  • 25
  • 44