├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── clwang │ │ └── chunyu │ │ └── me │ │ └── wcl_dagger_demo │ │ └── ApplicationTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── clwang │ │ │ └── chunyu │ │ │ └── me │ │ │ └── wcl_dagger_demo │ │ │ ├── DemoApplication.java │ │ │ ├── DemoComponent.java │ │ │ ├── DemoGraph.java │ │ │ ├── MainActivity.java │ │ │ ├── contents │ │ │ └── GitHubService.java │ │ │ ├── list │ │ │ ├── ListAdapter.java │ │ │ └── ReposListActivity.java │ │ │ └── modules │ │ │ ├── ApiModule.java │ │ │ └── MainModule.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_repos_list.xml │ │ └── item_repo.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── clwang │ └── chunyu │ └── me │ └── wcl_dagger_demo │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | wcl-dagger-demo -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 26 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | Android 46 | 47 | 48 | Android Lint 49 | 50 | 51 | Java 52 | 53 | 54 | Java language level migration aidsJava 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 76 | 77 | $USER_HOME$/.subversion 78 | 79 | 80 | 81 | 82 | 83 | 1.8 84 | 85 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 101 | 102 | 103 | 104 | 105 | 106 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # wcl-dagger-demo 2 | 3 | 全面解析依赖注入库 Dagger 的使用方式 4 | 5 | 项目文档:https://www.jianshu.com/p/2a26291104e5 6 | 7 | 本文的合集已经编著成书,**[高级Android开发强化实战](https://item.jd.com/12385680.html)**,欢迎各位读友的建议和指导。 8 | 9 | 在京东即可购买:https://item.jd.com/12385680.html 10 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | buildscript { 2 | repositories { 3 | jcenter() 4 | } 5 | dependencies { 6 | classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' 7 | } 8 | } 9 | 10 | // Lambda表达式 11 | plugins { 12 | id "me.tatarka.retrolambda" version "3.2.4" 13 | } 14 | 15 | apply plugin: 'com.android.application' 16 | apply plugin: 'com.neenbedankt.android-apt' // 注释处理 17 | 18 | android { 19 | compileSdkVersion 23 20 | buildToolsVersion '25.0.0' 21 | 22 | defaultConfig { 23 | applicationId "clwang.chunyu.me.wcl_dagger_demo" 24 | minSdkVersion 17 25 | targetSdkVersion 23 26 | versionCode 1 27 | versionName "1.0" 28 | } 29 | 30 | buildTypes { 31 | release { 32 | minifyEnabled false 33 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 34 | } 35 | } 36 | 37 | // 注释冲突 38 | packagingOptions { 39 | exclude 'META-INF/services/javax.annotation.processing.Processor' 40 | } 41 | 42 | // 使用Java1.8 43 | compileOptions { 44 | sourceCompatibility JavaVersion.VERSION_1_8 45 | targetCompatibility JavaVersion.VERSION_1_8 46 | } 47 | } 48 | 49 | dependencies { 50 | compile fileTree(dir: 'libs', include: ['*.jar']) 51 | testCompile 'junit:junit:4.12' 52 | compile 'com.android.support:appcompat-v7:23.1.1' 53 | compile 'com.android.support:recyclerview-v7:23.1.1' // RecyclerView 54 | 55 | compile 'com.jakewharton:butterknife:7.0.1' // 标注 56 | 57 | compile 'com.google.dagger:dagger:2.0.2' // dagger2 58 | compile 'com.google.dagger:dagger-compiler:2.0.2' // dagger2 59 | 60 | compile 'io.reactivex:rxandroid:1.1.0' // RxAndroid 61 | compile 'io.reactivex:rxjava:1.1.0' // 推荐同时加载RxJava 62 | 63 | compile 'com.squareup.retrofit:retrofit:2.0.0-beta2' // Retrofit网络处理 64 | compile 'com.squareup.retrofit:adapter-rxjava:2.0.0-beta2' // Retrofit的rx解析库 65 | compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2' // Retrofit的gson库 66 | 67 | provided 'javax.annotation:jsr250-api:1.0' // Java标注 68 | } 69 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/wangchenlong/Installations/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/clwang/chunyu/me/wcl_dagger_demo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/DemoApplication.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo; 2 | 3 | import android.app.Application; 4 | 5 | /** 6 | * 应用信息 7 | *

8 | * Created by wangchenlong on 16/1/2. 9 | */ 10 | public class DemoApplication extends Application { 11 | private static DemoGraph sDemoGraph; 12 | private static DemoApplication sInstance; 13 | 14 | @Override public void onCreate() { 15 | super.onCreate(); 16 | sInstance = this; 17 | buildComponentAndInject(); 18 | } 19 | 20 | public static DemoGraph component() { 21 | return sDemoGraph; 22 | } 23 | 24 | public static void buildComponentAndInject() { 25 | sDemoGraph = DemoComponent.Initializer.init(sInstance); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/DemoComponent.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo; 2 | 3 | import javax.inject.Singleton; 4 | 5 | import clwang.chunyu.me.wcl_dagger_demo.modules.ApiModule; 6 | import clwang.chunyu.me.wcl_dagger_demo.modules.MainModule; 7 | import dagger.Component; 8 | 9 | /** 10 | * 组件 11 | * Created by wangchenlong on 16/1/2. 12 | */ 13 | @Singleton 14 | @Component(modules = {MainModule.class, ApiModule.class}) 15 | public interface DemoComponent extends DemoGraph { 16 | final class Initializer { 17 | private Initializer() { 18 | } // No instances. 19 | 20 | // 初始化组件 21 | public static DemoComponent init(DemoApplication app) { 22 | return DaggerDemoComponent.builder() 23 | .mainModule(new MainModule(app)) 24 | .build(); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/DemoGraph.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo; 2 | 3 | import clwang.chunyu.me.wcl_dagger_demo.list.ReposListActivity; 4 | 5 | /** 6 | * Dagger2的图接口 7 | *

8 | * Created by wangchenlong on 16/1/2. 9 | */ 10 | public interface DemoGraph { 11 | void inject(MainActivity mainActivity); // 注入MainActivity 12 | 13 | void inject(ReposListActivity reposListActivity); // 注入列表Activity 14 | } 15 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.View; 7 | 8 | import butterknife.ButterKnife; 9 | import clwang.chunyu.me.wcl_dagger_demo.list.ReposListActivity; 10 | 11 | /** 12 | * 主模块, 注册类. 13 | */ 14 | public class MainActivity extends AppCompatActivity { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | ButterKnife.bind(this); 22 | 23 | DemoApplication.component().inject(this); // 应用注入 24 | } 25 | 26 | // 跳转列表视图 27 | public void gotoReposList(View view) { 28 | startActivity(new Intent(this, ReposListActivity.class)); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/contents/GitHubService.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo.contents; 2 | 3 | import java.util.ArrayList; 4 | 5 | import clwang.chunyu.me.wcl_dagger_demo.list.ListAdapter; 6 | import retrofit.http.GET; 7 | import retrofit.http.Path; 8 | import rx.Observable; 9 | 10 | /** 11 | * GitHub服务 12 | *

13 | * Created by wangchenlong on 16/1/2. 14 | */ 15 | public interface GitHubService { 16 | String ENDPOINT = "https://api.github.com"; 17 | 18 | // 获取库, 获取的是数组 19 | @GET("/users/{user}/repos") 20 | Observable> getRepoData(@Path("user") String user); 21 | } 22 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/list/ListAdapter.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo.list; 2 | 3 | import android.support.v7.widget.RecyclerView; 4 | import android.view.LayoutInflater; 5 | import android.view.View; 6 | import android.view.ViewGroup; 7 | import android.widget.TextView; 8 | 9 | import java.util.ArrayList; 10 | 11 | import butterknife.Bind; 12 | import butterknife.ButterKnife; 13 | import clwang.chunyu.me.wcl_dagger_demo.R; 14 | 15 | /** 16 | * RecyclerView的Adapter 17 | *

18 | * Created by wangchenlong on 16/1/2. 19 | */ 20 | public class ListAdapter extends RecyclerView.Adapter { 21 | private ArrayList mRepos; // 库信息 22 | public ListAdapter() { 23 | mRepos = new ArrayList<>(); 24 | } 25 | 26 | public void setRepos(ArrayList repos) { 27 | mRepos = repos; 28 | notifyItemInserted(mRepos.size() - 1); 29 | } 30 | 31 | @Override 32 | public RepoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 33 | View view = LayoutInflater.from(parent.getContext()) 34 | .inflate(R.layout.item_repo, parent, false); 35 | return new RepoViewHolder(view); 36 | } 37 | 38 | @Override public void onBindViewHolder(RepoViewHolder holder, int position) { 39 | holder.bindTo(mRepos.get(position)); 40 | } 41 | 42 | @Override public int getItemCount() { 43 | return mRepos.size(); 44 | } 45 | 46 | public static class RepoViewHolder extends RecyclerView.ViewHolder { 47 | @Bind(R.id.item_iv_repo_name) TextView mIvRepoName; 48 | @Bind(R.id.item_iv_repo_detail) TextView mIvRepoDetail; 49 | 50 | public RepoViewHolder(View itemView) { 51 | super(itemView); 52 | ButterKnife.bind(this, itemView); 53 | } 54 | 55 | public void bindTo(Repo repo) { 56 | mIvRepoName.setText(repo.name); 57 | mIvRepoDetail.setText(String.valueOf(repo.description + "(" + repo.language + ")")); 58 | } 59 | } 60 | 61 | public static class Repo { 62 | public String name; // 库的名字 63 | public String description; // 描述 64 | public String language; // 语言 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/list/ReposListActivity.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo.list; 2 | 3 | import android.app.Activity; 4 | import android.os.Bundle; 5 | import android.support.v7.widget.LinearLayoutManager; 6 | import android.support.v7.widget.RecyclerView; 7 | 8 | import javax.inject.Inject; 9 | 10 | import butterknife.Bind; 11 | import butterknife.ButterKnife; 12 | import clwang.chunyu.me.wcl_dagger_demo.DemoApplication; 13 | import clwang.chunyu.me.wcl_dagger_demo.R; 14 | import clwang.chunyu.me.wcl_dagger_demo.contents.GitHubService; 15 | import rx.android.schedulers.AndroidSchedulers; 16 | import rx.schedulers.Schedulers; 17 | 18 | /** 19 | * 代码库列表 20 | *

21 | * Created by wangchenlong on 16/1/2. 22 | */ 23 | public class ReposListActivity extends Activity { 24 | @Bind(R.id.repos_rv_list) RecyclerView mRvList; 25 | 26 | @Inject GitHubService mGitHubService; 27 | 28 | @Override protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_repos_list); 31 | ButterKnife.bind(this); 32 | 33 | DemoApplication.component().inject(this); 34 | 35 | LinearLayoutManager manager = new LinearLayoutManager(this); 36 | manager.setOrientation(LinearLayoutManager.VERTICAL); 37 | mRvList.setLayoutManager(manager); 38 | 39 | ListAdapter adapter = new ListAdapter(); 40 | mRvList.setAdapter(adapter); 41 | loadData(adapter); 42 | } 43 | 44 | // 加载数据 45 | private void loadData(ListAdapter adapter) { 46 | mGitHubService.getRepoData("SpikeKing") 47 | .subscribeOn(Schedulers.newThread()) 48 | .observeOn(AndroidSchedulers.mainThread()) 49 | .subscribe(adapter::setRepos); 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/modules/ApiModule.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo.modules; 2 | 3 | import javax.inject.Singleton; 4 | 5 | import clwang.chunyu.me.wcl_dagger_demo.contents.GitHubService; 6 | import dagger.Module; 7 | import dagger.Provides; 8 | import retrofit.GsonConverterFactory; 9 | import retrofit.Retrofit; 10 | import retrofit.RxJavaCallAdapterFactory; 11 | 12 | /** 13 | * 接口模块 14 | *

15 | * Created by wangchenlong on 16/1/2. 16 | */ 17 | @Module 18 | public class ApiModule { 19 | 20 | @Provides 21 | @Singleton 22 | protected GitHubService provideGitHubService() { 23 | Retrofit retrofit = new Retrofit.Builder() 24 | .baseUrl(GitHubService.ENDPOINT) 25 | .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) // 添加Rx适配器 26 | .addConverterFactory(GsonConverterFactory.create()) // 添加Gson转换器 27 | .build(); 28 | return retrofit.create(GitHubService.class); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /app/src/main/java/clwang/chunyu/me/wcl_dagger_demo/modules/MainModule.java: -------------------------------------------------------------------------------- 1 | package clwang.chunyu.me.wcl_dagger_demo.modules; 2 | 3 | import android.app.Application; 4 | import android.content.res.Resources; 5 | 6 | import javax.inject.Singleton; 7 | 8 | import clwang.chunyu.me.wcl_dagger_demo.DemoApplication; 9 | import dagger.Module; 10 | import dagger.Provides; 11 | 12 | /** 13 | * 主要模块, 提供Application和resources. 14 | *

15 | * Created by wangchenlong on 16/1/2. 16 | */ 17 | @Module 18 | public class MainModule { 19 | private final DemoApplication mApp; 20 | 21 | public MainModule(DemoApplication application) { 22 | mApp = application; 23 | } 24 | 25 | @Provides 26 | @Singleton 27 | protected Application provideApplication() { 28 | return mApp; 29 | } 30 | 31 | @Provides 32 | @Singleton 33 | protected Resources provideResources() { 34 | return mApp.getResources(); 35 | } 36 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 |