Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issue Setting up Dagger 2.x for Android on AndroidStudio3.0 Canary4

I get following errors while setting up dagger 2.x in Android Studio 3.0 Canary 4

Error:(71, 20) Failed to resolve: com.google.dagger:dagger:2.x

Error:(73, 20) Failed to resolve: com.google.dagger:dagger-android:2.x

Error:(74, 20) Failed to resolve: com.google.dagger:dagger-android-support:2.x

My build files are like below:

dependencies {
    //For DI - Dagger 2
    implementation 'com.google.dagger:dagger:2.x'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
    implementation 'com.google.dagger:dagger-android:2.x' // If you're using classes in dagger.android
    implementation 'com.google.dagger:dagger-android-support:2.x' // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.x'
}

The project build file has below snippets

allprojects {
    repositories {
        jcenter()
        maven { url "https://maven.google.com" }
        maven { url "https://oss.sonatype.org/content/repositories/snapshots/" }
    }
}

Your help is appreciated...

like image 245
karthiks Avatar asked Jun 25 '17 18:06

karthiks


People also ask

What is the use of dagger 2 in Android?

Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.

What is the difference between Dagger and hilt?

In the Dagger-Android, we have to create the scope annotations like ActivityScope, FragmentScope for managing the lifecycle of an object. But Hilt provides the following basic scopes as components. By attaching @InstallIn annotation to a module, the module will get a limited lifetime belongs to the scope.

What is dagger in Android for Beginners?

Dagger generates code similar to what you would have written manually. Internally, Dagger creates a graph of objects that it can reference to find the way to provide an instance of a class. For every class in the graph, Dagger generates a factory-type class that it uses internally to get instances of that type.

What is the use of component in dagger?

Now Component in a Dagger works by creating a graph of all the dependencies in the project so that it can find out where it should get those dependencies when they are needed. In order to implement this, an interface needs to be created and should be annotated with @Component.


2 Answers

If you're like me to have got into this problem, this is what I did to get out of this situation.

I went to https://github.com/google/dagger/releases to figure out the latest release version of dagger, and found v2.11 to be the latest as on date. I replaced 2.x with 2.11 in the version portion for this libraries configuration in the build file and bingo the build is successful.

dependencies {
    //For DI - Dagger 2
    implementation 'com.google.dagger:dagger:2.11'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'
    implementation 'com.google.dagger:dagger-android:2.11' // If you're using classes in dagger.android
    implementation 'com.google.dagger:dagger-android-support:2.11' // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
}
like image 102
karthiks Avatar answered Sep 21 '22 19:09

karthiks


Edit: I completely agree with the comments mentioning the usage of specific version of this library instead of +, so for example 2.11 should be used instead of 2.+ 2.+ was intended to fix the issue with 2.x as most of beginners take 2.x literally like I did when I first used it. The x here meant to be latest minor version of the stable release. Please check the latest release notes and replace x with latest minor version of library.

Original Answer: I am sure by now you have resolved your issue, though after trying few others and this one too, I found a reliable solution and posting it for helping others. Instead of 2.x use 2.+.

It solved all issues for me, not only it resolved the above problem,it also make sure to pull the latest version dagger 2.x available.

It should look like this:

dependencies {
    implementation 'com.google.dagger:dagger:2.+'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.+'
    implementation 'com.google.dagger:dagger-android:2.+' // If you're using classes in dagger.android
    implementation 'com.google.dagger:dagger-android-support:2.+' // if you use the support libraries
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.+'
}

Thanks!

like image 33
Piyush Saxena Avatar answered Sep 19 '22 19:09

Piyush Saxena