66

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?

Dabler
  • 877
  • 1
  • 7
  • 11

6 Answers6

145

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:


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'
}
bcorso
  • 45,608
  • 10
  • 63
  • 75
8

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!

rahul.ramanujam
  • 5,608
  • 7
  • 34
  • 56
5
dependencies {
    implementation 'com.google.dagger:dagger:2.0-SNAPSHOT'
}

in your app/build.gradle

and

allprojects {
    repositories {
        ...
        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots"
        }
    }
}

in build.gradle of your project.

AMK
  • 662
  • 6
  • 16
slnowak
  • 1,839
  • 3
  • 23
  • 37
  • 1
    The current latest build available is 2.1-SNAPSHOT ( so you can use `compile 'com.google.dagger:dagger:2.1-SNAPSHOT' `) – Francesco Pez May 04 '15 at 18:29
  • Dagger 2.0 is now out in Maven Central proper. Don't use snapshots unless you're prepared to deal with breakage. – Veeti May 05 '15 at 18:36
5

I had some trouble with this earlier today. Here's what worked for me with the latest versions as of this date using Android Studio 2.0 preview 8:

buid.gradle (Module:app)

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
android {
    compileSdkVersion 23
    buildToolsVersion '23.0.2'
    defaultConfig {
        applicationId 'com.example.android.redacted.app'
        minSdkVersion 16
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    repositories {
        mavenCentral()
    }
    productFlavors {
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.google.dagger:dagger:2.0.2'
    apt 'com.google.dagger:dagger-compiler:2.0.2'
    provided 'org.glassfish:javax.annotation:10.0-b28'

}

build.gradle (Project:proj):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.0.0-alpha8'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven {
            url 'https://oss.sonatype.org/content/repositories/snapshots/'
        }
    }

}
Jon Teets
  • 61
  • 1
  • 3
0

Add these latest dependencies in your app/build.gradle with the latest version of Android studios 3.0

dependencies {

//Dagger
implementation 'com.google.dagger:dagger:2.24'
implementation 'com.google.dagger:dagger-android:2.24'
implementation 'com.google.dagger:dagger-android-support:2.24'

}

Simple implementation of Dagger 2 Dagger 2 with MVP

ramkrishna kushwaha
  • 380
  • 1
  • 6
  • 17
0

Updating latest version of Dagger-2 dependencies

Current version: 2.36. You can find the latest versions of all the below dependencies here.

For core dagger dependencies,

dependencies {
  implementation 'com.google.dagger:dagger:2.36'
  annotationProcessor 'com.google.dagger:dagger-compiler:2.36'
}

For android dependencies,

implementation 'com.google.dagger:dagger-android:2.36'
implementation 'com.google.dagger:dagger-android-support:2.36'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.36'

If your project is using Kotlin, then use kapt instead of annotationProcessor. To use kapt, you need to add the plugin kotlin-kapt in your Gradle.

Anand
  • 857
  • 1
  • 12
  • 18
  • actually Im a Naive in android .So , can you please tell what the diff. betn 'Android Dependencies' and 'dependencies' – Sumit Aug 13 '21 at 11:45