Update: Link to project -> https://drive.google.com/open?id=1D8mYZL3Pb8FezPp5FOKA20-Um5kTRYxH
I had been starting with Dagger and following this tutorial till step 5.4: https://www.vogella.com/tutorials/Dagger/article.html#special-treatment-of-fields-in-dagger
I have added the following dependencies:
api 'com.google.dagger:dagger:2.20'
api 'com.google.dagger:dagger-android:2.20'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.20'
annotationProcessor 'com.google.dagger:dagger-compiler:2.20'
compileOnly 'javax.annotation:jsr250-api:1.0'
But IDE can't resolve "DaggerMyApplicationComponent" (in onCreate()). Seems like Dagger can't generate the code.
MyApplication.java
import android.app.Activity;
import android.app.Application;
import javax.inject.Inject;
import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;
public class MyApplication extends Application implements HasActivityInjector {
@Inject
DispatchingAndroidInjector<Activity> dispatchingAndroidInjector;
@Override
public void onCreate() {
super.onCreate();
DaggerMyApplicationComponent.create().inject(this);
}
@Override
public DispatchingAndroidInjector<Activity> activityInjector() {
return dispatchingAndroidInjector;
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parassidhu.daggerkumar">
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
MyApplicationModule.java:
import dagger.Module;
import dagger.android.ContributesAndroidInjector;
@Module
public abstract class MyApplicationModule {
@ContributesAndroidInjector
abstract MainActivity contributeActivityInjector();
}
MyApplicationComponent.java:
import javax.inject.Singleton;
import dagger.Component;
import dagger.android.AndroidInjectionModule;
import dagger.android.AndroidInjector;
@Singleton
@Component(modules = {AndroidInjectionModule.class, MyApplicationModule.class})
public interface MyApplicationComponent extends AndroidInjector<MyApplication> {
}
Please suggest what could be wrong. I have tried Make Project, Clean, Rebuild, Invalidate and Restart several times but no help.
I have solved this problem. The solution is very weird. I changed the dependencies to:
api 'com.google.dagger:dagger:2.20'
api 'com.google.dagger:dagger-android:2.20'
kapt 'com.google.dagger:dagger-android-processor:2.20'
kapt 'com.google.dagger:dagger-compiler:2.20'
Overall, I used kapt
instead of annotationProcessor
and then Clean Project and Make Project and boom! It worked.
Now comes the weird part. There's not a single Kotlin file in my project (except a default instrumented test) although Kotlin is configured in the project. It would be very much appreciated if anyone can fall light on it.
Hope it helps!