dagger

Dagger 源码解析

∥☆過路亽.° 提交于 2019-12-03 00:34:11
1. 功能介绍 1.1 Dagger Dagger 是一款 Java 平台的依赖注入库,关于依赖注入,详细见 依赖注入简介 。 Java 的依赖注入库中,最有名的应该属 Google 的 Guice,Spring 也很有名,不过是专注于 J2EE 开发。Guice 的功能非常强大,但它是通过在运行时读取注解来实现依赖注入的,依赖的生成和注入需要依靠 Java 的反射机制,这对于对性能非常敏感的 Android 来说是一个硬伤。基于此,Dagger 应运而生。 Dagger 同样使用注解来实现依赖注入,但它利用 APT(Annotation Process Tool) 在编译时生成辅助类,这些类继承特定父类或实现特定接口,程序在运行时 Dagger 加载这些辅助类,调用相应接口完成依赖生成和注入。Dagger 对于程序的性能影响非常小,因此更加适用于 Android 应用的开发。 1.2 依赖注入相关概念 依赖(Dependency): 如果在 Class A 中,有个属性是 Class B 的实例,则称 Class B 是 Class A 的依赖,本文中我们将 Class A 称为宿主(Host),并且全文用 Host 表示;Class B 称为依赖(Dependency),并且全文用 Dependency 表示。一个 Host 可能是另外一个类的 Dependency。 宿主

What is actual usage of “HasFragmentInjector” in Dagger 2

不羁的心 提交于 2019-12-02 20:44:10
I have implemented dagger2 v2.2 previously but as now they have added dagger.android part also. so I am creating sample project with that. I am aware about old methodology of @Provide and @Modules and @Components etc annotations but from Dagger 2.8+ they have added this android-support library also which have some new Injections like @ActivityKey , @ContributesAndroidInjector , @Subcomponent.Builder etc. So my question is what benefits it brings to the table. Does it resolve problems like Inject method with base class can work for all child class ? or Any other benefits ? 2nd question -

Use Dagger modules without the “injects” directive

只愿长相守 提交于 2019-12-02 19:14:33
I am trying to make Dagger work without the "injects" directive inside the @Module annotation. I am basing my test project on the Android Simple Dagger example This is the part that is giving me problems: @Module( injects = HomeActivity.class, complete = false ) public class DemoModule { // TODO put your application-specific providers here! } (Edit): Which in my code is CTXModules.java The part that I'd like to remove is the "injects = HomeActivity.class". I know I can mark my own modules with the @Inject annotation in the constructor to remove that part there, but somehow it doesn't work with

Using Dagger for dependency injection on constructors

孤街浪徒 提交于 2019-12-02 18:31:43
So, I'm currently redesigning an Android app of mine to use Dagger . My app is large and complicated, and I recently came across the following scenario: Object A requires a special DebugLogger instance which is a perfect candidate for injection. Instead of passing around the logger I can just inject it through A's constructor. This looks something like this: class A { private DebugLogger logger; @Inject public A(DebugLogger logger) { this.logger = logger; } // Additional methods of A follow, etc. } So far this makes sense. However, A needs to be constructed by another class B. Multiple

Dagger 2 component not generated

倾然丶 夕夏残阳落幕 提交于 2019-12-02 17:56:34
In my module, in my base Application class component = DaggerCompClassComponent.builder() .classModule(new ModuleClass()).build(); it can not find DaggerCompClassComponent. I have on module build.gradle apply plugin: 'com.neenbedankt.android-apt' ......................... apt 'com.google.dagger:dagger-compiler:2.8' compile 'com.google.dagger:dagger:2.8' provided 'javax.annotation:jsr250-api:1.0' and in Project build.gradle, classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' I have done build / rebuild / clean / restart project. I have a Component class where I inject objects and a

Why @ContributesAndroidInjector doesn't provide Android Framework type

左心房为你撑大大i 提交于 2019-12-02 15:59:22
问题 I have simplified my application to get the root of the problem and here is the simplified version. I'm implementing Dagger 2 using following configuration: AppComponent @Component(modules = [ AndroidSupportInjectionModule::class, ActivityBindingModule::class ]) interface AppComponent: AndroidInjector<MyApp> { @Component.Builder interface Builder{ @BindsInstance fun application(application: Application): Builder fun build(): AppComponent } } ActivityBindingModule @Module abstract class

Dagger AndroidInjector cannot be provided without an @Provides-annotated method

Deadly 提交于 2019-12-02 14:39:11
问题 I've done my Android projects MainActivity by means of the MVP pattern. So in my MainPresenter I want to inject a dynamic String, which then shall populate a TextView for instance: class MyMainPresenter @Inject constructor(@StringForTextView dynamicString : String ) whereas the StringForTextView annotation qualifier is defined as: import javax.inject.Qualifier @Qualifier @MustBeDocumented @Retention(AnnotationRetention.RUNTIME) annotation class StringForTextView then I also have my Interface:

Dagger AndroidInjector cannot be provided without an @Provides-annotated method

廉价感情. 提交于 2019-12-02 11:40:30
I've done my Android projects MainActivity by means of the MVP pattern. So in my MainPresenter I want to inject a dynamic String, which then shall populate a TextView for instance: class MyMainPresenter @Inject constructor(@StringForTextView dynamicString : String ) whereas the StringForTextView annotation qualifier is defined as: import javax.inject.Qualifier @Qualifier @MustBeDocumented @Retention(AnnotationRetention.RUNTIME) annotation class StringForTextView then I also have my Interface: interface DynamicString { @NonNull fun getDynamicString() : String } which is implemented as: data

Android Testing with Robolectric and Dagger

老子叫甜甜 提交于 2019-12-02 08:14:49
I am trying to write an Android application using Dagger. Trying to follow the TDD approach, I started writing a test for my First activity. For writing tests I am using Robolectric and I am trying to make it work in different scenarios using Mockito. Short story: I have an android activity which I want to test using robolectric. This activity has some of its dependencies provided through Dagger. I managed to make this work by overriding the Application class and providing a mock of the utility class. What I need now is to be able to change the behavior of the utility class(using Mockito) in

Dagger 2 : error while getting a multiple instances of same object with @Named

我的梦境 提交于 2019-12-01 22:22:34
How can i get multiple instances of same return type like cursor for example :- Module @CursorScope public class CursorModule { @Provides Cursor provideSongCursor( @Named("Song") Musician musician) { return musician.getApplicationContext().getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, new String[]{ BaseColumns._ID, MediaStore.Audio.AudioColumns.TITLE, MediaStore.Audio.AudioColumns.ARTIST, MediaStore.Audio.AudioColumns.ALBUM, MediaStore.Audio.AudioColumns.DURATION }, MediaStore.Audio.AudioColumns.IS_MUSIC + "=1", null, null); } @Provides Cursor provideAlbumCursor(