Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Program type already present: android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat

After upgrading to Android Studio 3.1, I started to get following error during build. Project uses multidex and DX is enabled by default as you would notice in the error. I tried to check dependency graph to understand what is going on but so far have no clue. Interestingly this only fails on my machine. I cleaned up everything, including reinstall etc but nothing worked.

Anyone had the same issue and how did you solve it? Or any direction that I can take a look?

AGPBI: {
    "kind":"error",
    "text":"Program type already present: android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat",
    "sources":[{}],
    "tool":"D8"
}

This is the task that fails:

transformDexArchiveWithExternalLibsDexMergerForDebug

I checked similar issues and it seems random things fixes their problem, I'm not sure what is the real cause.

like image 429
Orhan Obut Avatar asked Apr 12 '18 01:04

Orhan Obut


4 Answers

For my solution (I do not know it will work for you):

Firstly I followed @Orhan Obut's solution:

Search for duplicate classes in your project

I found that there are more than one class files in different libraries.

Then I put the ignore annotation above my support dependency in my project module's build.gradle (app folder):

 //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'

I realized that ignorance is no solution, because the error did not go away, even after clean-rebuilding and clearing/invalidating cache for the project.

See: Infographic: 11 Most Common Android Errors and How to Fix Them

So I explored more, and found out this link:

Android - Understanding and dominating gradle dependencies

It suggests ways to resolve conflicts. Hence I put this on my gradle just above the declarations of dependencies:

configurations.all {exclude group: 'com.android.support', module: 'support-v4'}

Since then when I search for duplicate classes for this one using @Orhan Obut's solution above, I find only single entry in the result. That meant that there were no duplicates.

Also, it will be better if you migrate to AndroidX with latest SDK and build tools. Make sure you don't have older support dependencies anywhere.

Happy Coding :-)

like image 82
Abhinav Saxena Avatar answered Oct 18 '22 22:10

Abhinav Saxena


I have my solution by change this:

implementation 'com.android.support:appcompat-v7:27.0.0'

to

implementation 'com.android.support:appcompat-v7:26.0.0'

it works for me.

like image 40
mas bro Avatar answered Oct 18 '22 23:10

mas bro


I managed to determine the root cause by using the following steps. It may be different use case for each issue, therefore this is the way to determine the root cause.

  • Go to android studio
  • Navigate -> Class
  • Check include non-project classes
  • Copy paste full class path with package name. android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat
  • You should be able to see where it is used. Most probably you may need to remove it from one of them.

In my case the issue was ViewPagerIndicator library was downloading support library as jar. Removing it solved the issue.

like image 25
Orhan Obut Avatar answered Oct 18 '22 23:10

Orhan Obut


For the easy option just add

configurations.all {exclude group: 'com.android.support', module: 'support-v4'}

before dependencies in build.gradle app module, it should ignore v4 support libraries, and the duplicate error will go away.

like image 6
Aegon Avatar answered Oct 18 '22 23:10

Aegon