I'm trying to add Dagger2 to my project in Android Studio but I can't find proper dependency to paste in build.gradle. Could you help and send me the proper line?
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.
Dagger is arguably the most used Dependency Injection, or DI, framework for Android. Many Android projects use Dagger to simplify building and providing dependencies across the app. It gives you the ability to create specific scopes, modules, and components, where each forms a piece of a puzzle: The dependency graph.
Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.
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.
// Application build.gradle dependencies { compile 'com.google.dagger:dagger:2.4' annotationProcessor "com.google.dagger:dagger-compiler:2.4" }
Maven Repositories:
Find the latest versions of the above dependencies in the Maven Repository:
Older versions of Android Studio need android-apt
for annotation processing.
// Project build.gradle buildscript { dependencies { // Assists in working with annotation processors for Android Studio. // No longer needed with Android Studio 2.2+ classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } } apply plugin: 'com.neenbedankt.android-apt'
And
// Application build.gradle dependencies { compile 'com.google.dagger:dagger:2.4' apt "com.google.dagger:dagger-compiler:2.4" }
For Dagger < 2.1-SNAPSHOT the javax.annotation
is needed for the @Generated
annotation used in Dagger generated code (see github.com/google/dagger/issues/95). The annotation is not included in the Android API jar, so you'll need to use one of these libraries (see differences):
// Application build.gradle dependencies { compile 'javax.annotation:jsr250-api:1.0' }
You don't need the android-apt plugin anymore,all functionality that was previously provided by android-apt is now available in the Android Gradle plugin version 2.2
https://bitbucket.org/hvisser/android-apt/wiki/Migration
Update Gradle plugin to
classpath 'com.android.tools.build:gradle:2.2.0'
and Dagger dependencies to
compile 'com.google.dagger:dagger:2.4' annotationProcessor 'com.google.dagger:dagger-compiler:2.4'
Cheers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With