Android Hilt Tutorial - Injecting Dependencies with Hilt

  Рет қаралды 28,216

Simplified Coding

Simplified Coding

Күн бұрын

Welcome to Android Hilt Tutorial; and it is the final video of this Android Dependency Injection Course. In the last video we used Dagger2 to perform Dependency Injection in our project.
I guess you have realized that using Dagger2 is complex and it requires a lot of boilerplate code.
To simplify the dagger2 implementation with minimal boilerplate code we have Hilt that is built on the top of dagger framework.
In this Android Hilt Tutorial, we will remove dagger and we will use hilt to perform dependency injection in our project.
Get the source code from here
👉 www.simplified...
If you are having trouble navigating between different lessons of this Android Dependency Injection Course then you can go to this link.
👉 simplifiedcodi...
Here you will find all the videos of this course in chronological order.
If you want to get in touch with me; you can find my contact details here:
👉 www.simplified...
Finally, If you love my content please SHARE my courses to your friends. You can share this course to your Facebook, WhatsApp etc.
For more contents related to Software Development visit my website.
👉 www.simplified...
You can SUPPORT me if you want
👉 Patreon: / belalkhan
👉 UPI (India Only): probelalkhan@okaxis
👉 PayTM (India Only): +917549161079
👉 PayPal: www.paypal.me/...
You can also check some of my other courses from these links
👉 Android Testing Tutorial: bit.ly/2OWyO95
👉 Android MVVM Tutorial: bit.ly/3iLfVzu
👉 Android Game Development Tutorial: bit.ly/38JmltC
👉 Retrofit Android Tutorial: bit.ly/2DEwAl1
👉 Android RecyclerView Tutorial: bit.ly/2TqVjPz
👉 Android Room Tutorial: bit.ly/2PfQHeN
👉 Firebase Auth Tutorial: bit.ly/2WUGhEo
👉 Android Notification Tutorial: bit.ly/2Sc6jQl
👉 Building a Complete Wallpaper App: bit.ly/30UN8zV
👉 Build Tic Tac Toe in Android: bit.ly/30goxW0
❤️❤️ Thank You All for the Love and Support ❤️❤️
#AndroidDependencyInjection
#Dagger2
#Hilt

Пікірлер: 55
@berkayrahmikayaoglu3655
@berkayrahmikayaoglu3655 3 жыл бұрын
I think you are the best instructor that helps us develop apps with clean architecture. Thanks for that. Also, can you make a video for dark theme and custom dark theme topic ?
@infiniteitdevelopment
@infiniteitdevelopment 3 жыл бұрын
thank you a lot! going to watch the vid 10 more times, starting to understand something! brilliant channel, subscribed!
@JohnDoe-wx3zm
@JohnDoe-wx3zm 2 жыл бұрын
Thank you! I finally got my Hilt project to build! And it's thanks to this video! Of course, I shouldn't have had to, but then again Google's documentation sucks...
@krishnabharati9108
@krishnabharati9108 3 жыл бұрын
Brother I'm facing to much difficulty on learning android please make dedicated video on how to complete android journey. Also share your experience how you learned that will be helpful And I can't express my feelings 🥺 please brother. Everyone thought code but you can take to next level
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
I will make a video like this. And for you I would like to say "Work on your fundamental knowledge first".
@sumit180288
@sumit180288 2 жыл бұрын
You should now learn Jetpack Compose, which is the future of android
@harishreddy8247
@harishreddy8247 3 жыл бұрын
Thanks brother great explaination
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
You are welcome
@ntwontechnologies1728
@ntwontechnologies1728 Жыл бұрын
can you please do video like implementing hilt with example from the begining?
@krishnamohansingh821
@krishnamohansingh821 2 жыл бұрын
Nice Tutorial Thanks Bilal
@waylivelife___646
@waylivelife___646 2 жыл бұрын
thanks brother. Excellent explanation 💯
@shashankdaima
@shashankdaima 3 жыл бұрын
Sir I loved your content. Lots of love....❤❤❤
@rohanz_art_factory
@rohanz_art_factory 3 жыл бұрын
Good tutorials! Is this hilt integration possible with the login tutorial which was done using the BaseFragment concept?
@vaibhavalone4720
@vaibhavalone4720 Жыл бұрын
I need to see code for this concept. But when I try to get code showing me 500 server error. So where can I get code? please help here. Thanks in advance.
@mohitmalhotra4276
@mohitmalhotra4276 3 жыл бұрын
Great work !!
@iam-learning6374
@iam-learning6374 3 жыл бұрын
hi buddy - can you please give your thought on hilt ViewModel lifecycle; after screen rotation service is getting called again.
@vikashparajuli
@vikashparajuli 3 жыл бұрын
I am want to create an android library (without Application Class, Activity and Fragment) using Hilt. Can you please help on that?
@ntwontechnologies1728
@ntwontechnologies1728 Жыл бұрын
that includes, mvvm,retrofitapicalling,hilt .please do in single video.
@harishreddy8247
@harishreddy8247 3 жыл бұрын
Do one video through hilt show data in recycelerview
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
Sure.
@waqarvicky5943
@waqarvicky5943 3 жыл бұрын
Hi Bilal Khan, Your tutorial are awesome please make tutorials about CI/CD Fastlane for android.
@mojeebhasan5283
@mojeebhasan5283 3 жыл бұрын
Didn't you find any issue in his updated project regarding UsePrefrences. Have you gone through his updated project. I think you must check UserPrefrences and TokenAuthenticator class in his updated project. There you will find one issue and the issue is instantiating UserPrefrences despite Constructor injecting Userprefrences. In TokenAuthenticator UserPrefrences should be injected and not be instantiated.
@kenyong7595
@kenyong7595 3 жыл бұрын
Koin, hilt, dagger 2, which ine easy to implement? Dagger is really difficult to understand
@ArjunVerma-lq4cf
@ArjunVerma-lq4cf 3 жыл бұрын
for sure hilt but you have to understand just the basics of dagger 2 for hilt
@harshavardhansuda8981
@harshavardhansuda8981 3 жыл бұрын
Can we pass an activity in constructor to repository with hilt I want this functionality for common progress bar show and hide feature
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
No not at all, make a base activity to do this thing. You should never pass activity or view to repository.
@nareshbhadana3069
@nareshbhadana3069 3 жыл бұрын
Thanks a lot
@jebasingh2276
@jebasingh2276 3 жыл бұрын
Hi brother, you doing grate. I need road map to become app developer I have lot of confusion where to start and how to start kindly give some guidance
@preetisaxena2349
@preetisaxena2349 3 жыл бұрын
Sir please make videos on RXjava/ RxAndroid as I found there are no proper tutorial on this topic. Please please.
@daemongaming8733
@daemongaming8733 3 жыл бұрын
Hi sir i am your big fan and watching your videos from a long time . Kindly can you solve my problem have an error in my app: error is with AIDL 1 runtime error exception was raised while executing process.
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
You need to share more information about your problem.
@daemongaming8733
@daemongaming8733 3 жыл бұрын
@@SimplifiedCoding ok it is a VPN app (created by someone else). Can i contact u personally for full details?
@gulshankumarsingh1664
@gulshankumarsingh1664 3 жыл бұрын
Hello sir income solution ki web design apke taim ne ki thi
@mojeebhasan5283
@mojeebhasan5283 3 жыл бұрын
I have one doubt in your updated project. You have constructor injected UserPrefrences but in TokenAuthenticator.kt class you have instantiated UserPrefrences inspite of injecting UserPrefrences. Here is the code class TokenAuthenticator @Inject constructor( context: Context, private val tokenApi: TokenRefreshApi ) : Authenticator, BaseRepository(tokenApi) { private val appContext = context.applicationContext //context.applicationContext private val userPreferences = UserPreferences(appContext) // Here I think there is no need to instantiate UserPrefrences class UserPreferences @Inject constructor(@ApplicationContext context: Context) { private val appContext = context.applicationContext Are you getting my point?
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
I might have forget injecting that dependency, you can do it.
@NoName-tr2cc
@NoName-tr2cc 3 жыл бұрын
Your content is hidden by you. Your picture should be small please
@shashankbhat6184
@shashankbhat6184 3 жыл бұрын
🔥🔥🔥
@rohitjakhar6672
@rohitjakhar6672 3 жыл бұрын
Ya that is easier than dagger 😁
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
But for learning perspective you must know dagger too. As people ask about that in interview.
@rohitjakhar6672
@rohitjakhar6672 3 жыл бұрын
@@SimplifiedCoding ok sir i will also learn dagger
@naskop
@naskop 2 жыл бұрын
Simple and Clean explanation. Thanks..
@fsfaysalcse
@fsfaysalcse 3 жыл бұрын
This is the best explanation of dagger Hilt .. Thank you so much Brother . Can you make an another video with singleton , inject view models to activates also others hilt view components ?
@managersoft4512
@managersoft4512 2 жыл бұрын
does anyone remember the pass to log in?
@mojeebhasan5283
@mojeebhasan5283 3 жыл бұрын
I have no sound knowledge in android but my fundamental is strong. So I can understand the flow of app code and it can't escape me when somewhere something is wrong. It is in my blood that I fail to stop myself when somewhere something goes wrong. A lot of confusion and doubt arises while going through your updated code whether I am wrong or the logic you have applied is wrong. So forgive me If I am wrong.
@pooja87054
@pooja87054 3 жыл бұрын
I have navigation controller in one activity and hosting nav_graph.and opend fragment .i want context of basactivity in base fragment so How to get basactivity context in basefragment using dagger hilt?
@VamsiKrishna-cg4de
@VamsiKrishna-cg4de 2 жыл бұрын
Hi sir, you are best to the way of explaining the concept which we can understand easily. Now android studio also got update can you update this code in updated android studio.Which is help to the beginners also. Post a video on that one also which is more help full for leaners
@LongNguyen-ym5zi
@LongNguyen-ym5zi 3 жыл бұрын
First of all, thank you a great video :) I have some problem while using Hilt, really needs your help. In my tutorial, I also have similar structure. I have activity and view model. However I want to separate logic routing in activity into an interface for example (a Router). 1. In this Router, I need to pass a context which is refer to the activity because I want to do some routing right. Ex: (context as MainActivity).goToDetailItem() 2. This router is an interface, then I can inject it into viewmodel as constructor param 3. Viewmodel can do any logic, then routing the app by Router. So this is what I want. But I'm not sure how to solve problem with context in the activity module For example: @Module @InstallIn(ActivityComponent::class) object MainModule { @Provides @ActivityScoped fun provideMainRouter(context: Context): MainRouter { return MainRouterImpl(context) } } Question is how to refer the context above to `MainActivity context`, so that I can convert like this `(context as MainActivity).goToDetailItem()` Hope you can help me this case. Thank you 🙏
@mojeebhasan5283
@mojeebhasan5283 3 жыл бұрын
There is a one question. Suppose you alter access token, then in hndleApiError it will fall under which error category. There is a two framgment LoginFragment and HomeFragment right. So error 401 fill always come in LoginFrgment because of wrong credentials. Error 401 will never be raised in HomeFragment becuase you are not entering credentials in Homegrgment. So there is need to check if (this is LoginFragment) . You can simply use snackbar. I am not understanding what kind of logic you are using. By the way in case of tampring access token what error will occur and what will you in handleApi error in this case?
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
Suppose, the user is logged out from the backend. Aur the token saved in android side is expired. Now you will get 401 when in HomeFragment you will hit an api that requires a valid authorization. And 401 means user is not authenticated so we will clear the local storage and serve the user login page. Hope you got it.
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
You might have seen this situation many times in different apps. Sometimes apps logs you out automatically. It happens because token is expired.
@mojeebhasan5283
@mojeebhasan5283 3 жыл бұрын
But how server will perform logout operation for a user whose token has already been expired. I think there is no need to call logout function. Instead I can display the message using snackbar that token has expired and clear the expired token from android. Calling a logout function means your are hitiing an api with expired token in request header. Token has expired means you are already logout and recevied 401 error. logout function should be called when user wants to logout. Calling a logout function when a token got expired means you are asking an Api to log the user out who is not already logged in.
@SimplifiedCoding
@SimplifiedCoding 3 жыл бұрын
@@mojeebhasan5283 But how you will know the token is expired in app side. You will hit the api with the token, if you receive a 401 that means token is expired, Then you will try to hit the refresh token api to see if it is possible to get a refreshed token without relogin, thats why we created the TokenAuthenticator. If still you are getting a 401, that means clear the local data and serve the login page. That's what is happening here.
@mojeebhasan5283
@mojeebhasan5283 3 жыл бұрын
I have understood the flow of code in your app. What happens is when when you launch your app first dependency is injected wherever it is required. During the dependency injection refresh token stored in your app is sent to backened api to fetch the new access token and refresh token. Now here is the two cases. I have understood the flow of code in your app. What happens is when when you launch your app first dependency is injected wherever it is required. During the dependency injection refresh token stored in your app is sent to backened api to fetch the new access token and refresh token. Now here is the two cases. case(1)-> suppose the refresh token stroe in your app ha expired. In that case you you don't get new access token and refresh token from ackened api during dependency injection and you don't insert accesstoken in authorization header.When first main activity will start. Your are observing access token as livedata. If it is null it start login fragment else home fragment. In this case suppose access token is not null then it will start home fragment. And in home fragment you are making a request to backened api to fetch user info and this request requires access token in authorization header but during dependency injection access token has not been inserted in authorization header because you have got failure as a response while fetching refresh token and access token during dependency injection because your refresh token stored in your app has expired.A failure will occur and you are calling handliApi error. In handleApi error you are checking if it is error 401 and it is in home fragment you are calling logout function.And here is the problem because in case of logout function server needs to know who the user is to log him out but here is no acess token in authorization header. So how will backened api log the user out whose authorization header doesn't contain access token and user cant hit logout function on backened api unless and untill he sends access token in authorization header. So what I am saying is instead of calling logout fuction you make one more fuction just to clear data in your app and to send user to login fragment if you get 401 error in homefragment. No need to call logout. case(2)-> suppose refresh token stored stored in your app has not expired you get new access token and and refresh token during dependency injection. You then insert access token in authorization header along with storing it in your app datastore. So when main activity is lauch first acess token is not found null. So HomeFrgment will start and in Home frgament a request is made to backend api to fetch user info. In this case it will send user info back to your app because you have received new refresh token and access token against the old refresh token stored in your app datastore during dependency injection and you have already inserted that token in authorization header. If you don't trsut me you can check by calling a logout function when your refresh token is expired. It will raise an exception and application will crash
@mojeebhasan5283
@mojeebhasan5283 3 жыл бұрын
there is one issue in your project. You have constructor injected UserPrefrences but in TokenAuthenticator.kt class you have created the instance of UserPrefrences despite injecting UserPrefrences. Here is your updated code class TokenAuthenticator @Inject constructor( context: Context, private val tokenApi: TokenRefreshApi ) : Authenticator, BaseRepository(tokenApi) { private val appContext = context.applicationContext //context.applicationContext private val userPreferences = UserPreferences(appContext) // I am talking about this pls check this out
Android Dependency Injection - Manually Injecting Dependencies
14:54
Simplified Coding
Рет қаралды 16 М.
5 Fatal Coroutine Mistakes Nobody Tells You About
18:15
Philipp Lackner
Рет қаралды 87 М.
Will A Guitar Boat Hold My Weight?
00:20
MrBeast
Рет қаралды 243 МЛН
Spongebob ate Michael Jackson 😱 #meme #spongebob #gmod
00:14
Mr. LoLo
Рет қаралды 9 МЛН
ПРИКОЛЫ НАД БРАТОМ #shorts
00:23
Паша Осадчий
Рет қаралды 6 МЛН
Android Dependency Injection - Introduction
6:56
Simplified Coding
Рет қаралды 32 М.
Kotlin Scope Functions Tutorial - let, run, with, apply and also
11:59
Simplified Coding
Рет қаралды 12 М.
Kotlin Singleton Example with MVVM and Coroutines
36:24
CodingWithMitch
Рет қаралды 56 М.
Dagger Hilt Android Tutorial - Dependency Injection
31:42
Ahmed Guedmioui
Рет қаралды 6 М.
Hilt - Android Dependency Injection
28:57
Android Developers
Рет қаралды 51 М.
State in Jetpack Compose
43:46
Android Developers
Рет қаралды 73 М.
Hilt ViewModels and Dependency Injection
16:35
CodingWithMitch
Рет қаралды 30 М.