1

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.

mauryat
  • 1,610
  • 5
  • 29
  • 53
Paras Sidhu
  • 652
  • 1
  • 12
  • 36
  • Are you getting a compiler error due to this? – Arpit J. Sep 10 '19 at 03:31
  • @ArpitJ. Yes, it's a compiler error. – Paras Sidhu Sep 10 '19 at 04:25
  • @ParasSidhu it's possible that the editor window doesn't auto-update the errors after a successful build. Try the following options: 1) Close the file MyApplication.java and reopen, 2) Run Make Project two times (just to trigger editor update), 3) Search for file DaggerMyApplicationComponent in Studio. Hopefully that should solve it because I'm able to build the code from the tutorial. Even I had the problem you mention, but was able to overcome it by the above steps. If it still doesn't work, can you share the error you see in the "Build output window"? – mauryat Sep 11 '19 at 05:49
  • Another thing you can try is use builder() approach instead of create(). – mauryat Sep 11 '19 at 07:50
  • @mauryat It didn't work. I have updated the link to project in the description. You can have a look. – Paras Sidhu Sep 13 '19 at 10:45
  • @mauryat I have solved it and posted the solution. Please have a look. Surely upvotes if you can explain that behaviour. – Paras Sidhu Sep 13 '19 at 11:01

2 Answers2

1

As discussed in the comments, remove all unnecessary kotlin dependencies and plugins from gradle. If you don't have kotlin code in your project, it doesn't make sense to call your module a kotlin module. Dagger thinks it should generate kotlin code, but it doesn't have kotlin compiler to do it. Hence, either change the entire code to kotlin and use kotlin dependencies, or stick to java and remove all kotlin dependencies.

mauryat
  • 1,610
  • 5
  • 29
  • 53
-1

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!

Paras Sidhu
  • 652
  • 1
  • 12
  • 36
  • 1
    the problem seems to be stemming from your gradle files. I suggest removing everything that has the word kotlin or ktx in them. You don't need a gradle plugin for kotlin. You are also calling your app module as a kotlin module (by specifying thru apply plugin). You also don't need kotlin stdlib for java and androidx-kotlin. I removed all these from your project and was able to build successfully. :) – mauryat Sep 14 '19 at 06:26
  • @mauryat Yes, it's working afterwards. Wondering this shouldn't have happened at the first place when I wasn't even writing the code in a Kotlin file. – Paras Sidhu Sep 16 '19 at 15:49
  • you possibly unintentionally created a kotlin project while creating a new project. Or, if you are using a base project from some tutorial, it possibly had created a kotlin project or had those kotlin dependencies listed in gradle. – mauryat Sep 16 '19 at 15:56
  • Well, I did that intentionally and later on thought to begin the tutorial in Java only. But yeah got your point. Upvoted the answer. – Paras Sidhu Sep 16 '19 at 16:09