dagger

Using Dagger for dependency injection on constructors

夙愿已清 提交于 2019-12-20 08:59:26
问题 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,

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

一曲冷凌霜 提交于 2019-12-20 02:45:20
问题 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 },

Dagger - class has no injectable members

南笙酒味 提交于 2019-12-19 05:46:12
问题 I'm trying to integrate Dagger into my application. And I ran into a problem. I'm getting this error at runtime: java.lang.IllegalStateException: Errors creating object graph: com.app.NavigationController has no injectable members. Do you want to add an injectable constructor? required by class com.app.fragments.LoginFragment I tried looking into other, similar answers, but nothing helped me so far... Everything related to graph construction is implemented pretty much like in Android Activity

Dagger code giving NoClassDefFoundError when Espresso Tests are run, normal run works fine

别说谁变了你拦得住时间么 提交于 2019-12-19 05:16:07
问题 Started exploring Espresso 2.0, but seem to have run into a hiccup. I cannot get the tests to successfully run against any project which includes Dagger. When I run the tests I get the following Exception (entire stacktrace at the end): java.lang.NoClassDefFoundError: com/pdt/daggerexample/model/DaggerExampleAppModule$$ModuleAdapter$ProvideMySingletonProvidesAdapter The application runs when not running from the AndroidInstrumentationTest. Here are some of the relevant files, I've also

Module depending on another module in Dagger

倾然丶 夕夏残阳落幕 提交于 2019-12-18 15:57:53
问题 I'm trying to use Dagger to do Dependency Injection on an app that I'm building, and running into trouble constructing proper DAGs when I have one package's Module depending on values provided by the Injector (presumably provided by another Module). If I have a simple module for some configurable variables (that I might want to swap out for testing environments, for example) @Module( injects = DependentModule.class, ) public class ConfigModule { @Provides @Named("ConfigOption") String

Dagger 2 examples [closed]

余生长醉 提交于 2019-12-18 10:13:11
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 4 years ago . Dagger 2 is around the corner but the available examples wouldn't even compile right off the box, and the documentation is a copy-paste-replace from Dagger 1. Does anyone have an example of a proper application working on Google's Dagger 2? 回答1: I've just published sample app based on Gradle which integrates

Cannot create an instance of custom ViewModel

孤人 提交于 2019-12-18 05:42:17
问题 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

Singletons in Dagger 1.x

戏子无情 提交于 2019-12-18 02:14:12
问题 When using Dagger, I found that I'm getting multiple instances of a singleton when I inject it wherever I need it. I've annotated the class and the provides method with @Singleton . Can anyone think of why this is happening? Edit: If it helps, I have followed the same structure for my app as the sample application in Dagger's GitHub (https://github.com/square/dagger/tree/master/examples/android-activity-graphs). I'm trying to get the Singleton in the base activity and a couple of third party

How do I use AndroidInjection class in custom views or other android classes?

与世无争的帅哥 提交于 2019-12-17 23:30:21
问题 My issue with the Android-specific pattern is, if you use their AndroidInjection class, there is no way to members inject other objects besides Activities / Fragments /custom views/adapters, except with the Application Component. This is because you cannot get a reference the the Subcomponent ( AndroidInjector ) used to inject Activities / Fragments . This makes injecting Dialogs (if you use DialogFragments ). The AndroidInjection class seems to support just the core Android types. 回答1: What

What are the specific benefits of using DI on Android?

女生的网名这么多〃 提交于 2019-12-17 18:17:09
问题 What are the specific benefits or advantages of using a dependency injection framework for Android, like Dagger, Transfuse or RoboGuice? For example, what kind of apps would benefit the most from using DI? is there more of a performance advantage, or is it more on the ease of extending an app, or even more about making it testable? One of the reasons for asking this is to gauge if an app I'm developing would actually benefit from it or not much. Since I intend the app to be serious at some