dagger-2

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

霸气de小男生 提交于 2019-11-29 16:23:54
问题 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

Can a Dagger 2 dependency be non-injectable?

霸气de小男生 提交于 2019-11-29 14:47:51
Is there a way to tell Dagger 2 how to provide something, but not allow it to be injected? Say I want to inject a Qux . A Qux requires a Foo and a Bar : @Provides @Singleton Foo foo() { return new Foo(); } @Provides @Singleton Bar bar() { return new Bar(); } @Provides @Singleton Qux qux(final Foo foo, final Bar bar) { return new Qux(foo, bar); } But what if I don't want Foo and Bar to be injectable? Perhaps exposing them would break the encapsulation of the Qux , or perhaps they're factories of some kind that I only want the Qux to have access to. I've thought of a couple ways I could achieve

ViewModelProviders with Dagger 2, not able to grasp the concept

拥有回忆 提交于 2019-11-29 11:07:47
I have a Retrofit Service like this public interface BrandsService { @GET("listBrand") Call<List<Brand>> getBrands(); } Then I have a Repository to get data from an api like this public class BrandsRepository { public static final String TAG = "BrandsRepository"; MutableLiveData<List<Brand>> mutableLiveData; Retrofit retrofit; @Inject public BrandsRepository(Retrofit retrofit) { this.retrofit = retrofit; } public LiveData<List<Brand>> getListOfBrands() { // Retrofit retrofit = ApiManager.getAdapter(); final BrandsService brandsService = retrofit.create(BrandsService.class); Log.d(TAG,

Why can't dagger process these kotlin generics?

限于喜欢 提交于 2019-11-29 10:37:23
I'm having some weird kotlin generic issues with Dagger that I kinda fixed but the solution isn't sound. Here's the dagger classes: @Module class P5Module { @Provides fun pool(): RecyclerView.RecycledViewPool = RecyclerView.RecycledViewPool() @Provides fun adapters(fusion: P5FusionAdapter, personas: P5ListAdapter, skills: P5SkillsAdapter, info: InfoAdapter) : List<Pageable> = listOf(fusion, personas, skills, info) } @ActivityScope @Subcomponent(modules = arrayOf(P5Module::class)) interface P5Component { fun adapter(): PageableAdapter } interface Pageable { fun manager(ctx: Context):

Dagger 2 error: dependency “cannot be provided without an @Inject constructor” while it actually annotated with @Inject

六月ゝ 毕业季﹏ 提交于 2019-11-29 10:34:07
问题 I've started using Dagger 2 and faced strange issue that looks like a bug to me. I have 3 modules, that are composed into one subcomponent, which in turn extends/pluses higher level component. Subcomponent is pretty simple: just combination of modules and a single injection point: @Singleton @Subcomponent( modules = { NavigationDrawerModule.class, NavigationListModule.class, SwitcherModule.class } ) public interface NavigationDrawerComponent { NavigationDrawerFragment inject

Cannot create an instance of custom ViewModel

℡╲_俬逩灬. 提交于 2019-11-29 09:18:19
I am using dagger2 library. whenever I am trying to run my project is says not able to create instance of view model class. main activity where I am trying to create an instance ((MovieApplication) getApplication()).getAppComponent().inject(this); mViewModel = ViewModelProviders.of(this).get(MoviesDataViewModel.class); My factory class public class ViewModelFactory implements ViewModelProvider.Factory { private MoviesDataViewModel mViewModel; @Inject public ViewModelFactory(MoviesDataViewModel viewModel) { this.mViewModel = viewModel; } @Override public <T extends ViewModel> T create(Class<T>

Kotlin and Dagger: Can I use @Inject to an object still make it nullable/optional?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 09:03:08
问题 I need something that can make my class nullable/optional because of the runtime errors that is happening on some devices. Is this possible? class MyFragment extends Fragment { @Inject var presenter: MyPresenter? = null // Other codes here... } I wanted to use the presenter as an option because SOME of the old Android devices especially are throwing this error (this is before I removed the lazyinit). Code: class MyFragment extends Fragment { @Inject lazyinit var presenter: MyPresenter? = null

How to migrate missing inject from module with complete = false from Dagger 1 to Dagger 2

断了今生、忘了曾经 提交于 2019-11-29 08:16:48
I have a library project/module that is used by both Android apps and regular java apps. In Dagger 1 this project/module has property complete = false . Within there is an @Inject field that is not satisfied by any class implementation or @Provides method. The idea is to force the "top" module(s) which has complete = true to provide system specific implementation Just for the sake of example: In the library project I have ActLogin activity that have field @Inject @Named("app version") mAppVersion . The value of this field is used when logging in into a server. ActLogin is used by several apps

Dagger: IllegalArgumentException: No injector factory bound for Class

两盒软妹~` 提交于 2019-11-29 05:24:27
I am new to Dagger 2. I have 2 Activities, I want to use injected ViewModel for both. Here is my ViewModuleFactory : @Singleton public class ProductViewModelFactory implements ViewModelProvider.Factory { private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators; @Inject public ProductViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) { this.creators = creators; } @SuppressWarnings("unchecked") @Override public <T extends ViewModel> T create(Class<T> modelClass) { Provider<? extends ViewModel> creator = creators.get(modelClass); if (creator == null

Dagger2 - How to conditionally choose modules at runtime

天大地大妈咪最大 提交于 2019-11-29 04:02:21
问题 I have a BIG Android app that needs to run different code for depending on the OS version, the manufacturer, and many other things. This app however needs to be a single APK. It needs to be smart enough at runtime to determine which code to use. Until now we have been using Guice but performance issues are causing us to consider migrating to Dagger. However, I've been unable to determine if we can achieve the same use case. The main goal is for us have some code that runs at startup to