Dagger2 Android应用:接入

这部分先介绍Dagger2在工程中导入的步骤,
后面会接着说Dagger2在实际开发中的各种概念。

在Dagger2的笔记中所有涉及到的代码,
已经上传到Github,地址在最后一篇笔记的最后部分。

目录:
Dagger2 Android应用:依赖注入的背景
Dagger2 Android应用:@Component和@Module
Dagger2 Android应用:@Scope和@Subcomponent

导入 Dagger2 依赖

网上不少资源说要在Project和module下的build.gradle分别添加以下依赖,然而是错的

buildscript {
  dependencies {
     classpath  'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }
}
apply plugin: 'com.neenbedankt.android-apt'

会出现以下错误,android-apt报警,该插件的作者在官网发表声明证实了后续将不会继续维护android-apt,Google官方 插件提供了名为annotationProcessor的功能来完全代替android-apt

Error:android-apt plugin is incompatible with the Android Gradle plugin.  Please use 'annotationProcessor' configuration instead.

正确方式是直接在module下添加如下依赖就行,不需要增加插件什么的

annotationProcessor 'com.google.dagger:dagger-compiler:' + rootProject.dagger2

以上正确配置之后,就可以在代码里使用Dagger2了

参考链接:

Google官方文档
https://google.github.io/dagger

Google官方MVP+Dagger2架构详解
http://www.jianshu.com/p/01d3c014b0b1

Android常用开源工具(1)-Dagger2入门
http://blog.csdn.net/duo2005duo/article/details/50618171

你可能感兴趣的:(Dagger2 Android应用:接入)