Thursday, February 2, 2017

How to setup Dagger 2 in Android Studio


Step 1 - Build.gradle (Project Level) 

// Top-level build file where you can add configuration options common to
//all sub-projects/modules.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.3.3'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files    }
}

allprojects {
    repositories {
        jcenter()
        maven{
            url = "https://maven.google.com"
        }
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}


Step 2 - Build.gradle (Module Level)

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        applicationId "com.verma.mobile.android.daggerdemo"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'),
                          'proguard-rules.pro'        }
    }
}


ext {
       supportLibraryVersion = '25.2.0'
       daggerVersion = '2.6'
    }


dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:appcompat-v7:$supportLibraryVersion"

    compile 'com.android.support.constraint:constraint-layout:1.0.1'
    testCompile 'junit:junit:4.12'

    compile  "com.google.dagger:dagger:$daggerVersion"

    apt "com.google.dagger:dagger-compiler:$daggerVersion"
}