kotlin dagger retrofit field injection - java

When trying to inject field variables using dagger I'm getting null. Here are the files. Some are in Java and some in Kotlin
App.java
public class App extends DaggerApplication{
#Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().application(this).build();
}
}
AppComponent.kt
#Singleton
#Component(modules = arrayOf(
NetworkModule::class,
ApplicationModule::class,
AndroidSupportInjectionModule::class
))
interface AppComponent : AndroidInjector<TBApplication> {
#Component.Builder
interface Builder {
#BindsInstance
fun application(application: Application): AppComponent.Builder
fun build(): AppComponent
}
}
NetworkModule.kt
#Module
class NetworkModule {
#Provides
#Singleton
fun provideOkHttpClient(): OkHttpClient {
val builder = OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
val interceptor = HttpLoggingInterceptor()
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
builder.addInterceptor(interceptor).build()
}
return builder.build()
}
#Singleton
#Provides
fun provideRetrofit(client: OkHttpClient): Retrofit {
val retrofit = Retrofit.Builder()
.baseUrl(BaseApi.SITE_ENDPOINT)
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.client(client)
.build();
return retrofit
}
}
// Repository where injection should be done
class Repository {
private var examsService: BlogExamsService
#Inject
var retrofit: Retrofit? = null
init {
// retrofit is null here
examsService = retrofit?.create(BlogExamsService::class.java)!!
}
}

Field injection won't work as you do not run inject() method.
To make it work with your approach you should call in you Repository class:
App.self.getComponent().inject(this)
Where:
self is static instance of your application
getComponent() public getter for ApplicationComponent
Though I would not recommend it in your case, it is a misuse of DI framework.
You should create RepositoryModule and #Provide instance of Repository the same as you have done with NetworkModule.

Change your Repository to:
class Repository {
private var examsService: BlogExamsService
#Inject
constructor(retrofit: Retrofit) {
examsService = retrofit.create(BlogExamsService::class.java)!!
}
}

Related

NPE while injecting Retrofit with Dagger

I'm using Dagger for DI and Retrofit for network.
When i trying to call retrofits api method in my presenter, i catchs NPE.
Here is Network module:
#Module
public class NetworkModule {
String baseUrl;
public NetworkModule(String baseUrl) {
this.baseUrl = baseUrl;
}
#Provides
#Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
return gsonBuilder.create();
}
#Provides
#Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient client) {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())
.baseUrl(baseUrl)
.build();
}
#Provides
#Singleton
UsersApi provideUsersApi(Retrofit retrofit){
return retrofit.create(UsersApi.class);
}
}
And app component:
#Singleton
#Component(dependencies = {}, modules = {AppModule.class, DatabaseModule.class, NetworkModule.class})
public interface AppComponent {
void inject(UsersFragmentPresenter usersFragmentPresenter);
UserDao userDao();
Retrofit retrofit();
UsersApi usersApi();
AppDatabase appDatabase();
Application application();
}
And when i inject api and call api method here, the member isn't inizialized
#InjectViewState
public class UsersFragmentPresenter extends MvpPresenter<BaseFragmentView> {
#Inject
UsersApi usersApi; // is null
public UsersFragmentPresenter() {
App.getAppComponent().inject(this);
}
public void loadData(){
usersApi.getUser()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(observer);
}

Inject presenter into activity via Dagger

I want to know how to inject Presenter in activity using code Following are details
Following is error message:
Error:(12, 46) error: cannot find symbol class DaggerCategoryPresenterComponent
Error:(9, 46) error: cannot find symbol class DaggerNetComponent
Error:(18, 10) error: [Dagger/MissingBinding] com.headytest.android.category_listing.CategoryContract.CategoryPresenter cannot be provided without an #Provides-annotated method.
com.headytest.android.category_listing.CategoryContract.CategoryPresenter is injected at
com.headytest.android.MainActivity.categoryPresenter
com.headytest.android.MainActivity is injected at
com.headytest.android.dagger_component.NetComponent.inject(com.headytest.android.MainActivity)
Following are modules
#Module
public class NetworkModule {
String baseURL;
public NetworkModule(String baseURL) {
this.baseURL = baseURL;
}
#Provides
#Singleton
Cache provideHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}
#Provides
#Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
#Provides
#Singleton
OkHttpClient provideOkhttpClient(Cache cache) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.cache(cache);
client.addInterceptor(interceptor);
return client.build();
}
#Provides
#Singleton
Retrofit provideRetrofit(Gson gson, OkHttpClient okHttpClient) {
try {
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(baseURL)
.client(okHttpClient)
.build();
} catch (Exception e) {
e.printStackTrace();
return new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.baseUrl(Constants.BASE_URL)
.client(okHttpClient)
.build();
}
}
}
ApplicationModule
#Module
public class ApplicationModule {
Application application;
public ApplicationModule(Application application) {
this.application = application;
}
#Provides
#Singleton
Application providesApplication() {
return application;
}
}
CategoryContractModule
#Module
public class CategoryContractModule {
public CategoryContractModule() {
}
#Provides
#AScope
CategoryContract.CategoryPresenter providesCategoryPresenter(CategoryPresenterImpl categoryPresenter) {
return (CategoryContract.CategoryPresenter)categoryPresenter;
}
}
Following are components:
#Singleton
#Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
void inject(MainActivity activity);
}
CategoryPresenterComponent:
#AScope
#Component(dependencies = NetComponent.class, modules =
{CategoryContractModule.class})
public interface CategoryPresenterComponent {
void inject(MainActivity activity);
}
AScope
#Scope
#Retention(RetentionPolicy.RUNTIME)
public #interface AScope {
}
Following is the code in MainActivity:
#Inject
CategoryPresenter categoryPresenter;
#Inject
Retrofit retrofit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DaggerCategoryPresenterComponent.builder()
.categoryContractModule(new CategoryContractModule())
.netComponent(((App) getApplicationContext()).getNetComponent())
.build()
.inject(this);
}
CategoryPresenterImpl
public class CategoryPresenterImpl implements CategoryContract.CategoryPresenter {
#Inject
public CategoryPresenterImpl() {
}
#Override
public void onStart() {
}
#Override
public void onStop() {
}
#Override
public void getCategoryLiast() {
}
}
I assume current error will be easy to fix, because it very much looks like this issue.
You've instructed Dagger how to provide #AScope CategoryContract.CategoryPresenter but in activity you request Dagger to inject CategoryContract.CategoryPresenter. Dagger is confused at this point, because those two things do not match.
What you have to do, is simply add #AScope to categoryPresenter:
#Inject
#AScope
CategoryPresenter categoryPresenter;
I've checked out your project. The problem was in NetComponent.java:
#Singleton
#Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
void inject(MainActivity activity);
}
The issue is in line void inject(MainActivity activity). Why is this an issue? Because by the time you construct NetComponent in App, dagger analyzes MainActivity for #Inject annotated field and sees CategoryPresenter. This means, that at this point Dagger should find appropriate provider/binder method in order to be able to inject MainActivity as declared inside this interface.
But it cannot find such a provider/binder method, because it is declared in an another component - CategoryPresenterComponent and this component (NetComponent) is not connected with CategoryPresenterComponent anyhow (subcomponents or component dependencies), thus bindings are not exposed.
Simply removing that line will make your build successful:
#Singleton
#Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
}
For resolving "Error:cannot access Nullable" refer to this thread, which suggest applying compile 'com.google.code.findbugs:jsr305:3.0.2' to your gradle file.
After doing this you'll overcome that error message and will stumble on retrofit2.Retrofit cannot be provided without an #Inject constructor or an #Provides-annotated method, which has the same symptoms as CategoryPresenter had.
To overcome this issue you have to add a Retrofit provider method inside NetComponent (or to remove #Inject Retrofit retrofit from activity):
#Singleton
#Component(modules = {ApplicationModule.class, NetworkModule.class})
public interface NetComponent {
Retrofit providesRetrofit();
}
After doing this you'll be able to run the app, but you'll end up in a NPE in MainActivity, because of this line:
.categoryContractModule(new CategoryContractModule(retrofit.create(HeadyAPI.class)))
You are referring to retrofit object, which is not yet initialized.
Long story short, your initial question has mutated a couple of times and in fact you had a few problems in your code. Still you have a problem there, because you are trying to use retrofit in order to construct a component, that was supposed to inject the retrofit for you.

Module must be set dagger 2

I am trying to create dependencies using dagger 2 with kotlin. I am getting this error on runtime
Caused by: java.lang.IllegalStateException: pk.telenorbank.easypaisa.di.modules.RetrofitApiModule must be set
at pk.telenorbank.easypaisa.di.DaggerAppComponent$Builder.build(DaggerAppComponent.java:54)
at pk.telenorbank.easypaisa.EasypaisaApp.onCreate(EasypaisaApp.kt:22)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1015)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4834)
at android.app.ActivityThread.access$1600(ActivityThread.java:168) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1440) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:150) 
at android.app.ActivityThread.main(ActivityThread.java:5659) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:822) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:712) 
Here is dependency graph.
#Module(includes = arrayOf(NetworkModule::class))
class RetrofitApiModule(val retrofitMvpApi: RetrofitMvpApi) {
#Provides
#Singleton
fun provideMvpApi(): RetrofitMvpApi {
return retrofitMvpApi
}
}
Here is RetorfitMvpApi
#Singleton
class RetrofitMvpApi #Inject constructor(retrofit: Retrofit) : MvpApi {
var retrofitService: RetrofitService
init {
retrofitService = retrofit.create(RetrofitService::class.java)
}
override fun login(source: String) =
retrofitService.getPosts(source, Constants.API_KEY)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.map { rs -> rs }
.doOnError { t -> t.printStackTrace() }
interface RetrofitService {
#POST
fun getPosts(#Query("sources") source: String,
#Query("apiKey") key: String): Observable<WSResponse>
}
}
Here is the component.
#Singleton
#Component(modules = arrayOf(AppModule::class, RetrofitApiModule::class))
interface AppComponent {
fun loginComponent(loginModule: LoginModule) : LoginComponent
}
What am i doing wrong here? I am using dagger 2.15
Dagger will automatically create Module for dependency graph if that Module has default constructor. If you use custom constructor then you have to provide the Module when you are building the graph.
For java code:
#Module
public class ContextModule {
private final Context context;
public ContextModule(Context context) {
this.context = context;
}
#Provides
#GithubApplicationScope
#ApplicationContext
public Context provideContext(){
return context.getApplicationContext();
}
}
Building graph:
githubApplicationComponent = DaggerGithubApplicationComponent.builder()
.contextModule(new ContextModule(this))
// not needed as Dagger automatically generate module class with no arguments
//.networkModule(new NetworkModule())
.build();
Your provideMvpApi method should take an instance of retrofitMvpApi and return its interface:
#Module(includes = arrayOf(NetworkModule::class))
class RetrofitApiModule() {
#Provides
#Singleton
fun provideMvpApi(val retrofitMvpApi: RetrofitMvpApi): MvpApi {
return retrofitMvpApi
}
}

How do I inject dependencies into an Activity that require an argument from a previous Activity?

I have a LoginActivity where the user logs in via Auth0 and returns an auth token. This token is passed to MainActivity:
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
intent.putExtra(KEY_ACCESS_TOKEN, credentials.getAccessToken());
intent.putExtra(KEY_ID_TOKEN, credentials.getIdToken());
startActivity(intent);
I was able to get dependency injection working with LoginActivity fine by following this guide.
Now I'm trying to inject dependencies into MainActivity. My MainActivity has a MainActivityViewModel to handle all the interactions between the UI and the data layer. I'd like to inject my API into my ViewModel:
PetshackApi apiService;
#Inject
public PetMapViewModel(PetshackApi apiService) {
this.apiService = apiService;
}
I have ViewModelModule, ViewModelKey, and MainActivityViewModelFactory (renamed from GithubViewModelFactory) defined. I injected the viewModelFactory at the top of my MainActivity:
#Inject
ViewModelProvider.Factory viewModelFactory;
And then use the factory to get my viewModel:
viewModel = ViewModelProviders.of(this, viewModelFactory).get(MainActivityViewModel.class);
I set this up using this answer.
The problem is that my Retrofit/PetshackApi dependency will require the accessToken from the LoginActivity. So I defined another method in my MainActivity to allow retrieving it:
public String getAccessToken() {
return getIntent().getStringExtra(LoginActivity.KEY_ACCESS_TOKEN);
}
I'm having trouble setting up my modules/components/???. I think I need to inject MainActivity somehow into my modules so I tried following Injecting Activity objects.
MainActivityComponent.java
#Component(modules={AndroidSupportInjectionModule.class, AppModule.class, MainActivityModule.class, ViewModelModule.class})
public interface MainActivityComponent extends AndroidInjector<PetApplication> {
#Component.Builder
abstract class Builder extends AndroidInjector.Builder<PetApplication>{
#BindsInstance
abstract Builder application(Application application);
}
void inject(MainActivity mainActivity);
}
MainActivityModule.java
#Module(subcomponents = MainActivitySubcomponent.class)
abstract class MainActivityModule {
#Binds
#IntoMap
#ActivityKey(MainActivity.class)
abstract AndroidInjector.Factory<? extends Activity>
bindMainActivityInjectorFactory(MainActivitySubcomponent.Builder builder);
}
MainActivitySubcomponent.java
#Subcomponent(modules = MainActivityChildModule.class)
public interface MainActivitySubcomponent extends AndroidInjector<MainActivity> {
#Subcomponent.Builder
public abstract class Builder extends AndroidInjector.Builder<MainActivity> {}
}
MainActivityChildModule.java
#Module
abstract class MainActivityChildModule {
#Provides
#Singleton
Retrofit providesRetrofit(Application application, MainActivity mainActivity) {
final String accessToken = mainActivity.getAccessToken();
Interceptor interceptor = new Interceptor() {
#Override
public okhttp3.Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("authorization", "Bearer " + accessToken).build();
return chain.proceed(newRequest);
}
};
// Add the interceptor to OkHttpClient
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(interceptor);
OkHttpClient client = builder.build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(application.getString(R.string.endpoint_url))
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
return retrofit;
}
#Provides
#Singleton // needs to be consistent with the component scope
PetshackApi providesPetshackApiInterface(Retrofit retrofit) {
return retrofit.create(PetshackApi.class);
}
}
Am I on the right track? Any hints or examples on how to do this?
I'd recommend moving your networking code outside of your Activity module and creating an Application module that could be shared across your application.
The important thing is, if you have a TokenStore that provides your token for each request you'd need to update the value as requests are sent.
#Module
abstract class NetworkModule {
#Provides
#Singleton
static TokenStore provideTokenStore(TokenStoreImpl tokenStore) {
return tokenStore;
}
#Provides
#Singleton
static OkHttpClient provideOkHttpClient(AuthInterceptor authInterceptor) {
// Add the interceptor to OkHttpClient
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(authInterceptor);
return builder.build();
}
#Provides
#Singleton
static Retrofit providesRetrofit(Application application, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(application.getString(R.string.endpoint_url))
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit;
}
#Provides
#Singleton // needs to be consistent with the component scope
static PetshackApi providesPetshackApiInterface(Retrofit retrofit) {
return retrofit.create(PetshackApi.class);
}
}
interface TokenStore {
String getToken();
void setToken(String token);
}
#Singleton
class TokenStoreImpl implements TokenStore {
String token;
#Inject
public TokenStoreImpl() { }
#Override
public String getToken() {
return token;
}
#Override
public void setToken(String token) {
this.token = token;
}
}
#Singleton
class AuthInterceptor implements Interceptor {
private final TokenStore tokenStore;
#Inject
public AuthInterceptor(TokenStore tokenStore) {
this.tokenStore = tokenStore;
}
#Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request()
.newBuilder().addHeader("authorization", "Bearer " + tokenStore.getToken()).build();
return chain.proceed(newRequest);
}
}

Dagger 2 with Kotlin, returning type with generic in ApplicationComponent

I want to return type with generic to be exposed by sub-graphs, the problem is in auto-generated java-classes, I tried to do something, but the one way to solve it is to remove generic type from AppComponent and return simple object. Is there more "right" approach?
Here is the AppComponent
#Singleton
#Component(modules = arrayOf(ApplicationModule::class))
interface ApplicationComponent {
fun inject(activity: BaseActivity<MvpView, MvpPresenter<MvpView>>)
//...
fun dataBase(): Database<Realm>
}
here is function in the ApplicationModule
#Provides #Singleton fun provideDatabase(#AppContext context: App): Database<Realm> {
Realm.init(context)
val config = RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.name("db")
.build()
Realm.setDefaultConfiguration(config)
return RealmDatabase(Realm.getDefaultInstance())
}
Then I want to receive my Database
#Provides #ActivityScope fun provideDich(database: Database<Realm>) = Someobject(database)
And then I see log that says:
**Error:com.test.data.storage.Database<? extends io.realm.Realm> cannot be provided without an #Provides-annotated method.**
Because dagger2 generates factories like this and there is java masks
public final class Logout_Factory implements Factory<Logout> {
private final MembersInjector<Logout> logoutMembersInjector;
private final Provider<SessionStorage.CloudStorage> arg0Provider;
private final Provider<Database<? extends Realm>> arg1Provider;
public Logout_Factory(
MembersInjector<Logout> logoutMembersInjector,
Provider<SessionStorage.CloudStorage> arg0Provider,
Provider<Database<? extends Realm>> arg1Provider) {
assert logoutMembersInjector != null;
this.logoutMembersInjector = logoutMembersInjector;
assert arg0Provider != null;
this.arg0Provider = arg0Provider;
assert arg1Provider != null;
this.arg1Provider = arg1Provider;
}
#Override
public Logout get() {
return MembersInjectors.injectMembers(
logoutMembersInjector, new Logout(arg0Provider.get(), arg1Provider.get()));
}
public static Factory<Logout> create(
MembersInjector<Logout> logoutMembersInjector,
Provider<SessionStorage.CloudStorage> arg0Provider,
Provider<Database<? extends Realm>> arg1Provider) {
return new Logout_Factory(logoutMembersInjector, arg0Provider, arg1Provider);
}
}
I had the same problem and I found out the solution.
You need to declare the #JvmWildcard in your #Provide method return type.
#Provides
#Singleton
fun provideDatabase(#AppContext context: App): Database<#JvmWildcard Realm> {
Realm.init(context)
val config = RealmConfiguration.Builder()
.deleteRealmIfMigrationNeeded()
.name("db")
.build()
Realm.setDefaultConfiguration(config)
return RealmDatabase(Realm.getDefaultInstance())
}

Categories

Resources