The Ultimate Dagger-Hilt Guide (Dependency Injection) - Android Studio Tutorial

แชร์
ฝัง
  • เผยแพร่เมื่อ 5 ก.ค. 2024
  • Dependency injection is a core concept of software development. In this video, I'll show you how you can apply DI in an Android app using Dagger-Hilt.
    ⭐ Get certificates for your future job
    ⭐ Save countless hours of time
    ⭐ 100% money back guarantee for 30 days
    ⭐ Become a professional Android developer now:
    pl-coding.com/premium-courses...
    💻 Let me personally review your code and provide individual feedback, so it won't backfire and cost you a fortune in future:
    elopage.com/s/philipplackner/...
    Subscribe to my FREE newsletter for regular Android, Kotlin & Architecture advice!
    pl-coding.com/newsletter
    Join this channel to get access to perks:
    / @philipplackner
    Get the code here:
    github.com/philipplackner/Dag...
    Join my Discord server:
    / discord
    Regular programming advice on my Instagram page: / _philipplackner_
    Checkout my GitHub: github.com/philipplackner
    You like my free content? Here you can buy me a coffee:
    www.buymeacoffee.com/philippl...
    00:00 - What is dependency injection?
    06:45 - Setting up Dagger-Hilt
    08:25 - Setting up the structure
    10:39 - Creating a module
    17:20 - Injecting into ViewModels
    20:15 - Injecting the ViewModel in the UI
    21:40 - Creating the application class
    24:05 - Two dependencies of same type
    26:20 - Binding abstractions
    29:58 - Field injection
    31:28 - Lazy injection

ความคิดเห็น • 244

  • @leonardosibela
    @leonardosibela 2 ปีที่แล้ว +150

    Man, first of all, you are THE BEST Android TH-cam channel in the whole website.
    Second, you are just awesome!! You are a great teacher and a very charismatic human being.
    Last, but not least, I don't know how you don't get tired of making so many great videos.
    You ARE the best!! Thanks for teaching me so much. It's being a pleasure to have your videos as part of my life.

  • @manuelsilveriof
    @manuelsilveriof 11 หลายเดือนก่อน +28

    This is by far the best explanation of Dagger Hilt I have found online. Not even the official Android Developers' TH-cam channels explain it like you do. Thank you so much!!

  • @alexeysimchenko7494
    @alexeysimchenko7494 ปีที่แล้ว +32

    I use dagger/hilt every day but usually as copy/paste so today I decided to finally get known how it works. Your explanation is the best of several that I saw today!

  • @priyanshugupta7904
    @priyanshugupta7904 ปีที่แล้ว +6

    I am a student I can't buy u a coffee but I can like and share your video with my friends.
    Amazing Teaching :)

  • @shashanknanda7299
    @shashanknanda7299 4 หลายเดือนก่อน +3

    This is the most easy to understand explaination that I've ever found on dagger-hilt till now, you explained each topic very consicely yet clearly

  • @henrygalvan9333
    @henrygalvan9333 ปีที่แล้ว +2

    broo thank you, "you never actually call these functions, dagger hilt does", cleared up so much for me!

  • @osisuper98
    @osisuper98 2 ปีที่แล้ว +3

    I am lost for words already. Man is always hitting the nail on the end. Much love ❤️.

  • @moforemmanuel158
    @moforemmanuel158 ปีที่แล้ว +1

    your tutorials are the best because you take time to explain the subtle concepts behind the code which is rare from other tutorials
    i just had to watch this once the get the whole scope of what DI with Hilt is and it implementations

  • @harishodzic3274
    @harishodzic3274 ปีที่แล้ว +2

    I always try to give credit where credit is deserved. And you my man are pure gold. I just stumbled on your content and I'm happy for that.
    Fast, to the point, full of valuable information. Keep on going this is great. I will subscribe, upvote, like.
    Wish you a good luck in the upcoming days!

  • @jagat.jeeban
    @jagat.jeeban 2 ปีที่แล้ว +2

    You're a great teacher. Hats off to you man!! 🙌 Keep making such types of tutorials on android topics.

  • @mandarpawar1423
    @mandarpawar1423 ปีที่แล้ว

    Man, you don't know how grateful I am right now! THANK YOU SO MUCH!!!

  • @nantiyachomtee2998
    @nantiyachomtee2998 ปีที่แล้ว

    The quality of your content is incredibly high, awesome stuff!! Thanks a lot!!

  • @muyassarabdullah1504
    @muyassarabdullah1504 5 หลายเดือนก่อน

    I always go back to your tutorial because it is very simple and easy to understands from fundamentals point of view

  • @tonyalbanese6446
    @tonyalbanese6446 ปีที่แล้ว +6

    I still can't believe how terrific these videos are. You explanations are superb. Thank you so much for taking the time to make this content.

  • @attilabacsa2808
    @attilabacsa2808 2 ปีที่แล้ว +8

    Thank you, excellent content as always. When I decided to switch carrier and become an Android dev, your channel was one of the main sources to learn the basics.
    Keep up the good work! 💯

    • @PhilippLackner
      @PhilippLackner  2 ปีที่แล้ว +2

      Awesome, thank you and keep it up! 🙏

  • @mohinithakkar2172
    @mohinithakkar2172 ปีที่แล้ว +2

    Thank you so much for this! It has covered almost all the features of Dagger Hilt.

  • @ranjith_uzumaki
    @ranjith_uzumaki ปีที่แล้ว +8

    My man teaches me more than what I learnt from a 4 yearbachelor's degree.

    • @technics6215
      @technics6215 ปีที่แล้ว +1

      This is about a tool / library. Tools and libraries will change over time. Most of the things you learn in school won't change.
      If you are in engineering school you are not there to learn how to make a hole in metal with one particular kind of machine. You don't need a professor fot that. You are there to know why, when and where you have to make that hole.

    • @SumriseHD
      @SumriseHD 10 หลายเดือนก่อน

      ​@@technics6215but that opinion is way too nuanced!

  • @khalidrinaldy3671
    @khalidrinaldy3671 10 หลายเดือนก่อน

    you are the best Kotlin Android Developer's teacher, or maybe the best programming teacher i've ever watched

  • @nrg4285
    @nrg4285 4 หลายเดือนก่อน

    I normally blast through these tutorials but this one took a solid 1-2 hours because of coding and note-taking at the same time and there was so much good content. For example it takes 7 individual scrolls to get through the notes top to bottom, that's a lot more than usual

  • @samasama3482
    @samasama3482 5 หลายเดือนก่อน

    The perfect explanation of Dagger Hilt I have ever seen! thank you

  • @parulsinghal2k5
    @parulsinghal2k5 3 หลายเดือนก่อน

    you slayyyed it!!! 30 minutes video covered almost everything for a person who knew only how to use dagger2 👏

  • @rajm2671
    @rajm2671 ปีที่แล้ว

    Finallyyy, I learned Dagger-Hilt. Good explanation. Thanks, Philipp

  • @daniellubenov827
    @daniellubenov827 ปีที่แล้ว +1

    Those videos are with the latest and the best practices. Really the best android tutorials out there.

  • @guitarcat01
    @guitarcat01 2 ปีที่แล้ว +2

    You always teach in a very patient and simple way and all i can do i really thank you SO MUCH for what you're doing! I'd say more than 60% of what i know about android comes from you. This is by far the greatest android development channel on TH-cam =)
    PS: After this video i already implemented Dagger in two of my projects

  • @VinothKumar-oo8ye
    @VinothKumar-oo8ye 10 หลายเดือนก่อน

    philip lackner, i studied your and workout your Weather app (clean code architecture) course, and i don't understand most of things, then after watching this dependency injection i understand all the doubts i have on the weather app course. super tutorial ;)

  • @codingwithtd5196
    @codingwithtd5196 7 หลายเดือนก่อน

    Thanks man. This is one of the best Dagger Hilt tutorials I found.

  • @arma6363
    @arma6363 ปีที่แล้ว +1

    Hilt would be super easy by your teaching, Thanks alot dear philip

  • @maryfrancegonzales9570
    @maryfrancegonzales9570 2 ปีที่แล้ว

    The KING is BACK! Thanks bro!

  • @senk0n
    @senk0n 2 ปีที่แล้ว

    Oh my, thanks to YOU, it's such an easy to understand and implement simple clean arch project for my CV now

  • @tasmanlg
    @tasmanlg ปีที่แล้ว

    I have such praise for your content. Thank you so much, truly.

  • @AliAhmad-di5ck
    @AliAhmad-di5ck หลายเดือนก่อน

    Thanks a lot, I now can understand DI really well.

  • @Langi667
    @Langi667 ปีที่แล้ว

    Your tutorials are extremely helpful, thank you so much!

  • @leonkennedy1267
    @leonkennedy1267 3 หลายเดือนก่อน

    Thanks for your videos! It's very helpful! More power to you!

  • @GoceAnastasovski
    @GoceAnastasovski ปีที่แล้ว +1

    Great video! I finally understood Hilt! I would create a sequel to this video where you focus on unit tests, you write the tests without hilt first and then with Hilt

    • @PhilippLackner
      @PhilippLackner  ปีที่แล้ว +1

      I do have a hilt testing video on my channel :)

  • @mymobile550
    @mymobile550 2 ปีที่แล้ว

    Became so curious after watching this tutorial . Just after I implemented this in
    one of my project including database module.

  • @rahulrvp
    @rahulrvp 5 หลายเดือนก่อน

    Thanks a lot for making this video. Super helpful!

  • @pqsk
    @pqsk ปีที่แล้ว

    Great video! Thanks so much for the explanation of this library.

  • @pablocruz1302
    @pablocruz1302 2 ปีที่แล้ว +1

    You are the Best Android development content out there. Thanks for your great content

  • @avinashreddy9565
    @avinashreddy9565 9 หลายเดือนก่อน

    Great Explanation!!! Very Easy to understand...

  • @mithilmehta1501
    @mithilmehta1501 2 ปีที่แล้ว +12

    Hey Philipp, I am from India and I really love your videos and it helps a lot. Thanks! God bless you and keep up the good work. Your explanation are so simple that even a fresher can understand. 😊

    • @RaajKanchan
      @RaajKanchan ปีที่แล้ว

      Hey Mithil, I am new to Dagger Hilt. Can you help me, I want to pass String value from Activity to an AppModule. How can I do that?

    • @mithilmehta1501
      @mithilmehta1501 ปีที่แล้ว

      @@RaajKanchan why do you want to pass string to app module from activity? Do you want to pass it to next activity?

    • @RaajKanchan
      @RaajKanchan ปีที่แล้ว

      ​@@mithilmehta1501 No, no its just an example. Let me give you the example.
      -----------------------------------------------------
      class Customer @Inject constructor(@CName val customerName: String, private val age: Int) {
      fun printCustomerDetails() {
      println("$TAG :: $customerName is $age old")
      }
      }
      @Module
      @InstallIn(SingletonComponent::class)
      object AppModule {
      @Provides
      @Singleton
      @CName
      fun provideCustomerName() = "Mithil Mehta"
      @Provides
      @Singleton
      fun provideCustomerAge() = 24
      }
      @Qualifier
      @Retention(AnnotationRetention.BINARY)
      annotation class CName
      @Inject
      lateinit var customer: Customer
      customer.printCustomerDetails()
      -----------------------------------------
      Here in the module I am providing CustomerName and CustomerAge statically. Not dynamically. I just want to learn how can I do that.

    • @mithilmehta1501
      @mithilmehta1501 ปีที่แล้ว +1

      @@RaajKanchan bro, you are getting it wrong. This is not something to be passed usong dagger. This data which should be passed either from activity to another Activity or else load from network for that particular screen. Dagger should be used to inject those dependencies which screen requires but doesn't care how it is created. So, as per my understanding your usecase is incorrect.

  • @user-xm2hf1sb8n
    @user-xm2hf1sb8n 7 หลายเดือนก่อน

    Best Video in short time. Good content.

  • @TerminalKid
    @TerminalKid 8 หลายเดือนก่อน

    dude you're Epic, learning a 'lot of good practices from you which saves me time. 👍👍

  • @behnawm
    @behnawm 2 ปีที่แล้ว +1

    Thanks for the great video! Please consider explaining different scopes and lifecycles for the dependencies too.

  • @PamontBahktar
    @PamontBahktar ปีที่แล้ว

    You're awesome Philipp, thanks so much for this it's really helpful.

  • @lioliknumberone1428
    @lioliknumberone1428 ปีที่แล้ว

    very interesting and clear tutorial! Thank you very much!

  • @fredericoamigo
    @fredericoamigo 3 หลายเดือนก่อน

    I’m so grateful for all of your brilliant videos on Android development in kotlin and jetpack compose.
    Thank you so much for this!
    If I might suggest a future video: how to use foreground location for in a MVVP app using datasource-repository-viewmodel architecture.

  • @luckydevil1601
    @luckydevil1601 ปีที่แล้ว

    thank you so much! finally I understood the magic behind this!

  • @saikou_absolute
    @saikou_absolute ปีที่แล้ว

    this cool . I had the same problem and solved it with your content. good luck with your work.

  • @kamertonaudiophileplayer847
    @kamertonaudiophileplayer847 2 ปีที่แล้ว

    A very frequently my class functionality requires the context. But it is available only in an activity. So I was really frustrated until watched you video. Now, I just tell - inject! That's it, simple and elegant.

  • @masmmaw
    @masmmaw 2 ปีที่แล้ว +1

    love it 💖 still provide complete information and of course free. Thank you.

  • @user-di3iu9li6c
    @user-di3iu9li6c 5 หลายเดือนก่อน

    Thank you so much for your excellent explanation dude
    😀😃😄😁

  • @LinaSarkar7307
    @LinaSarkar7307 ปีที่แล้ว

    its priceless. the slickness and the passion of the devs. priceless. what is cheaper and better than soft? Piracy is NOT the answer if you

  • @Alchemist10241
    @Alchemist10241 2 ปีที่แล้ว

    This video was the exact info that i have needed.

  • @winsonmac
    @winsonmac 2 ปีที่แล้ว +4

    Great sharing! I learned new things from you like @Binds and Lazy Injection

  • @musfickjamil2831
    @musfickjamil2831 2 ปีที่แล้ว

    Philipp you look so fresh in new setup..setup looks awesome. we want setup tour

  • @SapphireStudioart
    @SapphireStudioart ปีที่แล้ว

    Great video! Well explained, thank you

  • @Hoseinsadonasl-jo4dm
    @Hoseinsadonasl-jo4dm หลายเดือนก่อน

    Really awsome Philipp

  • @kitcat2449
    @kitcat2449 ปีที่แล้ว

    Very clear, thanks!

  • @Sarthakz99
    @Sarthakz99 ปีที่แล้ว

    I was looking for the same Auth token use case!! Thanks a lot!!

  • @amirnaderlo3045
    @amirnaderlo3045 ปีที่แล้ว

    it was so helpful,Thanks

  • @atelier_mongame4119
    @atelier_mongame4119 ปีที่แล้ว

    This was so helpful!! Thank you

  • @BilalBerek-tl3tq
    @BilalBerek-tl3tq 5 หลายเดือนก่อน

    perfectly explained dude.

  • @MrVipulLal
    @MrVipulLal 8 หลายเดือนก่อน

    Your videos are all amazing 🙏👍👍👌💯

  • @7xFuryPlayz
    @7xFuryPlayz ปีที่แล้ว

    now i understand how to use Hilt properly , within my project but i have still some doubts within my mind about clean architecture implementation, hope so i will figure them too . but thanks for this complete video . 🤩☺

  • @bdjobstv8575
    @bdjobstv8575 ปีที่แล้ว

    thanks a lot!!! so nice reverb

  • @coldwolf5050
    @coldwolf5050 2 ปีที่แล้ว

    Great video, very helpful 👍

  • @whogashaga666
    @whogashaga666 ปีที่แล้ว

    Thanks for sharing this video.

  • @toniwincent6653
    @toniwincent6653 ปีที่แล้ว

    You are the best at explaining this so that I understand! Thank you so much! When I get my first job as a developer I will buy you coffey! 🙂

  • @ojo_lali_ngaji
    @ojo_lali_ngaji 2 ปีที่แล้ว

    great tutorial as always

  • @VV-bo7rx
    @VV-bo7rx 7 หลายเดือนก่อน

    Thank You very much!

  • @tarekrefaei90
    @tarekrefaei90 2 ปีที่แล้ว

    Totally Awesome ... Thanks alot ..

  • @andinaevitha6950
    @andinaevitha6950 ปีที่แล้ว

    ITS REALLY WORKED LOL THANK YOU DUDE

  • @manishpingale6558
    @manishpingale6558 ปีที่แล้ว

    I understood hilt like never before 🤯

  • @kamrulhuqmaroof113
    @kamrulhuqmaroof113 2 ปีที่แล้ว

    Wow man it really works!

  • @fsfaysalcse
    @fsfaysalcse 2 ปีที่แล้ว +2

    Sometimes I thought you might be a superhero 😜 And you Come from a different universe. Best of luck, brother. I am your biggest fan from Coroutines video playlist ❤

  • @xandrviking1113
    @xandrviking1113 ปีที่แล้ว

    Thanks Philipp 👏🤝

  • @chitye-aung
    @chitye-aung 2 ปีที่แล้ว

    Awesome! Thank You Philip

  • @genctasbasi
    @genctasbasi 11 หลายเดือนก่อน

    This guy is a gem.

  • @chrisperey561
    @chrisperey561 ปีที่แล้ว

    you are literally the best

  • @David-zb8br
    @David-zb8br ปีที่แล้ว

    Hey philipp, could you please make a video on how to integrate hilt viemodels and the compose navigation component the correct way? I been meessing around but all i get are unwanted recompositions and overall a mess (should you pass viewmodels between composables using the navigation component? or only the data inside them) Thanks in advance!

  • @realurah
    @realurah ปีที่แล้ว +1

    damn... you are such a good teacher man

  • @abdelazizyasser3512
    @abdelazizyasser3512 2 ปีที่แล้ว

    thank you so much for this amazing explanation, you're very awesome.I have a question please, how i can use dagger-hilt in my multi module project (modules are library-modules not a feature-modules), i thought to create a separate module object (i mean the dagger-hilt-module that annotated with @Module 😅) for each library-module and provide/bind what i need to inject.am i correct ?

  • @jafxc2383
    @jafxc2383 2 ปีที่แล้ว

    Thank you for sharing!!!

  • @OleksandrArtiushenko
    @OleksandrArtiushenko 2 ปีที่แล้ว

    hi, can i use interface "module " instead of astract class "module " (all funs inside are abstact)? during binding abstractions. thank you

  • @gastonsaillen2226
    @gastonsaillen2226 ปีที่แล้ว +1

    I think we should use @InstallIn(ViewModelComponent::class.java) since the repository lives inside a ViewModel, in the meantime that we inject the repository inside the ViewModel, hilt will then delete this repository instance when the ViewModel is cleared (onCleared is called, ViewModel is destroyed because its activity/fragment does not need it anymore). If we do it with the SingletonComponent it will be tied up with the Application class you created, and hence, the repository instance will outlive the ViewModel.
    Another way of not using @Named could be to use @Qualifiers which can be more verbose on for example creating two different okHttp clients
    Great video Philipp, it contains a lot of excellent suggestions !

  • @user-uu8rn6wf4u
    @user-uu8rn6wf4u ปีที่แล้ว

    luv

  • @RahulKumar-un6ur
    @RahulKumar-un6ur ปีที่แล้ว

    Wow....what an amazing,clear and short to the point explanation. Out of curiosity, I would like to ask a question please. Can we keep the AppModule a normal class instead of object? Is it recommend to make a module (of course, not a binding module) object instead of a normal class? I tested and both works. Tons of thanks for your time and this amazing explanation.

  • @omkarkshirsagar8812
    @omkarkshirsagar8812 2 หลายเดือนก่อน

    first time i saw video i dont get it , after seeing video second time now its crystal clear

  • @mnouman6736
    @mnouman6736 2 ปีที่แล้ว

    lot of love for you bro

  • @iziaulla
    @iziaulla ปีที่แล้ว

    Great video man!!! I have just one question though. Where is all the Api parsing done? I thought it was in the view model. An object of the ViewModel is created in MainActivity. Then the UI is assigned its values in the MainActivity, correct?

  • @theevan3867
    @theevan3867 2 ปีที่แล้ว +1

    Thanks Phillip! if you can, can you make a youtube short of app examples that would be considered impressive to get an entry level position? Please. Thanks!!!

  • @samsammaniamantap
    @samsammaniamantap 10 หลายเดือนก่อน

    this is so helpful

  • @muhammadsarimmehdi
    @muhammadsarimmehdi ปีที่แล้ว +1

    can you create a dagger-hilt tutorial with custom scopes? That will be very useful to understand, especially for apps with user logins

  • @rajnarayanan3935
    @rajnarayanan3935 ปีที่แล้ว

    What keyboard shortcuts are you using for pulling up the "New Kotlin File/Class" and "New package" dialogs in this video?

  • @burakkarabekir2863
    @burakkarabekir2863 2 ปีที่แล้ว

    Nice recap, thx man!

  • @mohannadyoussef8683
    @mohannadyoussef8683 2 ปีที่แล้ว

    Thank you so much. you are the best.

  • @danielvillanuevacastillo359
    @danielvillanuevacastillo359 2 ปีที่แล้ว

    What shortcuts do you use for the new kotlin file and the new package?

  • @georgegichura8848
    @georgegichura8848 ปีที่แล้ว

    Worked, thx

  • @680086
    @680086 ปีที่แล้ว

    Very useful video

  • @ubersticks
    @ubersticks 2 ปีที่แล้ว +1

    Great job Phillip! When should we use "vm = hiltViewModel()" vs "vm:MyViewModel" by viewModels()" ??