“程序类型已经存在”是什么意思?

我想在安卓工作室里开发一个应用。在添加 Eclipse Paho 库作为渐变依赖项之后(或者它是 Maven?我是 Android 生态系统的新手) ,我得到了以下错误:

Program type already present: android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat
Message{kind=ERROR, text=Program type already present: android.support.v4.accessibilityservice.AccessibilityServiceInfoCompat, sources=[Unknown source file], tool name=Optional.of(D8)}

我已经检查了许多与此错误相关的 StackOverflow 问题,但是答案都是特定于某些库的。这样人们就更容易找到针对特定情况的解决方案。到目前为止,还没有答案。

从 StackOverflow 的其他答案中,我总结出它与我的 gradle 文件有关,下面是 app/build.gradle:

apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "---REDACTED FOR PRIVACY---"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.0'
implementation 'com.android.support:support-media-compat:27.1.0'
implementation 'com.android.support:support-v13:27.1.0'
implementation 'com.google.android.gms:play-services-maps:12.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
implementation 'org.eclipse.paho:org.eclipse.paho.android.service:1.0.2'
}


repositories {
maven { url 'https://repo.eclipse.org/content/repositories/paho-releases/' }
}
86908 次浏览

This problem usually come from a naming conflict, in your case the support-v4 library, which is use by several libraries.

To find the list of dependencies for the module app (default module's name for the app) we can do a gradlew app:dependencies to retrieve a list of all the libraries.

We found that support-v4 is used by:

//short version of the dependencies list highlighting support-v4
+--- com.android.support:support-v13:27.1.0
|    \--- com.android.support:support-v4:27.1.0


+--- com.google.android.gms:play-services-maps:12.0.1
|    +--- com.google.android.gms:play-services-base:12.0.1
|    |    +--- com.google.android.gms:play-services-basement:12.0.1
|    |    |    +--- com.android.support:support-v4:26.1.0 -> 27.1.0 (*)


+--- org.eclipse.paho:org.eclipse.paho.android.service:1.0.2
|    +--- com.google.android:support-v4:r7  // <- problem here

We see that the support-v4 on Maps will use the version provided from support-v13.

We also see that the eclipse library is using another version (r7 ??).

To resolve your issue, you can try to exclude the module support-v4 from this eclipse library like this:

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

Then you should be able to compile your application.

Btw you should take care that the eclipse module won't break by testing your code.

Add Support library to app level Gradle file

implementation 'com.android.support:design:27.1.0'

From official Doc

If a class appears more than once on the runtime classpath, you get an error similar to the following:

Program type already present com.example.MyClass

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. ( See if same library is added as jar and gradle dependency)

For me just cleaning the project solved the issue

Using Terminal :

./gradlew clean

Using Android Studio :

Build (menu) > Clean Project

It's happened to me also but in my case, I try to include different dependencies that have same class using debugApi & Api so Android Studio marked as duplicate class, so I solved the issue by using debugApi & releaseApi to include different dependencies based on the build variant.

In my case it mean I have 2 *.jar file or 2 libary some where in source code. For example: I have 2 youtube.jar in app/libary and module/libary Delete the redundant once and it will be fine

I hope it helps somebody, a Build > Clean Project worked for me.

Problem for this issue - If you use the library as module & same library as dependency in another library.

Example: LibraryA imported as Module & same LibraryA added as dependency in any other library module.

How to fix this issue?

Solution 1 - > if you want to keep both -> Just refactor the package name of LibraryA module

Solution 2 - > remove the LibraryA dependency and use the module

implementation project(':LibraryA')

I have the same problem but with my project's internal library after updating from Android Studio 4.0.2 to 4.1. I have searched and tried many things without success. If you guys have the same case as me, I hope this answer can help. What I have done:

  1. Clean Project then Rebuild Project - failed
  2. Invalidate Caches / Restart - failed
  3. Delete global Gradle caches (/User/username/.gradle/caches for macOS) - failed
  4. Investigating app dependencies via terminal with the command: ./gradlew app:dependencies or via Gradle tool window: project->Tasks->android->androidDependencies - nothing suspicious
  5. Delete .gradle and .idea from my root project - success (although I'm not sure if I need to delete both. Maybe just delete one of them also working)

The reason I was getting this error was because I converted a Java file to a Kotlin one like this:

Code (menu) -> Convert Java file to Kotlin file

The old Java class must have still been hanging around on the classpath resulting in a duplicate. Doing a clean fixed it:

Build (menu) -> Clean Project