简体   繁体   中英

Android: Application class not recognized in Dagger2 Component

It seems like I cant find the MyApplication class from my ApplicationComponent:

在此处输入图片说明

And this error output:

error: cannot find symbol class MyApplication

Here is all the related classes:

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 class:

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;
    }
}

And here is my manifest:

<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>

Why does it not recognize the class?

Apparently looks like a package name issue as component is unable to import it or use it so

  1. Move the Application class under the root package name (that you entered while creating app)

  2. Use .MyApplication to register you application class

android:name=".MyApplication"

or may also add any additional package name with application name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM