如何解决“程序类型已存在:com.google.common.util.concurrent.ListenableFuture”问题?

63

我正在尝试使用 WorkManager 1.0.0-alpha09,但遇到了这个错误:

Program type already present: 
com.google.common.util.concurrent.ListenableFuture

Message{kind=ERROR, text=Program type already present: 
com.google.common.util.concurrent.ListenableFuture, sources=[Unknown source 
file], tool name=Optional.of(D8)}
如果我使用版本1.0.0-alpha08或更低版本,就不会出现这个错误,但我需要公共构造函数。
public Worker(Context context, WorkerParameters workerParams)
4个回答

107
在我的情况下,我必须将以下配置添加到应用程序模块的build.gradle文件中。
configurations {
    all*.exclude group: 'com.google.guava', module: 'listenablefuture'
}

这种情况通常是由于一些依赖同时使用了com.google.guava:guavacom.google.guava:listenablefuture所致,导致依赖冲突。


你把这个放在哪个build.gradle文件里了?是只放在根目录下还是放在某个子项目中了? - Gillis Haasnoot
@GillisHaasnoot 把它放在应用程序的模块中。 - Anggrayudi H
4
只是为了提供更多关于位置的细节:它在应用程序的build.gradle中,配置应该与android{}和dependencies{}处于“相同级别”。 - fangzhzh
@AyejuniIlemobayoKings 在 app/build.gradle 文件中应该放在哪里?是在 android 下面吗? - questionasker
@AyejuniIlemobayoKings 在 app/build.gradle 文件中 android 块之内和之外。 - Anggrayudi H
谢谢,这对我有用。只需在app > build.gradle中添加到android{}即可。 - yubaraj poudel

40

我遇到了这个错误:org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'................................Caused by: com.android.builder.merge.DuplicateRelativeFileException: More than one file was found with OS independent path 'META-INF/proguard/androidx-annotations.pro' - Simran Marok
1
尝试清理Android Studio的缓存。选择“文件>使高速缓存失效/重启”,然后单击“使其失效并重新启动”按钮。清理和重建您的项目。 - pfmaggi
@Rahul,我想问一下,这样做会有什么副作用吗?它会从其他依赖项中选择错误的listenablefuture库版本吗? - Cheok Yan Cheng
不,接口是相同的。没有副作用。 - Rahul

20

我只是在我的应用 gradle 文件的末尾添加了 implementation 'com.google.guava:guava:27.0.1-android',然后错误就消失了。


2
我正在使用来自工作管理器的ListenableFuture。"最初的回答"。
implementation("android.arch.work:work-runtime:1.0.0")

所以,除了exclude group: 'com.google.guava', module: 'listenablefuture'之外,对我没有用。

我正在使用一个依赖项,它在内部使用androidTestImplementation "com.google.truth:truth:42",该依赖项在内部使用com.google.guava:guava:25.0.1-android。这对我造成了问题。

com.google.truth:truth升级到43解决了我的问题。

注:Original Answer翻译成"最初的回答"。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接