How to Fix the "Duplicate Class ListenableFuture" Error in Android
If you're working on an Android project and see a build error about a duplicate ListenableFuture
class, you've encountered a common dependency conflict. This guide will explain why it happens and show you how to fix it.
Understanding the Error
The error message looks like this:
Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules:
guava-23.0.jar (com.google.guava:guava:23.0)
and
listenablefuture-1.0.jar (com.google.guava:listenablefuture:1.0)
This means two different libraries in your project are trying to provide the same class, causing a collision.
guava-23.0.jar
contains the full Guava library, which includesListenableFuture
.listenablefuture-1.0.jar
is a standalone, minimal artifact containing only theListenableFuture
class.
The conflict arises when one of your dependencies requires the standalone JAR, but your project also includes the full Guava library.
The Solution: Exclude the Standalone Module
The most reliable fix is to identify which dependency is pulling in the standalone listenablefuture
and exclude it.
Step 1: Find the Problematic Dependency
Open the terminal in Android Studio (View > Tool Windows > Terminal
) and run this command to see your project's dependency tree:
For Groovy (build.gradle):
./gradlew :app:dependencies
For Kotlin DSL (build.gradle.kts):
/gradlew :app:dependencies
(Note: If your main module has a name other than app
, replace it in the command.)
Scan the output for com.google.guava:listenablefuture:1.0
. This will show you which library is its parent.
Step 2: Exclude the Transitive Dependency
Once you've found the culprit, add an exclude
rule to its dependency declaration in your app/build.gradle
file.
Example in Kotlin DSL:
dependencies {
// ... other dependencies
implementation("com.example.someotherlibrary:core:1.2.3") {
exclude(group = "com.google.guava", module = "listenablefuture")
}
// Make sure you have a version of the full Guava library.
// Often, another Google library (like Firebase) will bring in a compatible
// Android version of Guava. If not, you might need to add it,
// or preferably, use constraints.
// Example: implementation("com.google.guava:guava:31.1-android")
}
Example in Groovy:
dependencies {
// ... other dependencies
implementation('com.example.someotherlibrary:core:1.2.3') {
exclude group: 'com.google.guava', module: 'listenablefuture'
}
// Make sure you have a version of the full Guava library.
// implementation 'com.google.guava:guava:31.1-android' // Example
}
Step 3: Sync, Clean, and Rebuild
Sync your project with the Gradle files.
Go to
Build > Clean Project
.Then, go to
Build > Rebuild Project
.
This should resolve the duplicate class error.
Additional Tips
Use Android-Compatible Guava: For Android projects, it's best to use the Android-specific version of Guava (e.g.,
com.google.guava:guava:31.1-android
) to avoid compatibility issues and reduce method count.Version Alignment: If you have multiple dependencies pulling in different versions of the full Guava library, you can force a specific version using a constraint:
dependencies {
constraints {
implementation("com.google.guava:guava:32.1.3-android") { // Use a recent Android-compatible version
because("Align Guava versions to avoid conflicts and ensure Android compatibility")
}
}
// ... your other dependencies
}
The Empty JAR: Some modern libraries depend on
listenablefuture-9999.0-empty-to-avoid-conflict-with-guava
, an intentionally empty artifact designed to prevent this exact conflict. If you see this, it's a sign of a well-designed dependency. Your conflict is likely from another library still using the old1.0
artifact.
By following these steps, you can eliminate the build error and get your project running smoothly again.
You may also find below article helpful:
How to Find Which Dependency Adds a Specific Library or Class in Android Studio
Comments
Post a Comment