Dagger on default constructors

From the docs: Classes that lack @Inject annotations cannot be constructed by Dagger. Dagger actively requires you to add @Inject to your injectable class, either by adding a no-args constructor, or adding an injectable field. The third option is to return the class from an @Provides method like so: @Module(…) class MyModule { @Provides Foo … Read more

Can I extend a custom Application in Espresso?

With a custom instrumentation runner, you can override newApplication and have it instantiate something other than the default application from the manifest. public class MyRunner extends AndroidJUnitRunner { @Override public Application newApplication(ClassLoader cl, String className, Context context) throws Exception { return super.newApplication(cl, MyCustomEspressoApplication.class.getName(), context); } } Be sure to update testInstrumentationRunner with your custom runner’s … Read more

Dagger + Retrofit. Adding auth headers at runtime

I personally created an okhttp3.Interceptor that does that for me, which I update once I have the required token. It looks something like: @Singleton public class MyServiceInterceptor implements Interceptor { private String sessionToken; @Inject public MyServiceInterceptor() { } public void setSessionToken(String sessionToken) { this.sessionToken = sessionToken; } @Override public Response intercept(Chain chain) throws IOException { … Read more

How to inject into a BroadcastReceiver

Dagger 2 example for injecting objects into a BroadcastReceiver. The BroadcastReceiverModule.kt @Module abstract class BroadcastReceiverModule { @ContributesAndroidInjector abstract fun contributesMyTestReceiver() : MyTestReceiver } The AppComponent.kt @Singleton @Component( modules = [ (AndroidSupportInjectionModule::class), (BroadcastReceiverModule::class) ]) interface AppComponent : AndroidInjector<MyApp> { @Component.Builder abstract class Builder : AndroidInjector.Builder<MyApp>() } The Application class class MyApp : DaggerApplication() { override fun … Read more

DaggerAppComponent not created

With the dependencies listed below everything works: If you are using Kotlin apply plugin: ‘kotlin-kapt’ dependencies { def daggerVer = 2.27 // or latest version implementation “com.google.dagger:dagger:$daggerVer” implementation “com.google.dagger:dagger-android-support:$daggerVer” kapt “com.google.dagger:dagger-android-processor:$daggerVer” kapt “com.google.dagger:dagger-compiler:$daggerVer” } If you are using Java: dependencies { def daggerVer = 2.27 // or latest version implementation “com.google.dagger:dagger:$daggerVer” implementation “com.google.dagger:dagger-android-support:$daggerVer” annotationProcessor “com.google.dagger:dagger-android-processor:$daggerVer” … Read more

Dagger injection not working for “object” in Kotlin

If you look into kotlin bytecode you’ll find that the code you’ve written is translated into following: public final class SomeSingleton { public static LProperty; property // <- Notice static field here public final getProperty()LProperty … public final setProperty(LProperty)V … } As you can see the actual field is static which makes it uneligible for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)