Android error: cannot find symbol class DaggerAppC

2019-08-28 19:05发布

问题:

My colleague developed an Application according to this repository and I should make some changes

In my case After some changes and remove Google Room Library I faced this problem:

error: cannot find symbol class DaggerApplicationComponent

After several rebuilds still cannot generate Project

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 import dagger.Module; import 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();

}

Error Log :

error: [Dagger/MissingBinding] com.packagename.data.DataManager cannot be provided without an @Provides-annotated method.

com.packagename.data.DataManager is injected at

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

com.packagename.ui.feed.FeedViewModel is injected at

com.packagename.ui.feed.FeedActivity.mFeedViewModel

com.packagename.ui.feed.FeedActivity is injected at

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:

The problem is you havnt injected the DataManager class so you need to inject DataManager via constructor injection or you have to provide it in the AppComponent with the @Provides annotation and you should use AndroidSupportInjectionModule.class in place of AndroidInjectionModule.class as When your activities are extending AppCompatActivity then the AndroidSupportInjection is used.

@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();
    }

}