[英]dagger2 injection not working - SharedPreferences
I am trying my hands on Dependancy Injection using Dagger2 . 我正在尝试使用Dagger2进行Dependancy Injection。 It gives error in build phase and says cannot inject SharedPreference instance. 它在构建阶段给出了错误,并表示无法注入SharedPreference实例。
Here are my modules and components. 这是我的模块和组件。
Application Module
@Module
public class ApplicationModule {
private Application app;
private String PREF_NAME = "prefs";
public ApplicationModule(Application app) {
this.app = app;
}
@Singleton
@Provides
public Picasso getPicasso() {
return new Picasso.Builder(app).build();
}
@Singleton
@Provides
public SharedPreferences getAppPreferences() {
return app.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
}
}
ApplicationComponent
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(App app);
}
App.java
public class App extends Application {
ApplicationComponent appComponent;
@Override
public void onCreate() {
super.onCreate();
createDaggerInjections();
}
public ApplicationComponent getAppComponent() {
return appComponent;
}
public static App getAppInstance(Context context) {
return (App) context.getApplicationContext();
}
private void createDaggerInjections() {
appComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(this))
.build();
appComponent.inject(this);
}
}
Login Module
@Module
public class LoginModule {
LoginView view;
public LoginModule(LoginView view) {
this.view = view;
}
@Provides LoginView getLoginView()
{
return view;
}
@Provides LoginPresenter getLoginPresenter(LoginView view)
{
return new LoginPresenterImpl(view);
}
}
LoginComponent
@ActivityScope
@Component(
dependencies = ApplicationComponent.class,
modules = LoginModule.class)
public interface LoginComponent {
void inject(LoginActivity activity);
LoginPresenter getLoginPresenter();
}
LoginActivity.java
public class LoginActivity extends BaseActivity implements LoginView {
private static final String TAG = "LoginActivity";
@Inject
SharedPreferences prefs;
-----
-----
-----
@Override
public void createDaggerInjections() {
DaggerLoginComponent.builder().applicationComponent(App.getAppInstance(this).getAppComponent())
.loginModule(new LoginModule(this))
.build();
}
This line @Inject SharedPreferences prefs;
这行@Inject SharedPreferences prefs;
gives error which is as follows. 给出如下错误。 The same error comes when I try to inject Picasso Instance also. 当我尝试注入Picasso Instance时也会出现同样的错误。
/home/blackidn/proj/styling android 3/dagger 2/DaggerEx/app/src/main/java/com/mohammad/daggerex/App/App.java:8: error: cannot find symbol
import com.mohammad.daggerex.dagger.DaggerApplicationComponent;
^
symbol: class DaggerApplicationComponent
location: package com.mohammad.daggerex.dagger
/home/blackidn/proj/styling android 3/dagger 2/DaggerEx/app/src/main/java/com/mohammad/daggerex/ui/Login/LoginComponent.java:16: error: android.content.SharedPreferences cannot be provided without an @Provides- or @Produces-annotated method.
void inject(LoginActivity activity);
^
com.mohammad.daggerex.ui.Login.LoginActivity.prefs
[injected field of type: android.content.SharedPreferences prefs]
Stucked with this and Don't know how to solve this and move forward. 坚持这一点,不知道如何解决这个问题并向前迈进。 What am I missing ? 我错过了什么? Any help would be great. 任何帮助都会很棒。
You should expose SharedPreferences to LoginComponent in ApplicationComponent. 您应该将SharedPreferences公开给ApplicationComponent中的LoginComponent。 Otherwise, you can't inject it. 否则,你不能注入它。
@Component(modules = {ApplicationModule.class})
public interface ApplicationComponent {
void inject(App app);
SharedPreferences sharedPreferences();
}
Dagger 2 update : The @Subcomponent could be used without any need to do this as can be seen here for the context: (ChatComponent is a subcomponent with custom scope) Dagger 2更新:@Subcomponent可以在没有任何需要的情况下使用,这里可以看到上下文:( ChatComponent是一个带有自定义范围的子组件)
from this excellent article: https://proandroiddev.com/dagger-2-part-ii-custom-scopes-component-dependencies-subcomponents-697c1fa1cfc 从这篇优秀的文章: https : //proandroiddev.com/dagger-2-part-ii-custom-scopes-component-dependencies-subcomponents-697c1fa1cfc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.