我正在尝试编译https://github.com/svenjacobs/android-dagger2-example,但是遇到了与作用域组件有关的未作用域化组件依赖的错误。(Android Studio 1.1, Gradle 2.2.1)。此外,如果有人知道其他带有片段的Dagger2 Android示例,我想了解。
更新:这里还有另一个例子,一个非常基本的带有片段的示例: https://github.com/gk5885/dagger-android-sample
/Users/Mac1/android-dagger2-example-master/app/src/main/java/com/svenjacobs/dagger2/ActivityComponent.java
Error:(15, 1) error: com.svenjacobs.dagger2.ActivityComponent (unscoped) cannot depend on scoped components:
@Singleton com.svenjacobs.dagger2.ApplicationComponent
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
这里是文件 ActivityComponent,显然它没有范围:
import dagger.Component;
/**
* Component for all activities.
*
* @author Sven Jacobs
*/
@Component(dependencies = ApplicationComponent.class,
modules = {
MainActivityModule.class,
AModule.class,
BModule.class
})
interface ActivityComponent extends AFragment.Injector, BFragment.Injector {
void inject(MainActivity activity);
void inject(AnotherActivity activity);
}
这里是具有作用域的组件:
package com.svenjacobs.dagger2;
import javax.inject.Singleton;
import dagger.Component;
/**
* Application-wide dependencies.
*
* @author Sven Jacobs
*/
@Singleton
@Component(modules = ApplicationModule.class)
interface ApplicationComponent {
void inject(Dagger2Application application);
/**
* Provides dependency for sub-components
*/
SomeApplicationDependency someApplicationDependency();
}