Loading...
Home
  • Tech Blogs
  • Videos
  • Conferences
    • Droidcon News
    • Upcoming Conferences
    • Become a Partner
    • Past Events
    • Keep Me Informed
    • Diversity Scholarships
  • Community
    • droidcon Team
    • How to Hold a Droidcon
  • Android Careers
Sign In

Global CSS

 

Change DI Library from Koin to Dagger-Hilt

 

 
hongbeom
Android Developer 1년차 주니어 안드로이드 개발자입니다.
Published: June 21, 2020
Tweet
Share
 

Photo by Jack Anstey on Unsplash

 

My first Android DI library was Koin. Dagger had a high running curve at the beginning and it was difficult to learn, and at that time, I used Koin to build an app at the company I work for. Recently, the Android studio unveiled a library called Dagger-Hilt, which makes it easier to use Dagger, which has a disadvantage of being difficult to learn. I didn’t know anything about the dagger, but I thought I should learn it someday, so I started using the Dagger-Hilt. Using HarryPotter, a sample project I used to posted about MVVM architecture, I changed my DI library from Koin to Dagger-Hilt to try to study about Hilt.

 

🧐 What is Dagger-Hilt?

The Hilt Library was first introduced at Dagger 2.28. It is currently in the alpha stage based on the post-creation date, and an official blog has been opened. Present, blog has a guidebook for easy migration using Dagger and a quick start guide for those who are new to Dagger-Hilt. First, let’s take a brief look at Hilt annotation.

 

@HiltAndroidApp

This annotation is the entry point for all modules that should be pasted into the application class we are going to use. It must be pasted to use Hilt, similar to @AndroidEntryPoint, but this only works in an application class.

 

@AndroidEntryPoint

Annotation used for Activity, Fragment, View, Service, BroadCast Receiver, etc. It acts like @DaggerActivity, @DaggerFragment, instantiated and processed as the proper Hilt component at the right point in Android class and Lifecycle. The name of the base class is ‘Hilt_<annotated_class_name>’. You can then insert an object in super.onCreate() at this point using @inject annotation.

 

@InstallIn

This is an annotation to add components to the module. It cannot be used without @Module or @EntryPoint annotation and cannot declare any components to add to this module.

 

Component

Currently, the Hilt components used by @InstallIn are configured as follows: Each component has its each own name have lifetime. This means that the objects in the module follow the life cycle of each component.

 

Image for post

 

@ViewModelInject

Simply paste this annotation into the ViewModel and you can automatically inject it. At this time, the ViewModel constructor can take advantage of all the dependency available through the @Activity RetainedComponent scope, or can selectively take advantage of the @Assisted annotation to use the SavedStateHandle.

Now, let’s look through the code.

 

🥇 Koin

The following code shows the implementation using the existing Koin.

DataSourceModule — Provides a data source that serves to import the required data after receiving an API service injection.

 

 

NetWorkModule — Sets Retrofit and OkHttp and provides services.

 

 

ViewModelModule — Receives the intent argument parameter and provides the viewModel via injection of the repository.

 

 

And that’s how Activity used it to hand over the required argument in ViewModel.

 

 

In the application class, the module was inserted into the startKoinfunction and used.

 

 

🗡 Dagger-Hilt

Now let’s use Dagger-Hilt to change the code created using the Koin. First, let’s start with a build.gradle file. Create the dependency in the project level build.gradle file as follows

 

 

It also writes the module-level build.gradle file as follows

 

 

From now on, Write code the modules in the same way. Please check it while comparing it with the code created by the Koin above.

DataSourceModule

 

 

NetworkModule

 

 

RepositoryModule

 

 

⚠️ Unlike the Koin, Dagger-Hilt does not require you to create a separate module for the ViewModel, and you can inject dependencies by attaching the ViewModelInject annotation to the ViewModel.

 

 

Assisted annotation allows you to inject a SavedStateHandle to easily load and use the required intent argument.

And this is how you use it in Activity.

 

 

Finally, for the Application class, you just have to add an annotation called HiltAndroidApp.

 

 

Conclusion

So I tried to use DI library with Koin and Dagger-Hilt easily, but in my opinion, the Dagger-Hilt is still in alpha stage, but I didn’t think there were many references and it was too early to use it at production level. Nevertheless, the official version will be released soon, so I think you good learn beforehand.

The full code can be found here. If this posting was helped, please give a star at GitHub.

 

hongbeomi/HarryPotter

Harry Potter Harry Potter Application is sample based on MVVM architecture. Fetch data from the network via repository…

github.com

 

Thank you for reading it! 🙌

 

 

 

Tags: Android, Android Studio, Dagger Hilt, Koin

 

View original article at: 


 

Originally published: June 27, 2020

Android News
Evolution of Android Update SystemEvolution of Android Update System
Evolution of Android Update SystemEvolution of Android Update System

By Ivan Kuten

So, how can you update Android on mobile devices? While developing software for Smart TVs and Android-based set-top boxes, we’ve narrowed it down to four ways, discarding some very exotic options:

By ProAndroidDev -
Android News
Happy Railway
Happy Railway

By Hadi Lashkari Ghouchani

This post is on the tail of Railway Oriented Programming in Kotlin by Antony Harfield. So you need to read it first and continue here. As it’s obvious I really liked it and tried it out. It needs every process have a result like

By ProAndroidDev -
Android News
Unit Tests and Concurrency
Unit Tests and Concurrency

By Stojan Anastasov

Once Retrofit added RxJava support, RxJava became my go-to concurrency framework for writing Android apps. One of the great things about RxJava is the excellent testing support. It includes TestObserver, TestScheduler, RxJavaPlugins so you can switch your schedulers in tests.

By ProAndroidDev -
Android News
When Compat libraries will not save you
When Compat libraries will not save you

By Danny Preussler

And why you should avoid using the “NewApi” suppression! The idea of “Compat” libraries was probably one of the key aspects of Android dominating the mobile space. Other than with iOS, Android users often could not update their operating system after a new version launch, simply as their phones won’t allow them to, the Android problem of fragmentation.

 

By ProAndroidDev -
droidcon News

Tech Showcases,

Developer Resources &

Partners

/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/home-details/EmployerBrandingHeader
EmployerBrandingHeader
https://jobs.droidcon.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/jobs-droidcon/jobs.droidcon.com
jobs.droidcon.com

Latest Android Jobs

http://www.kotlinweekly.net/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/kotlin-weekly/Kotlin Weekly
Kotlin Weekly

Your weekly dose of Kotlin

https://proandroiddev.com/
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/pad/ProAndroidDev
ProAndroidDev

Android Tech Blogs, Case Studies and Step-by-Step Coding

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Zalando/Zalando
Zalando

Meet one of Berlin's top employers

/detail?content-id=/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
/portal/rest/jcr/repository/collaboration/Groups/spaces/droidcon_hq/Documents/public/employerbranding/Academy for App Success/Academy for App Success
Academy for App Success

Google Play resources tailored for the global droidcon community

Follow us

Team droidcon

Get in touch with us

Write us an Email

 

 

Quicklinks

> Code of Conduct

> Terms and Conditions

> How to hold a conference

> FAQs

> Imprint

Droidcon is a registered trademark of Mobile Seasons GmbH Copyright © 2020. All rights reserved.

powered by Breakpoint One