Android: Dagger2 组件无法识别应用程序 class
Android: Application class not recognized in Dagger2 Component
我似乎无法从我的 ApplicationComponent 中找到 MyApplication
class:
并且此错误输出:
error: cannot find symbol class MyApplication
这里是所有相关的 classes:
应用程序组件:
@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();
}
我的应用程序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;
}
}
这是我的清单:
<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>
为什么不识别class?
似乎是包名称问题,因为组件无法导入或使用它
将应用程序 class 移动到根包名称(您在创建应用程序时输入的名称)下
使用.MyApplication
注册您的应用程序class
android:name=".MyApplication"
或者还可以添加带有应用程序名称的任何其他包名称
我似乎无法从我的 ApplicationComponent 中找到 MyApplication
class:
并且此错误输出:
error: cannot find symbol class MyApplication
这里是所有相关的 classes:
应用程序组件:
@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();
}
我的应用程序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;
}
}
这是我的清单:
<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>
为什么不识别class?
似乎是包名称问题,因为组件无法导入或使用它
将应用程序 class 移动到根包名称(您在创建应用程序时输入的名称)下
使用
.MyApplication
注册您的应用程序class
android:name=".MyApplication"
或者还可以添加带有应用程序名称的任何其他包名称