Dagger实现依赖注入,为了解决程序直接的耦合度。
本例子主要为了实现简单的依赖注入
配置:
Project级别的build.gradle
// 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.1.2'
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()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Module级别的build.gradle
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.android.demo"
minSdkVersion 17
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.jakewharton:butterknife:8.0.1'
apt 'com.jakewharton:butterknife-compiler:8.0.1'
compile 'com.squareup.dagger:dagger:1.2.5'
apt 'com.squareup.dagger:dagger-compiler:1.2.5'
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/message"
android:layout_margin="10dp"
android:text="开始"/>
</RelativeLayout>
Student.java
package com.android.demo;
/**
* 学生
*/
public class Student {
String id;
String name;
public void setId(String id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public String getName() {
return name;
}
public Student(){}
}
AppModule.java
package com.android.demo;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
@Module(injects = {MainActivity.class},complete = false ,library = true)
public class AppModule {
@Provides @Singleton
public Student provideStudent(){
Student student = new Student();
student.setId("2014");
student.setName("WiseClown");
return student;
}
}
package com.android.demo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import javax.inject.Inject;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dagger.ObjectGraph;
public class MainActivity extends AppCompatActivity {
@Inject
Student student;
@BindView(R.id.message)
TextView message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ObjectGraph objectGraph = ObjectGraph.create(AppModule.class);
objectGraph.inject(this);
ButterKnife.bind(this);
}
@OnClick(R.id.btn)
public void submit(View view){
message.setText(student.getId()+"\n"+student.getName());
}
}
分析:
在MainActivity中需要Student类的实例,所以通过依赖注入器将Student类在AppModule中实例化后再注入MainActivity中。