[Attachment] build.gradle.kts for Compose + Retrofit + KSP + Room + Hilt

In order to apply Hilt to my project, I modified build.gradle.kts and updated Compose, Retrofit, KSP, Room, Hilt, etc. to the latest. Both build.gradle.kts are provided in an attachment. Korean version of this posting: https://hhtt.kr/103026

I decided to use Hilt.

After struggling for over a day to apply HIlt to my project, I succeeded the day before yesterday evening. My goal was to change the instance contained in a container and passed as an argument to ViewModel through ViewModel Factory to be passed through Hilt. In other words, manual injection is changed to Hilt injection. I followed the codelab exactly, but it didn’t work. There were several differences between the two projects. The codelab I referenced is here: https://developer.android.com/codelabs/android-hilt

To apply HIlt to a project, you must first add Hilt-related libraries to the gradle build script. I don’t know if others did it easily, but even this was difficult for me. So, for now, I will only write about it in this article.

I always choose Google recommendations.

When developing Android apps, there are times when you have to make a choice. Regarding the language, you can choose between Kotlin and Java, and for UI, you can choose between Compose and Views. In this case, I decided to go the route recommended by Google. Therefore, I chose Kotlin and Compose. As for the build script, you can choose between a groovy script (build.gradle) and a Kotlin script (build.gradle.kts), but it seems to me that the one recommended by Google is the Kotlin script, so I chose that.

It was difficult to apply Hilt because the codelab and my project were different.

Compose + Retrofit + KSP + Room, etc. were applied to the project I am currently working on. This time I tried to add Hilt here. However, codelab’s build script was a groovy script. Since I have been using Kotlin scripts since I first started studying development, it was not easy to move the settings in build.gradle to build.gradle.kts.

Another problem is that the codelab uses kapt. When I last started using Room, I had changed all kapt to KSP. This time, I didn’t want to include kapt again because of HIlt. Although they said the two could coexist, I didn’t feel like it. Fortunately, these days, KSP can be used in Hilt. Now I just need to apply it to my build.gradle.kts.

I couldn’t do it alone, so I searched. Then I referred to the following article written by a Japanese person: https://qiita.com/ihridoydas/items/68608041202315cb7268
However, since kapt is used for Hilt here, I changed it to KSP to suit my situation. This wasn’t successful all at once, so I was able to find the right settings by modifying them one by one, referring to the error messages that Android Studio gave me.

I updated the important libraries to the latest version.

Last time, in order to resolve KSP-related errors, I referred to a Codelab’s build script related to it.
https://hhtt.kr/102886
But at that time, I was so focused on success that I didn’t pay attention to the versions of each library. Maybe I’ve adapted a lot over the months,. This time I feel like I want to upgrade all the libraries to the latest version.

Kotlin

In particular, I wanted to use the latest version of Kotlin. I saw a sentence saying that when a compilation error occurred in a certain situation, it was resolved by upgrading Kotlin. First, I kept Android Studio up to date and checked the Kotlin version in Settings > Languages and Frameworks > Kotlin. As a result, we decided to use Kotlin 1.9.22.

Since I updated Kotlin to the latest version, everything else seems to be working fine.

I tried to upgrade Room to 2.6.1 before, but it failed. This time, even after upgrading, the compilation error did not occur and everything worked fine.

Things that must be compatible

KSP vs Kotlin

You can check it out at:
https://github.com/google/ksp/releases

Since the Kotlin version I chose was 1.9.22, for KSP, I chose 1.9.22-1.0.16, which has the largest last number among 1.9.22-*.

Compose vs Kotlin

You can check it out at:
https://developer.android.com/jetpack/androidx/releases/compose-kotlin

Compose version suitable for kotlin 1.9.22 is 1.5.8. However, jvmTarget is listed as “19” there, but a compilation error occurred with this, so I set jvmTarget = “1.8”.

Attachment: build.gradle.kts (Project), build.gradle.kts (Module)

I need to decide whether to apply the settings to the entire project or to a specific module, but since my project only has one module, the settings are still a bit confusing. I plan to organize it later when it comes time to add other modules.

The addresses of each referenced site were commented out in the file.

Leave a Comment