DaggerAppComponent not created

2019-04-04 00:04发布

With dagger 2.10 I used to be able to create the app component by doing

    sAppComponent = DaggerAppComponent.builder()
            .appModule(new AppModule(this))
            .sessionModule(new SessionModule())
            .netModule(new NetModule())
            .dataModule(new DataModule())
            .build();

I was already using the AndroidInjector for Activities and everything was fine. Now I switched to 2.11 and I can't find the way to create the app component. In the google tutorial I see:

DaggerYourApplicationComponent.create()
    .inject(this);

to be added in the onCreate of the Application. In my case DaggerYourApplicationComponent = DaggerAppComponent. The problem is that DaggerAppComponent class isn't created anymore.

I have:

public class App extends android.support.multidex.MultiDexApplication implements HasActivityInjector {
    @Inject DispatchingAndroidInjector<Activity> mDispatchingActivityInjector;
    @Override
    public void onCreate() {
        super.onCreate();

        sAppComponent = DaggerAppComponent.create().inject(this); //here the error

and:

@Singleton
@Component(modules = {
        AppModule.class,
        MainActivityModule.class,
        ...
})
public interface AppComponent {
        void inject(App app);
        ...
}

in the build.gradle file I have:

def daggerVer = 2.11
compile "com.google.dagger:dagger:$daggerVer"
compile "com.google.dagger:dagger-android-support:$daggerVer"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVer"

2条回答
男人必须洒脱
2楼-- · 2019-04-04 00:28

Ok Sorry for the noise. My silly mistake: when I switched to 2.11 I copied the build.gradle section from the google tutorial where the dependency:

annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer"

isn't listed for a reason I don't know. With the dependencies listed below everything works:

def daggerVer = 2.12 // 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"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVer"

If you are using Kotlin

apply plugin: 'kotlin-kapt'

dependencies {
    def daggerVer = 2.12 // 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"
}
查看更多
唯我独甜
3楼-- · 2019-04-04 00:31

In Kotlin, we have to add kapt compiler plugin to use Dagger 2.

In your app gradle, add this plugin

apply plugin: 'kotlin-kapt'

And add dependencies as below

dependencies
{
    implementation "com.google.dagger:dagger:$latest_version"
    kapt  "com.google.dagger:dagger-compiler:$latest_version"
    implementation "com.google.dagger:dagger-android:$latest_version"
    kapt  "com.google.dagger:dagger-android-processor:$latest_version"
    implementation "com.google.dagger:dagger-android-support:$latest_version"
    kapt  "com.google.dagger:dagger-android-support:2.12"
}

See Kotlin Documentation

查看更多
登录 后发表回答