Android错误:找不到符号类DaggerAppComponent

时间:2019-01-26 07:06:45

标签: android dagger-2 dagger

我的同事根据repository开发了一个应用程序,我应该进行一些更改

我的情况经过一些更改并删除了 Google Room 库,我遇到了这个问题:

错误:找不到符号类DaggerApplicationComponent

几次重建后仍无法生成项目

dagger2Version = '2.20'

implementation "com.google.dagger:dagger:$rootProject.dagger2Version"
annotationProcessor "com.google.dagger:dagger-compiler:$rootProject.dagger2Version"
implementation "com.google.dagger:dagger-android:$rootProject.dagger2Version"
implementation "com.google.dagger:dagger-android-support:$rootProject.dagger2Version"
annotationProcessor "com.google.dagger:dagger-android-processor:$rootProject.dagger2Version"

Application Class

import javax.inject.Inject;

import dagger.android.DispatchingAndroidInjector;
import dagger.android.HasActivityInjector;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;

public class MvvmApp extends Application implements HasActivityInjector {

    @Inject
    DispatchingAndroidInjector<Activity> activityDispatchingAndroidInjector;

    @Inject
    CalligraphyConfig mCalligraphyConfig;

    @Override
    public DispatchingAndroidInjector<Activity> activityInjector() {
        return activityDispatchingAndroidInjector;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        DaggerAppComponent.builder()
                .application(this)
                .build()
                .inject(this);
        BluetoothCollector.getInstance(this);
        AppLogger.init();

        AndroidNetworking.initialize(getApplicationContext());
        CalligraphyConfig.initDefault(mCalligraphyConfig);
    }


    @Override
    public void onTerminate() {
        BluetoothCollector.getInstance(this).disconnect();
        super.onTerminate();
    }
}

AppComponent interface

import javax.inject.Singleton;

import dagger.BindsInstance;
import dagger.Component;
import dagger.android.AndroidInjectionModule;

@Singleton
@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {

    void inject(MvvmApp app);

    @Component.Builder
    interface Builder {

        @BindsInstance
        Builder application(Application application);

        AppComponent build();
    }
}

ActivityBuilder Class     导入dagger.Module;     导入dagger.android.ContributesAndroidInjector;

@Module
public abstract class ActivityBuilder {

    @ContributesAndroidInjector(modules = {
            FeedActivityModule.class,
            BlogFragmentProvider.class,
            OpenSourceFragmentProvider.class})
    abstract FeedActivity bindFeedActivity();

    @ContributesAndroidInjector(modules = LoginActivityModule.class)
    abstract LoginActivity bindLoginActivity();

    // ...

    @ContributesAndroidInjector(modules = SettingsActivityModule.class)
    abstract SettingsActivity bindPageSixActivity();

    @ContributesAndroidInjector(modules = ChoosingModeActivityModule.class)
    abstract ChoosingModeActivity bindChoosingModeActivity();

}

错误日志:

  

错误:如果没有@Provides注释的方法,则无法提供[Dagger / MissingBinding] com.packagename.data.DataManager。

     在

com.packagename.data.DataManager中注入

     

com.packagename.ui.feed.FeedActivityModule.provideFeedViewModel(dataManager,…)

     在

com.packagename.ui.feed.FeedViewModel注入

     

com.packagename.ui.feed.FeedActivity.mFeedViewModel

     在以下位置注入

com.packagename.ui.feed.FeedActivity

     

dagger.android.AndroidInjector.inject(T)[com.packagename.di.component.AppComponent吗? com.packagename.di.builder.ActivityBuilder_BindFeedActivity.FeedActivitySubcomponent]

FeedActivityModule

import dagger.Module;
import dagger.Provides;

@Module
public class FeedActivityModule {

    @Provides
    FeedPagerAdapter provideFeedPagerAdapter(FeedActivity activity) {
        return new FeedPagerAdapter(activity.getSupportFragmentManager());
    }

    @Provides
    FeedViewModel provideFeedViewModel(DataManager dataManager, SchedulerProvider schedulerProvider) {
        return new FeedViewModel(dataManager, schedulerProvider);
    }
}

FeedViewModel

public class FeedViewModel extends BaseViewModel/*<FeedNavigator>*/ {

    public FeedViewModel(DataManager dataManager, SchedulerProvider schedulerProvider) {
        super(dataManager, schedulerProvider);
    }
}

1 个答案:

答案 0 :(得分:1)

问题是您已经注入了DataManager类,因此您需要通过构造函数注入来注入DataManager,或者必须在AppComponent中为其提供@Provides批注,并且应在使用AndroidSupportInjectionModule.class代替AndroidInjectionModule.class时您的活动正在扩展AppCompatActivity,然后使用AndroidSupportInjection。

@Component(modules = {AndroidSupportInjectionModule.class, AppModule.class, ActivityBuilder.class})
public interface AppComponent {

    void inject(AppApplication networkApplication);

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(AppApplication application);

        AppComponent build();
    }

}