Icon

How integrate room and hilt on android with Jetpack Compose

A simple guide to implement Hilt and Room with Compose
24 Jul 18:16

In this post, we will address the topic of dependency injection with Dagger Hilt and the use of Room in Android,
using the Jetpack Compose frontend framework and Kotlin.

1. Creating a new project in Android Studio

Version Catalog

• Select the empty Jetpack Compose template
Version Catalog

• Give your project a name and change the package name to your preference
Version Catalog

• Wait for your project to load and run it using the play button
Version Catalog

• View your application
Version Catalog

2. Configuration of version catalog in the project

• Copy the following versions into your file

libs.versions.toml

- file
Version Catalog

code copied successfully!
• Copy the following dependencies into your file

libs.versions.toml

- file
Version Catalog

• Copy the following plugins into your file

libs.versions.toml

- file
Version Catalog

3. Configuration of build.gradle and necessary dependencies:

• Verify that your build.gradle.kts (project) file looks like this

build.gradle.kts (Project)

- file
Version Catalog

• Verify that your build.gradle.kts (:app) file has the following plugins added

build.gradle.kts (:app)

- file
Version Catalog

• Check that your build.gradle.kts (:app) file has the following dependencies added

build.gradle.kts (:app)

- file
Version Catalog

4. To make Hilt work in your application, do the following:

• Add the following code to the file

App.kt

- file
Version Catalog

5. Add your application class to your AndroidManifest

AndroidManifest.xml

- file
Version Catalog

6. Create the following file structure.

• This step can be optional, but we want to maintain good readability of our project and understand it
Version Catalog

7. In the root of the data package, create a data model called UsersImp and a repository called UsersRepository

UsersImp.kt

- file
Version Catalog

UsersRepository.kt

- file
Version Catalog

Version Catalog

Version Catalog

Version Catalog

8. In the di (Dagger Hilt) module, create a file

DataModule.kt

- file
• Add the following content, so we can use it in the ViewModel layer.
Version Catalog

9. In the root of your local directory, create a constants file called

CONSTANTS.kt

- file
• Add a constant called `NAMEDATABASE` and assign it the name of the database that your application will use.
Version Catalog

10. Create a package named AppDataBase and add the following files:

AppDataBase.kt

- file
Version Catalog

UsersDao.kt

- file
Version Catalog

Version Catalog

11. Create a Dagger Hilt package and add the file

DataBaseModule.kt

- file
Version Catalog

12. Create a presentation layer and add the following files

HomeViewModel.kt

- file
Version Catalog

HomeScreen.kt

- file
Version Catalog

Version Catalog

Version Catalog

13. Add information to the entity in your database using your ViewModel layer in the view

HomeScreen.kt

- file
Version Catalog


14. Add the view to the MainActivity

MainActivity.kt

- file
Version Catalog

15. Finish the app by hitting play and checking the results. 🎉

Version Catalog