Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Dependency Injection in Android with Dagger 2 and Kotlin

In this Android with Kotlin tutorial, you'll learn about dependency injection and how to make use of the Dagger 2 Java/Android framework for this purpose.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/262-dependency-injection-in-android-with-dagger-2-and-kotlin

Great article. Using dagger and DI in general has been a confusing topic for me so I’m trying to soak up as much information as possible. I have a question though.
.
.
.
.

Now, you must initialize AppComponent. Do so by adding the following method to WikiApplication:

private fun initDagger(app: WikiApplication): AppComponent =
  DaggerAppComponent.builder()
      .appModule(AppModule(app))
      .build()

You’ll likely notice an error called out by Android Studio on DaggerAppComponent. Click Make Module ‘app’ from the Android Studio Build menu. This will generate a new file, called DaggerAppComponent.java

Import the generated DaggerAppComponent to clear the compile errors. Ignore the deprecation warning on the appModule() method; that will be fixed shortly.

.
.
.
.

This part is giving me trouble. I’m not sure if I’m doing anything wrong but clicking "Make Module 'app'" does nothing for me but give me an error:

Error:(16, 13) Unresolved reference: DaggerAppComponent

I downloaded the final project and it seems to be working fine. I wanted to see if there was something I was missing, but I seem to have the correct gradle dependencies and everything. Any insight into what I may be missing?

This can be caused by many things:

  1. Make sure you’ve imported all the dagger stuff
implementation 'com.google.dagger:dagger:2.11'   // at this time you can use 2.13 if I remember correctly
kapt 'com.google.dagger:dagger-compiler:2.11'    // 2.13 here too
provided 'javax.annotation:jsr250-api:1.0'
  1. Make sure you’ve applied the kotlin annotation processor plugin (apply plugin: 'kotlin-kapt') in the correct build.gradle file. In the correct file you should have the android application plugin applied (apply plugin: 'com.android.application'), usually as the first line

  2. Use the same name you’ve used for your Component. You should have annotated a class with the @Component annotation, and if you’ve called that class MyFirstComponent you will have to use DaggerMyFirstComponent instead of DaggerAppComponent

If everything is ok and you’re still having trouble let me know and I’ll try to help!

Hey thanks I was missing

provided 'javax.annotation:jsr250-api:1.0'

working fine now!

Is the original version of this article with Java still available? I found it was an excellent resource.

@coletz Please add Google Maven repository for a clean build on first load.

Can you please post a link to the previous version (Java only, no Kotlin) of the article? It was an excellent article for those who are learning to do DI with Java code. I did a lot of searching and it seems to be lost.

Hi prosquid, if you want me to add the google() repository to the root build.gradle that shouldn’t be needed since it’s just a shortcut for maven { url "https://maven.google.com" }, that is present in both the starter and the final project. So it should compile as is.

Hi @isaacp and @mediumone, sadly the old tutorial has been removed so there’s no link available. Apart from the kapt plugin and the syntax, it should’t be THAT different, the logic is the same

Oh okay, it didn’t compile on my end till I added that.

Oh. That’s unfortunate. For someone who has not started learning Kotlin, this article is not of much help really. I started following the older article along with the older code sets, but had to take a break. I resumed now and it’s not available. If possible, you could keep both articles online. These articles show up in the first two Google search results for “Dagger tutorials” and they would surely help both types of developers - those who use Java and Kotlin.

I’m sorry about the disservice, but that’s not on my own at all. Since android evolves very quickly it’s possible that articles are not in line with newer technologies, and thus it’s not ok for us to spread old stuff. We want to push new technologies, and I suggest to check kotlin if you’re not using it (it’s really a step forward!). Anyway if you want to continue with java, the main part here are annotations and the inject functions, and there’s no difference between java and kotlin in these things (variable types are declared AFTER variable name (eg String sample in kotlin is sample: String) that’s all). Sorry again!

Hello, I have a question. In tutorial there is:

class WikiApi @Inject constructor(private val client: OkHttpClient, private val requestBuilder: HttpUrl.Builder?) {

For curiosity I’ve tried the same with:

class WikiApi(private val client: OkHttpClient, private val requestBuilder: HttpUrl.Builder?) {

I mean without @Inject annotation. It seems to work the same as in tutorial. Can You tell me what’s the difference and why does it work too?
I’ve tried the same with EntryPresenterImpl and HomepagePresenterImpl and it works as well :slight_smile:

@coletz Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi, thanks for this great article, just a question. What about the deprecation warning on app module method? How can this be fixed?

@coletz Can you please help with this when you get a chance? Thank you - much appreciated! :]

The deprecation warning should disappear after a successful build. Let me know if that’s not your case, but it would be strange if you’re following the tutorial step by step. If you’re working on different code you should show it to us in order to comprehend what’s going on

Hi! Great tutorial and it helped me understand Dagger better!
I have a question regarding the use of @Singleton. I understand that it is telling Dagger that there is only a single instance of a particular dependency but does it make sense though when applied to Presenters or even the OkHttpClient? Without using Dagger, we would not use them as Singletons so why here?

Hi @pascalhow! As you said it’s not common to use singleton for instances like a presenter or an http client. And that’s not what we’re doing! Dagger APIs are “misleading” (sorry for not pointing this out in the article) and are using the annotation @Singleton while you are not actually creating a singleton, but a scope. You can check the video of the talk linked in the article (by Jake Wharton, one of the author of Dagger 2) that explain everything about how Dagger 2 works (and better than me for sure!)

Hi
I have a couple of questions:

  1. I am having trouble understanding the need for AppModule. I don’t see when we use it, more specifically I don’t see where we are using Context. I went ahead and deleted the module and all its relevant code all together and the project still compiled and even build and ran correctly. I am using the code from the final project provided at the end of the tutorial. My guess would be that if one was to expand on this architecture then we would have the context available to us if needed?

  2. This happened more by accident but in the NetworkModule I commented out the provideWikiApi function, ran the project on a device and it still worked correctly. How is this possible seeing as it is responsible for providing the WikiApi?