dagger-2

Dagger 2: Provide same instance between multiple Component with same Scope on different library modules

和自甴很熟 提交于 2019-11-29 03:08:21
I have a Core Android Library where I'm defining a CoreComponent ad using the @Singleton scope to inject instances of classes provided by a CoreModule. @Singleton @Component(modules = {CoreModule.class}) public interface CoreComponent { void inject(SomeClass target); } @Module public class CoreModule { @Singleton @Provides CoreRepository provideCoreRepository() { return new CoreRepositoryImpl(); } } I would like to access the same @Singleton instances from another Android Library that is depending on the Core Library and is using another component. @Singleton @FooScope @Component(modules =

Unresolved reference DaggerApplicationComponent

余生长醉 提交于 2019-11-28 22:33:56
I'm trying to create my app component, but Dagger does not generate my app component. here is MyApplication class class MyApplication : Application() { companion object { @JvmStatic lateinit var graph: ApplicationComponent } @Inject lateinit var locationManager : LocationManager override fun onCreate() { super.onCreate() graph = DaggerApplicationComponent.builder().appModule(AppModule(this)).build() graph.inject(this) } } and here is my AppComponent class @Singleton @Component(modules = arrayOf(AppModule::class)) interface ApplicationComponent { fun inject(application: MyApplication) } here is

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

女生的网名这么多〃 提交于 2019-11-28 20:29:43
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. What follows is not an answer to your question, but an explanation why you shouldn't be asking this question at

How do I configure IntelliJ/gradle to use dagger 2.0

℡╲_俬逩灬. 提交于 2019-11-28 20:21:46
I have a gradle project and I want to use dagger 2.0 in it. I don't know how to configure IntelliJ and gradle to generate files and let IntelliJ find them? My build.gradle file looks like: apply plugin: 'java' apply plugin: 'idea' version = '1.0' repositories { mavenCentral() maven { url "https://oss.sonatype.org/content/repositories/snapshots" } } dependencies { compile 'org.slf4j:slf4j-api:1.7.12' compile 'org.slf4j:slf4j-simple:1.7.12' compile 'commons-configuration:commons-configuration:1.10' compile 'commons-collections:commons-collections:3.2.1' compile 'com.google.dagger:dagger:2.0'

Problems with singletons when using component dependencies

本秂侑毒 提交于 2019-11-28 19:31:44
问题 I'm having a problem in understanding why the following code doesn't work. I have following project structure: @Component(modules = CCModule.class) public interface CComponent { XXX getXXX(); } where @Module public class CCModule { @Provides @Singleton public XXX provide XXX(){ return new XXX(); } } and @Component(dependencies = CComponent.class, modules = AAModule.class) public interface AComponent { YYY getYYY(); } where class YYY { @Inject public YYY(XXX xxx) { ... } } I initialize

Method injection using Dagger 2

岁酱吖の 提交于 2019-11-28 19:09:56
I haven't managed to find a good explanation/example on method injection using Dagger 2. Could someone please help me understand? Example: @Inject public Dinner makeDinner(Pasta pasta, Sauce sauce) { mPan.add(pasta); mPan.add(sauce); return mPan.cookDinner(); } So if I annotate my method with @Inject , am I correct to assume that the arguments in the method signature will be injected with defined objects from the object graph? How can I use this method in my code then? It will still expect me to supply all the arguments, when I make the method call, which sort of defeats the purpose. UPDATE:

RuntimeException with Dagger 2 on Android 7.0 and Samsung devices

北战南征 提交于 2019-11-28 17:51:31
On my Google Play console I see quite a lot crash reports since I started to use Dagger 2, but only on Android 7.0 and mainly on Samsung devices, some Huawai and Motorola devices and some rare Xperia devices: java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2984) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3045) at android.app.ActivityThread.-wrap14 (ActivityThread.java) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1642) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os

Android Unit Tests with Dagger 2

泪湿孤枕 提交于 2019-11-28 17:10:38
I have an Android app that uses Dagger 2 for dependency injection. I am also using the latest gradle build tools that allow a build variant for unit testing and one for instrumentation tests. I am using java.util.Random in my app, and I want to mock this for testing. The classes I'm testing don't use any Android stuff, so they're just regular java classes. In my main code I define a Component in a class that extends the Application class, but in the unit tests I'm not using an Application . I tried defining a test Module and Component , but Dagger won't generate the Component . I have also

Dagger + Retrofit. Adding auth headers at runtime

拈花ヽ惹草 提交于 2019-11-28 17:07:04
问题 I'm wondering if there is a way for Dagger to know that it should recreate an object when new data is available. The instance I am speaking of is with the request headers I have for retrofit. At some point (when the user logs in) I get a token that I need to add to the headers of retrofit to make authenticated requests. The issue is, I'm left with the same unauthenticated version of retrofit. Here's my injection code: @Provides @Singleton OkHttpClient provideOkHttpClient(Cache cache) {

Dagger 2 lifecycle of a component, module and scope

丶灬走出姿态 提交于 2019-11-28 16:26:54
问题 I've read a lot of posts and tutorials about dagger 2: http://frogermcs.github.io/dependency-injection-with-dagger-2-custom-scopes/ https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2 http://fernandocejas.com/2015/04/11/tasting-dagger-2-on-android/ https://github.com/konmik/konmik.github.io/wiki/Snorkeling-with-Dagger-2 What determines the lifecycle of a component (object graph) in Dagger 2? etc. But I am still confused about the lifecycle of a component, and