-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Dagger2Integration
Dagger 2
and AndroidAnnotations
can easily work together, combined usage of both frameworks is described here. This guide uses Dagger 2
; for Dagger 1
, see this guide. The article assumes that the reader is familiar with Dagger 2
usage of normal Android projects. If not, the Dagger documentation and example projects should be read first.
Injecting dependencies into AA annotated components does not require any additional step than normal Dagger 2
usage. You have to annotate the components with AA annotations as usual, and you have to declare the annotated class (not the generated class) in your @Component
s.
@EActivity
public class DaggerActivity extends Activity {
@AfterInject
void onInjectDependencies() {
((MyApplication) getApplication()).component().inject(this);
// dependency is now available
}
@Inject
DaggerDependency dependency;
}
public class DemoApplication extends Application {
@Singleton
@Component(modules = AndroidModule.class)
public interface ApplicationComponent {
void inject(DaggerActivity daggerActivity);
}
private ApplicationComponent component;
@Override public void onCreate() {
super.onCreate();
component = DaggerMyApplication_ApplicationComponent.builder()
.androidModule(new AndroidModule(this))
.build();
}
public ApplicationComponent component() {
return component;
}
}
If you want to use Dagger
to inject an instance of an AA enhanced class, like @EBean, you have to add a @Provides
method in your module. In that method, you have to create an instance of the generated class, by calling the generated methods manually.
@EBean
public class SomeDependency {
// ...
}
@Module
public class DaggerModule {
@Provides
SomeDependency provideSomeDependency(Context context) {
return SomeDependency_.getInstance_(context);
}
}
You have to also provide an Android Context
object, as usual in Dagger
Android projects. After this is done, you can inject the @EBean
with Dagger
to another class:
public class DaggerClass {
@Inject
SomeDependency someDependency; // will have an instance of SomeDependency_
}
19/11/2020 The 4.8.0 release is out !
- Get started!
- Cookbook, full of recipes
- Customize annotation processing
- List of all available annotations
- Release Notes
- Examples
- Read the FAQ
- Join the Mailing list
- Create an issue
- Tag on Stack Overflow
- Ask on Gitter