Android Studio配置Dagger2 以及butterknife

一、配置butterknife

在build.gradle(Module)文件中的dependencies模块添加:

 dependencies {
    // add butterknife
    compile 'com.jakewharton:butterknife:6.1.0'
}

然后点击右上角Sync Now运行成功后可以在External Libraries中查看到butterknife的jar包。

二、配置Dagger2

1、在build.gradle(Module)文件中的dependencies模块添加:

 dependencies {
    // add dagger2
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'
}

然后点击Sync Now会发现运行失败,这是因为还没有添加android-apt插件的引用。

2、build.gradle(Module)文件顶部添加android-apt插件应用:

apply plugin: 'android-apt'

3、build.gradle(Project)文件的模块添加:

 dependencies {
        //add android-apt
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }

然后点击Sync Now运行成功,配置完成。下面给出整个build.gradle文件代码:

 

build.gradle(Module)文件:

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

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.lz.www.ambts"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'

    // add dagger2
    compile 'com.google.dagger:dagger:2.0'
    apt 'com.google.dagger:dagger-compiler:2.0'

    // add butterknife
    compile 'com.jakewharton:butterknife:6.1.0'

}

build.gradle(Project)文件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'

        //add android-apt
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'

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

allprojects {
    repositories {
        jcenter()
    }
}

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

 

posted @ 2016-07-29 16:05  无主之城  阅读(2143)  评论(0编辑  收藏  举报