android – Supporting a Dagger 2 dependency in a Dagger 1 project-ThrowExceptions

Exception or error:

This is a follow-up to my question about Play Services Cast Framework, where the solution seems to be updating from v17.0.0 to v18.0.0. The new version unfortunately has a dependency on Dagger 2, whilst the (large & complex) project is Dagger 1.

We are currently using:

api "com.google.android.gms:play-services-cast:17.0.0"
api "com.google.android.gms:play-services-cast-framework:17.0.0"

Updating these to 18.0.0 results in:

> 1 exception was raised by workers:
java.lang.RuntimeException: Duplicate class dagger.Lazy found in modules dagger-1.2.2.jar (com.squareup.dagger:dagger:1.2.2) and dagger-2.22.jar (com.google.dagger:dagger:2.22)
Duplicate class dagger.MembersInjector found in modules dagger-1.2.2.jar (com.squareup.dagger:dagger:1.2.2) and dagger-2.22.jar (com.google.dagger:dagger:2.22)
Duplicate class dagger.Module found in modules dagger-1.2.2.jar (com.squareup.dagger:dagger:1.2.2) and dagger-2.22.jar (com.google.dagger:dagger:2.22)
Duplicate class dagger.Provides found in modules dagger-1.2.2.jar (com.squareup.dagger:dagger:1.2.2) and dagger-2.22.jar (com.google.dagger:dagger:2.22)

Ideally I’d be able to let the new version of the library use Dagger 2 whilst the rest of the app uses Dagger 1. Reworking the project to use Dagger 2 is a multi-week undertaking.

Any ideas?


What I’ve tried already:

Excluding Dagger 2 from the cast framework via:

api("com.google.android.gms:play-services-cast:18.0.0") {
    exclude group:'com.google.dagger', module:'dagger'
}
api("com.google.android.gms:play-services-cast-framework:18.0.0") {
    exclude group:'com.google.dagger', module:'dagger'
}

unsurprisingly resulted in:

   java.lang.NoClassDefFoundError: Failed resolution of: Ldagger/internal/Preconditions;
        at com.google.android.datatransport.runtime.DaggerTransportRuntimeComponent$Builder.setApplicationContext(com.google.android.datatransport:transport-runtime@@2.1.0:150)

Forcing the cast framework to use an older version via the following also resulted in duplicate classes:

configurations.all {
    resolutionStrategy {
        preferProjectModules()
        dependencySubstitution {
            substitute(module("com.google.dagger:dagger:2.22")).with(module("com.squareup.dagger:dagger:1.2.2"))
        }
    }
}

Potentially related questions that didn’t help:

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *