文章1:Android 神兵利器Dagger2使用详解(一)基础使用(初级,系列文章 四篇,第一篇刚开始如果@Inject不能用,就清除缓存重启一下AS,在第三四篇里面还有dagger-android的文章链接,测试项目Dagger2Test1)
文章3:Android:dagger2让你爱不释手-基础依赖注入框架篇(中级,了解并写过demo,可看此文章,从初级掌握向中级掌握迈进)
陈同学推荐:
文章2:Dagger2神器入门(一)(初级,详细,从简单讲起,适合初学者由浅入深学习,测试项目Dagger2Test2、Dagger2Test3)
Dagger 2 系列(一) – 前奏篇:依赖注入的基本介绍
备注:此文主要参考文章1
1、概念
====
1.1、什么是依赖注入
依赖注入是一种面向对象的编程模式,它的出现是为了降低耦合性,所谓耦合就是类之间依赖关系,所谓降低耦合就是降低类和类之间依赖关系。通过注解的方式注入,可以很好地降低耦合性,Dagger2就是通过注解的方式完成依赖注入的。
1.2、添加依赖
如下:
implementation ‘com.google.dagger🗡2.17’
annotationProcessor ‘com.google.dagger:dagger-compiler:2.17’
完整文件:项目的build.gradle
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:3.6.3’
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
app的build.gradle
apply plugin: ‘com.android.application’
android {
compileSdkVersion 29
buildToolsVersion “29.0.3”
defaultConfig {
applicationId “com.gs.dagtest1”
minSdkVersion 24
targetSdkVersion 29
versionCode 1
versionName “1.0”
testInstrumentationRunner “androidx.test.runner.AndroidJUnitRunner”
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android-optimize.txt’), ‘proguard-rules.pro’
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: ‘libs’, include: [’*.jar’])
implementation ‘androidx.appcompat:appcompat:1.1.0’
implementation 'and
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享
roidx.constraintlayout:constraintlayout:1.1.3’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘androidx.test.ext:junit:1.1.1’
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.2.0’
implementation ‘com.google.dagger🗡2.17’
annotationProcessor ‘com.google.dagger:dagger-compiler:2.17’
// implementation ‘com.google.dagger:dagger-android:2.17’
// implementation ‘com.google.dagger:dagger-android-support:2.17’
// annotationProcessor ‘com.google.dagger:dagger-android-processor:2.17’
//butterknife
implementation ‘com.jakewharton:butterknife:10.2.1’
annotationProcessor ‘com.jakewharton:butterknife-compiler:10.2.1’
}
2、代码实战
======
2.1、创建一个Student类,并添加Inject注解
import javax.inject.Inject;
public class Student {
@Inject
public Student() {
}
}
备注1:添加@Inject后,使用Ctrl+F9(或者rebuild)进行一次编译。
备注2:javax.inject包的目录如下所示:
2.2、编译后项目的变化
在目录Dagger2Test1\app\build\generated\ap_generated_sources\debug\out\com\gs\dagtest1\bean可以看到一个Student_Factory的类。
具体内容:
// Generated by Dagger (https://google.github.io/dagger).
package com.gs.dagtest1.bean;
import dagger.internal.Factory;
public final class Student_Factory implements Factory {
private static final Student_Factory INSTANCE = new Student_Factory();
@Override
public Student get() {
return provideInstance();
}
public static Student provideInstance() {
return new Student();
}
public static Student_Factory create() {
return INSTANCE;
}
public static Student newStudent() {
return new Student();
}
}
可以看到这是一个工厂类,而我们可以通过三种方式获取到Student对象,如下:
Student student1 = Student_Factory.create().get();
LogUtils.e(student1.toString());
Student student2 = Student_Factory.newStudent();
LogUtils.e(student2.toString());
Student student3 = Student_Factory.provideInstance();
LogUtils.e(student3.toString());
打印结果如下:
综上,我们通过@Inject注解了一个Student的构造方法后,可以让编译器帮助我们生成一个对应的工厂类Student_Factory,通过该工厂类,我们可以通过三种方法获取到Student对象。
当然,通过这三种方式获取到Student对象我们自己就可以实现,这里只是让大家更方便地理解编译类。
2.3、获取Student对象
我们创建一个Activity来调用Student
public class Test1Activity extends AppCompatActivity {
@Inject
Student student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
ButterKnife.bind(this);
}
@OnClick(R.id.btn1)
public void onViewClicked() {
Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();
}
}
接下来我们创建一个Activity类,在这个类中创建一个成员变量Student,按照Dagger2给我们的指示,当我们需要一个Student,我们只需要在这个成员变量上方加一个@Inject注解,编译器会自动帮我们产生对应的代码,我们就可以直接使用这个Student对象了!
本案例中我们设置一个Button,点击Button后我们打印出这个Student对象。
事实真的如此吗?我们直接运行代码,并点击Button,很遗憾,直接报空指针异常:
显然,和平常使用的结果一样,@Inject并没有帮助我们初始化对应的Student对象,或者说,我们的Activity并没有使用刚才我们看到的Student_Factory类,不过也可以理解,我们并没有建立Activity和Student_Factory类之间的关系嘛。
2.4、创建Module类和Component接口
import dagger.Module;
@Module
public class Test1Module {
private Test1Activity activity;
Test1Module(Test1Activity activity) {
this.activity = activity;
}
}
import dagger.Component;
@Component(modules = Test1Module.class)
public interface Test1Component {
void inject(Test1Activity activity);
}
请注意,Module类上方的@Module注解意味着这是一个提供数据的【模块】,而Component接口上方的@Component(modules = Test1Module.class)说明这是一个【组件】(我更喜欢称呼它为注射器)。
突然出现的这两个类可以称得上是莫名其妙,因为我们从代码上来看并不知道这对于Student和Activity之间关系有什么实质性的进展,但假如我们这时在Activty中添加这一段代码:
public class Test1Activity extends AppCompatActivity {
@Inject
Student student;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
ButterKnife.bind(this);
initDatas();