gpt4 book ai didi

android - @Component.Builder 缺少所需模块或组件的 setter [Dagger2]

转载 作者:行者123 更新时间:2023-11-29 14:59:33 24 4
gpt4 key购买 nike

我正在尝试使用 Component.Builder 来注入(inject)我的模块,但是 RestModule 有问题 - 我的模块之一。它返回

Error:(41, 5) error: @Component.Builder is missing setters for required modules or components: [com.injection.module.services.RestModule]

这是ApplicationComponent:

@Singleton
@Component(modules = {PresenterEnvironmentModule.class,
RestModule.class})
public interface PresenterEnvironmentComponent {


@Component.Builder
interface Builder {

@BindsInstance
Builder sharedPreferenceService(SharedPrefService sharedPreferences);

@BindsInstance
Builder loggingService(LoggingService loggingService);

@BindsInstance
Builder applicationService(Application application);

@BindsInstance
Builder connectivityService(ConnectivityService connectivityService);

@BindsInstance
Builder schedulersService(SchedulersService schedulersService);

@BindsInstance
Builder sessionService(SessionService sessionService);



PresenterEnvironmentComponent build();

}

@NonNull
PresenterEnvironment presenterEnvironment();

void inject(BaseBootstrapActivity<BaseBootstrapPresenter> baseBootstrapActivity);
}

这是 RestModule:

 @Module
public class RestModule {
private final boolean debuggable;
private final String baseUrl;
@NonNull
private final TokenInterceptor tokenInterceptor;
@NonNull
private final BootstrapRestService bootstrapRestService;
@Nullable
private Interceptor[] additionalInterceptors;
@Nullable
private Function<OkHttpClient.Builder, OkHttpClient.Builder> onOkHttpClient;
@Nullable
private Function<OkHttpClient.Builder, OkHttpClient.Builder> onOkHttpClientForUploading;

public RestModule(@NonNull RestSettings restSettings) {
this.debuggable = restSettings.isDebuggable();
this.baseUrl = restSettings.getBaseUrl();
this.tokenInterceptor = restSettings.getTokenInterceptor();
this.bootstrapRestService = restSettings.getRestService();
this.onOkHttpClient = restSettings.getOnOkHttpClient();
this.onOkHttpClientForUploading = restSettings.getOnOkHttpClientForUploading();
this.additionalInterceptors = restSettings.getAdditionalInterceptors();
}

@Provides
@Singleton
@NonNull
@Type.Basic
public OkHttpClient provideOkHttpClient(@NonNull TokenInterceptor tokenInterceptor,
@NonNull HttpLoggingInterceptor loggingInterceptor,
@NonNull OkLogInterceptor okLogInterceptor,
@NonNull CachingInterceptor cacheInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(tokenInterceptor);
if (additionalInterceptors != null && additionalInterceptors.length > 0) {
for (Interceptor interceptor : additionalInterceptors) {
builder.addInterceptor(interceptor);
}
}
if (onOkHttpClient != null) {
builder = apply(onOkHttpClient, builder);
}
if (debuggable) {
// Add HttpLoggingInterceptor
builder.addInterceptor(loggingInterceptor);
// add cache interceptor
builder.addNetworkInterceptor(cacheInterceptor);
// add okLog interceptor
builder.addInterceptor(okLogInterceptor);
}
return builder.build();
}

@Provides
@Singleton
@NonNull
@Type.ForUploading
public OkHttpClient provideOkHttpClientForUploading(@NonNull TokenInterceptor tokenInterceptor,
@NonNull HttpLoggingInterceptor loggingInterceptor,
@NonNull OkLogInterceptor okLogInterceptor,
@NonNull CachingInterceptor cacheInterceptor) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(tokenInterceptor);

if (additionalInterceptors != null && additionalInterceptors.length > 0) {
for (Interceptor interceptor : additionalInterceptors) {
builder.addInterceptor(interceptor);
}
}

if (onOkHttpClientForUploading == null) {
builder.writeTimeout(60, TimeUnit.SECONDS);
builder.connectTimeout(60, TimeUnit.SECONDS);
builder.readTimeout(60, TimeUnit.SECONDS);
} else {
builder = apply(onOkHttpClientForUploading, builder);
}
if (debuggable) {
// Add HttpLoggingInterceptor
builder.addInterceptor(loggingInterceptor);
// add cache interceptor
builder.addNetworkInterceptor(cacheInterceptor);
// add okLog interceptor
builder.addInterceptor(okLogInterceptor);
}
return builder.build();
}

@Provides
@Singleton
@NonNull
public OkLogInterceptor provideOkLogInterceptor() {
return new OkLogInterceptor.Builder().withAllLogData().build();
}

@Provides
@Singleton
@NonNull
public HttpLoggingInterceptor provideHttpLoggingInterceptor() {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
return interceptor;
}

@Provides
@Singleton
@NonNull
@Type.Basic
public Retrofit provideRetrofit(@NonNull @Type.Basic OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(LoganSquareConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}

@Provides
@Singleton
@NonNull
@Type.ForUploading
public Retrofit provideRetrofitForUploading(@NonNull @Type.ForUploading OkHttpClient client) {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(LoganSquareConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
}

@Provides
@Singleton
@NonNull
public CachingInterceptor provideCachingInterceptor(LoggingService loggingService) {
return new CachingInterceptor(loggingService);
}

@Provides
@Singleton
@NonNull
public TokenInterceptor provideTokenInterceptor() {
return tokenInterceptor;
}

@Provides
@Singleton
@NonNull
public BootstrapRestService provideBaseRestService(@NonNull @Type.Basic Retrofit retrofit,
@NonNull @Type.ForUploading Retrofit retrofitForUploading) {
return bootstrapRestService.setup(retrofit, retrofitForUploading);
}


@NonNull
private <T, R> R apply(@NonNull Function<T, R> f, @NonNull T t) {
try {
return Preconditions.checkNotNull(f.apply(t));
} catch (Throwable ex) {
throw new IllegalStateException("Applying function for " + t.toString() + " Threw " + ex);
}
}



}

最佳答案

@Module
public class RestModule {
// ...
public RestModule(@NonNull RestSettings restSettings) {

RestModule 是一个可实例化的模块,没有可见的无参数构造函数。 Dagger 不知道如何创建它的实例。您需要在 @Component.Builder 上提供类似 Builder restModule(RestModule restModule) 的方法,并在创建过程中像您的 @BindsInstance 方法一样调用它。

Each module dependency that Dagger can't instantiate itself (e.g, the module doesn't have a visible no-args constructor) must have an abstract setter method. Other module dependencies (ones that Dagger can instantiate) are allowed, but not required. (from the @Component.Builder javadoc)


并非所有模块都是可实例化的;有些是接口(interface)或抽象类。由于 Dagger 静态调用静态方法,在编译时反射到 @Binds 方法上,所以很多模块根本不需要实例化。这通常是更理想的,因为 Dagger 可以跳过创建实例。

对于可实例化但具有可见的零参数构造函数的模块,Dagger 可以自动调用 new,您可以将这些模块从构建器中移除。 (您可以选择包括它们;它们不需要被调用,但如果您对模块进行子类化可能会有用,which the docs advise against anyway。)

对于可实例化但只有带参数的构造函数的模块,Dagger 无法创建模块;它不会像使用 @Provides 方法那样为构造函数提供来自图形的参数。您需要显式创建一个 Builder 方法,并使用您自己创建的模块实例调用它。

关于android - @Component.Builder 缺少所需模块或组件的 setter [Dagger2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50588519/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com