Android-Dagger2 - 无法将应用程序组件的依赖项获取到 Activity 范围中

标签 android dagger-2 dagger

我正在使用 Dagger 2 进行依赖项注入(inject),如果注入(inject)在同一范围内,则一切正常,例如:如果 ApplicationComponent 在应用程序范围中注入(inject)实例,或者 ActivityComponent 在 Activity 中注入(inject)某个实例范围,一切都好。当我尝试使 ActivityComponent 依赖于 ApplicationComponent 并尝试从那里获取依赖项时,问题就会发生,应用程序根本不会构建,并且出现 3 个编译器错误 1. 找不到符号类DaggerActivityComponent 2. 找不到符号类DaggerApplicationCom 3. 无法访问Nullable。 我已经到处搜索过这个问题,但似乎没有解决问题。

这是应用程序组件:

@ApplicationScope
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(MyApplication application);
}

这是 ActivityComponent:

@ActivityScope
@Component(modules = {ActivityModule.class}, dependencies =   ApplicationComponent.class)
public interface ActivityComponent {

void inject(MainActivity activity);
}

这是应用程序模块:

@Module

公共(public)类应用程序模块{

@Provides
public Retrofit retrofit(GsonConverterFactory gsonConverterFactory) {
    return new Retrofit.Builder()
            .baseUrl(BuildConfig.BASE_URL)
            .addConverterFactory(gsonConverterFactory)
            .build();
}

@Provides
public Gson gson() {
    GsonBuilder gsonBuilder = new GsonBuilder();
    return gsonBuilder.create();
}


@Provides
public GsonConverterFactory gsonConverterFactory(Gson gson) {
    return GsonConverterFactory.create(gson);
}
}

这是 ActivityModule

@Module
public class ActivityModule {

}

这是应用程序类

public class MyApplication extends Application{

ApplicationComponent component;

@Inject
Retrofit retrofit;

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

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

public  ApplicationComponent component() {
    return component;
}
}

最后是 Activity :

public class MyApplication extends Application{

ApplicationComponent component;

@Inject
Retrofit retrofit;

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

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

public  ApplicationComponent component() {
    return component;
}
}

请帮忙,谢谢。

最佳答案

我也遇到了同样的问题,你需要做的就是在 ApplicationComponent 接口(interface)中为需要由 ApplicationComponent 注入(inject)的依赖项添加一个 getter 方法。

例如,如果您需要 Retrofit,例如您的情况,您通常会在 ApplicationModule 中定义 @Provides 方法并添加

Retrofit getRetrofit();

在应用程序组件中。

希望有帮助

关于Android-Dagger2 - 无法将应用程序组件的依赖项获取到 Activity 范围中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50241165/

相关文章:

android - 在 sleep 模式下做某事

android - 无法在 attachBaseContext() 中使用 Dagger 注入(inject)对象来更新语言环境

android - 通过 eclipse 构建的 Dagger 示例失败并显示 'Please ensure that code generation was run for this module.'

android - 如何使用 Dagger 2 将上下文注入(inject) Presenter

ToggleButton 内的 Android 文本不会居中

java - onCreateLoader 和 onLoadFinished 不同步

android - 展开 TableLayout 的最后一行

java - ViewModel 没有零参数构造函数错误,原因是 ViewModelFactory 一直为 null(不是 100% 确定)

Android Dagger 2 编译错误

android - Dagger 2 : Is it possible to inject based on Android Version?