Пытаюсь внедрить внедрение зависимостей с помощью Dagger 2, компонент инициализируется на базовом уровне в классе приложения, но приложение останавливается при запуске, прямо в строке кода, где инициализируется компонент кинжала, ниже приведен мой код.
Класс приложения
public class BaseApplication extends Application {
private Context context;
private Application application;
private static BaseApplication baseApplication;
protected AppComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
this.context = getApplicationContext();
this.application = this;
appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this)).build();
appComponent.inject(this);
//initializedApp();
}
public Context getContext() {
return context;
}
public Application getApplication() {
return application;
}
public AppComponent getAppComponent(){
return appComponent;
}
}
Класс AppComponent
@Singleton
@Component(modules = {NetModule.class, AppModule.class})
public interface AppComponent {
void inject(BaseApplication baseApplication);
}
Класс NetModule
@Module
public class NetModule {
@Provides
@Singleton
Retrofit provideRetrofit(){
return RetrofitClient.getInstance().getRetrofit();
}
}
Класс AppModule
@Module
public class AppModule {
private Application application;
public AppModule(Application application){
this.application = application;
}
@Singleton
@Provides
Application provideApplication(){
return this.application;
}
}
Конфигурация зависимости Gradle для Dagger 2
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVersion"
implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
DaggerVersion: 2.23
.
При этой настройке приложение запускается, но замирает с белым экраном, журналы показывают, что программа никогда выходит за пределы инициализации appComponent в BaseApplication
даже без внедрения Retrofit
в другие классы. Может кто-нибудь из тел, пожалуйста, помогите мне разобраться в этом, я признательна Даггеру c, я ценю.
Для большей ясности это мой класс RetrofitClient
public class RetrofitClient {
private final String TAG = RetrofitClient.class.getSimpleName();
private static String BASE_URL = "";
static {
if(BuildConfig.DEBUG){
BASE_URL = "http://0.0.0.0/api/v3/";
}else{
}
}
private static RetrofitClient mInstance;
private Retrofit retrofit;
private RetrofitClient() {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(getOkHttpClient().build())
.addConverterFactory(GsonConverterFactory.create())
.build();
}
public static synchronized RetrofitClient getInstance() {
if (mInstance == null) {
mInstance = new RetrofitClient();
}
return mInstance;
}
public Retrofit getRetrofit() {
return retrofit;
}
private OkHttpClient.Builder getOkHttpClient(){
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.authenticator(new Authenticator());
if(BuildConfig.DEBUG){
loggingInterceptor.level(HttpLoggingInterceptor.Level.BODY);
builder.addNetworkInterceptor(loggingInterceptor);
}
return builder;
}
private HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
@Override
public void log(@NotNull String s) {
Log.d(TAG, s);
}
});
}