我試圖在我的應用程序中使用Dagger 2。我一直得到這個錯誤:Dagger 2:有關子組件的錯誤,但我的應用程序中沒有任何子組件
Error:Execution failed for task ':app:compileDebugJavaWithJavac'. java.lang.IllegalArgumentException: @dagger.Module does not define an element subcomponents()
我在應用程序中根本不使用子組件,所以我不知道爲什麼會發生此錯誤。我有一個模塊和一個組件。模塊:
@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);
}
}
而且我的組件:
@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);
}
這裏是從我的應用程序中的onCreate:
@Override
public void onCreate() {
super.onCreate();
component = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
}
我不能讓DaggerApplicationComponent要停止被紅色,但是我認爲這是因爲這個項目實際上並沒有因爲這個奇怪的子而建造組件錯誤?我試過使用下劃線(Dagger_ApplicationComponent),但沒有幫助。
我試過Google,但是我發現的所有內容都是關於如何在Dagger中使用子組件的指南,這不是我想要的。我不想使用子組件。我只想使用這個組件。
而且,萬一這個重要的,這裏是我把我的build.gradle文件:
在項目buildscript:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
而在應用級的build.gradle:
apply plugin: 'com.neenbedankt.android-apt'
,然後倒在依賴關係:
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'
}
感謝任何能幫助我的人!我是Dagger2的新手,甚至在閱讀過幾本初學者指南之後,我仍然沒有完全明白它(我希望試圖使用它會讓事情變得更加清晰......迄今爲止,它已經成爲一次漏油事件)。很抱歉,我正在犯一個愚蠢的初學者錯誤的可能性很大的事件。
你可以嘗試從模塊中刪除'@ Singleton'註釋嗎? – benji
我嘗試過並嘗試再次構建。同樣的錯誤。 :(謝謝你的建議! – Elizabeth