dagger-2

Dagger 2 - injecting non Android classes

♀尐吖头ヾ 提交于 2019-11-30 23:20:02
I'm implimenting Dagger 2 in my Android app. I have it setup in the following way: AppComponent.java @Singleton @Component(modules = { AndroidInjectionModule.class, AndroidSupportInjectionModule.class, ActivityBuilder.class, AppModule.class, DataBaseDaoModule.class }) public interface AppComponent { @Component.Builder interface Builder { @BindsInstance Builder application(Application aApplication); AppComponent build(); } Application application(); void inject(MyApplication aApplication); } AppInjector.java ublic class AppInjector { public static void init(MyApplication aApplication) { /

Understanding scopes in Dagger 2

独自空忆成欢 提交于 2019-11-30 20:45:24
I have an scope-related error in Dagger 2 and I'm trying to understand how I can solve it. I have a CompaniesActivity that shows companies. When the user selects an item, selected company's employees are shown in EmployeesActivity . When the user selects an employee, her detail is shown in EmployeeDetailActivity . class Company { List<Employee> employees; } Class CompaniesViewModel contains the companies and the selected one (or null ): class CompaniesViewModel { List<Company> companies; Company selected; } CompaniesActivity has a reference to CompaniesViewModel : class CompaniesActivity

Dagger2 component with more than one dependencies

匆匆过客 提交于 2019-11-30 17:55:32
This is what I currently have and it works: @FragmentScope @Component(dependencies = {FacebookComponent.class}, modules = {FragmentFacebookLoginModule.class}) public interface FragmentFacebookLoginComponent { void inject(FragmentFacebookLogin fragment); } Now I want to add another dependency. I changed it to this: @Component(dependencies = {FacebookComponent.class, AnotherComponent.class}, modules = {FragmentFacebookLoginModule.class}) But now I get this error message: FragmentFacebookLoginComponent depends on more than one scoped component How can I solve this? How can I have more than one

Context cannot be provided without an @Provides-annotated method, but it is?

爷,独闯天下 提交于 2019-11-30 17:48:40
I have the following simple module: @Module public class ApplicationModule { private CustomApplication customApplication; public ApplicationModule(CustomApplication customApplication) { this.customApplication = customApplication; } @Provides @Singleton CustomApplication provideCustomApplication() { return this.customApplication; } @Provides @Singleton @ForApplication Context provideApplicationContext() { return this.customApplication; } } And the respective simple component: @Singleton @Component( modules = ApplicationModule.class ) public interface ApplicationComponent { CustomApplication

Using Dagger 2 to inject into service

≡放荡痞女 提交于 2019-11-30 11:22:47
问题 I have an app which is basically a service that runs all the time and alarms the user when something happens. When the service creates the alarm, it needs to give it his context so that the alarm can do callbacks to the service when something happens. For example: public MyService extends Service{ private SomeAlarm alarm; @Override public void onCreate() { super.onCreate(); alarm = new SomeAlarm(MyService.this); } } How can I inject the SomeAlarm class into the service, and give the SomeAlarm

Dagger 2 and android Studio: working but can't see generated classes

梦想的初衷 提交于 2019-11-30 10:57:55
I'm trying to use Dagger 2 in an Android Studio Project. I've used the CoffeeMaker example. I've managed to make the app build and working however: - I don't success in seeing the generated code. - If I debug, I can't see it neither. - Moreover DaggerCoffeeApp_Coffee as marked as reed (Cannot resolve symbol) My gradle files are: // 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:1.1.3' // NOTE: Do not place your application dependencies here; they

Dagger 2 Injecting Constructors

主宰稳场 提交于 2019-11-30 10:25:29
问题 I'm starting to use Dagger 2 in an application I'm developing but I have some questions about how Dagger 2 works. I get the all the logic behind the @Provides methods and the @Inject annotation for initialising your dependencies, but the @Inject annotation to class constructors kind of bugs my mind. For example: Im my app, I have one module defined, the ContextModule, to retrieve the context of my application: ContextModule.java @Module public class ContextModule { private final Context

Dagger2 generated class is all of a sudden missing from Android Studio

。_饼干妹妹 提交于 2019-11-30 06:43:56
I have been using Dagger2 in Android Studio for months now but today all of sudden it stopped working and is giving me the following error error: cannot find symbol return Dagger_Injector.builder() ^ symbol: variable Dagger_Injector location: class Initializer Note: Some input files use or override a deprecated API. Note: Recompile with -Xlint:deprecation for details. Note: Some input files use unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error Here is the code where it is failing @Singleton @Component(modules = {TestService.class, ServiceFactory.class})

Dagger 2.11 - A binding with matching key exists in component

China☆狼群 提交于 2019-11-30 05:45:31
问题 I have a small scenario where I have this following structure where I'm trying to inject fragment manager in baseActivity Fragment but for some reason I'm running out of luck :( @Singleton @Component(modules = { AppModule.class, ActivityModule.class, AndroidSupportInjectionModule.class }) public interface AppComponent extends AndroidInjector<App> { @Override void inject(App application); @Component.Builder interface Builder { @BindsInstance AppComponent.Builder application(App application);

Dagger2 + Kotlin: lateinit property has not been initialized

北战南征 提交于 2019-11-30 01:51:53
问题 I'm trying to inject the ViewModelFactory into my Activity, but it keeps throwing this same error: lateinit property viewModelFactory has not been initialized. I can't find what I may be doing wrong. See the code above from my classes AppComponent.kt @Component(modules = [(AppModule::class), (NetworkModule::class), (MainModule::class)]) interface AppComponent { fun inject(application: TweetSentimentsApplication) fun inject(mainActivity: MainActivity) fun context(): Context fun retrofit():