Android:Dagger2组件无法识别应用程序类

时间:2018-11-21 16:20:20

标签: android dagger-2 dagger

似乎我无法从ApplicationComponent中找到MyApplication类:

enter image description here

此错误输出:

  

错误:找不到符号类MyApplication

这里是所有相关的类:

ApplicationComponent:

@ApplicationScope
@Component(modules = {ApplicationContextModule.class,
        SharedPreferencesModule.class,
        KeyStoreModule.class,
        SharedPreferenceHelperModule.class,
        StartModule.class,
        AndroidInjectionModule.class,
        BindModule.class,
        AndroidSupportInjectionModule.class})
public interface ApplicationComponent {

    void inject(MyApplication myApplication);

    @Component.Builder
    interface Builder {
        @BindsInstance
        Builder application(MyApplication myApplication); //CANT FIND MYAPPLICATION
        ApplicationComponent build();
    }

    @ApplicationContext
    Context getApplicationContext();

    SharedPreferences getSharedPreferences();

    KeyStoreServiceInterface getKeyStoreService();

    SharedPreferencesHelper getSharedPreferencesHelper();

    StartViewModelFactory getStartViewModelFactory();

}

MyApplication类:

public class MyApplication extends MultiDexApplication implements HasActivityInjector {

    private ApplicationComponent applicationComponent;

    @Inject
    DispatchingAndroidInjector<Activity> dispatchingActivityInjector;

    @Override
    public void onCreate() {
        super.onCreate();
        applicationComponent = DaggerApplicationComponent.builder()
                .applicationContextModule(new ApplicationContextModule(this))
                .build();
        DaggerApplicationComponent
                .builder()
                .build()
                .inject(this); //ERROR HERE. Complains that there is no inject method, offers creation of one in ApplicationComponent, but already exists one.
    }

    @Override
    public AndroidInjector<Activity> activityInjector() {
        return dispatchingActivityInjector;
    }

    public ApplicationComponent getApplicationComponent() {
        return applicationComponent;
    }
}

这是我的清单:

<application
    android:name="MyApplication"
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme"
    tools:replace="android:appComponentFactory"
    android:appComponentFactory="whateverString">
    <activity android:name="start.StartActivity" ></activity>
</application>

为什么无法识别班级?

1 个答案:

答案 0 :(得分:2)

由于组件无法导入或使用它,显然看起来像是软件包名称问题

  1. 将Application类移动到根包名称(您在创建应用程序时输入的名称)下

  2. 使用.MyApplication注册您的应用程序类

android:name=".MyApplication"

或还可以在应用程序名称中添加任何其他软件包名称