Elizabeth Elizabeth - 4 years ago 238
Android Question

Dagger 2: Error about subcomponents, but I don't have any subcomponents in my app

I'm trying to use Dagger 2 in my app. I keep getting this error:


Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
java.lang.IllegalArgumentException: @dagger.Module does not define an element subcomponents()


I don't use subcomponents at all in the app so I have no idea why this error is occurring. I have one module and one component. Module:

@Singleton
@Module

public class ApplicationModule {

private final WSTApplication application;

public ApplicationModule(WSTApplication application) {
this.application = application;
}

@Provides
public WSTApplication application() {
return this.application;
}

@Provides
public Context applicationContext() {
return this.application;
}

@Provides
Realm provideRealm() {
return Realm.getDefaultInstance();
}

@Provides
RealmHelper providesRealmHelper(final Realm realm) {
return new RealmHelper(realm);
}

@Provides
@Singleton
public WorkoutPresenter providesWorkoutPresenter(final RealmHelper helper) {
return new WorkoutPresenter(helper);
}


}

And my component:

@Singleton
@Component(modules={ApplicationModule.class})
public interface ApplicationComponent {

void inject (MainActivity activity);

WSTApplication application();

Context applicationContext();

Realm provideRealm();

RealmHelper providesRealmHelper(Realm realm);

WorkoutPresenter providesWorkoutPresenter(RealmHelper helper);

}


And here is the onCreate from my application:

@Override
public void onCreate() {
super.onCreate();

component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}


I can't get DaggerApplicationComponent to stop being red, either, but I assume this is because the project isn't actually building because of this weird subcomponent error? I've tried using the underscore (Dagger_ApplicationComponent) but that doesn't help.

I tried Google but all I found was guides on how to use subcomponents in Dagger, which is not what I want. I don't want to use subcomponents. I just want to use the one component.

Also, just in case this matters, here is what I put in my build.gradle files:

In the project buildscript:

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'


And in the app level build.gradle:

apply plugin: 'com.neenbedankt.android-apt'


and then down in the dependencies:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
compile 'com.android.support:design:24.2.1'
compile 'com.google.dagger:dagger:2.1'
compile 'com.android.support:support-v4:24.2.1'
testCompile 'junit:junit:4.12'
apt 'com.google.dagger:dagger-compiler:2.7'
provided 'org.glassfish:javax.annotation:10.0-b28'
}


Thanks to anyone who can help me! I'm new to Dagger2 and even after reading several beginner guides, I still don't entirely get it (I was hoping that trying to use it would make things clearer...so far it's clear as an oil spill). Sorry in advance in the highly likely event that I'm making a stupid beginner mistake.

Answer Source

Why do you use com.neenbedankt.android-apt? As far as I know it is obsolete now.

The Dagger GitHub page explains how to use it within an Android project.

dependencies {
    compile 'com.google.dagger:dagger:2.x'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.x'
}

Here you can find Dagger releases for the dependencies versions.

I'm not sure if it solves your problem right away but you should definitely give it a try.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download