1

Доброго времени. Пытаюсь прикрутить Dagger 2

В gradle:

    compile 'com.google.dagger:dagger:2.11'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

При построении проекта все проходит нормально. Но при инжекте в активити падает с ошибкой в gradle

Error:(6, 8) The import com.hubble.news.di.DaggerAppComponent cannot be resolved
Error:(25, 16) DaggerAppComponent cannot be resolved
Error:(14, 10) com.hubble.news.di.modules.RetrofitModule cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
    com.hubble.news.di.modules.RetrofitModule is injected at
        com.hubble.news.ui.activity.MainActivity.retrofitModule
    com.hubble.news.ui.activity.MainActivity is injected at
        com.hubble.news.di.AppComponent.inject(mainActivity)
Error:Execution failed for task ':app:transformJackWithJackForDebug'.
> com.android.build.api.transform.TransformException: com.android.builder.core.JackToolchain$ToolchainException: Jack compilation exception
D:\ProgectAndroid\Hubble\app\src\main\java\com\hubble\news\di\AppComponent.java

MainActivity в котором инжект:

public class MainActivity extends MvpAppCompatActivity {

    @Inject
    RetrofitModule retrofitModule;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        App.getComponent().inject(this);

    }
}

App:

public class App extends Application {

    private static AppComponent component;

    public static AppComponent getComponent(){
        return component;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        component = buildComponent();
    }

    protected AppComponent buildComponent() {
        return DaggerAppComponent.builder()
                .retrofitModule(new RetrofitModule())
                .build();
    }
}

AppComponent:

@Component(modules = {RetrofitModule.class})
@Singleton
public interface AppComponent {

    void inject(MainActivity mainActivity);

}

RetrofitModule

@Module
public class RetrofitModule {

    @Provides
    @Singleton
    public HubbleApi provideRetrofit() {
        Gson gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ")
                .serializeNulls()
                .create();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://hubblesite.org/")
                .addCallAdapterFactory(RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io()))
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();

        return retrofit.create(HubbleApi.class);
    }
}

Как это вылечить???

1 ответ 1

2

Инъектить надо не модуль, но зависимость, кою модуль предоставляет. Т.е. замените тип переменной в активити на HubbleApi вместо RetrofitModule

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.