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.
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:
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?
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'
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
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!
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. 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!
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
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!)
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?
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?