Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger2 dependency - Gradle

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?

like image 950
Dabler Avatar asked Feb 22 '15 18:02

Dabler


People also ask

What is Dagger2?

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 dagger 2 used for?

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.

What is dagger Library in Android?

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.

Why do we need dagger in Android?

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.


2 Answers

Installing Dagger 2 on Android Studio 2

// 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:

  • dagger
  • dagger-compiler

Notes: Android Studio < 2.2

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" } 

Notes: Dagger < 2.1

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' } 
  • javax.annotation:jsr250-api:1.0
  • javax.annotation:javax.annotation-api:1.2
  • org.glassfish:javax.annotation:10.0-b28
like image 135
bcorso Avatar answered Oct 01 '22 17:10

bcorso


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!

like image 21
rahulrv Avatar answered Oct 01 '22 17:10

rahulrv