Tutorial de Hilt para Android: inyección de dependencias con Hilt



Bienvenido al tutorial de Android Hilt; y es el video final de este curso de inyección de dependencia de Android. En el último video usamos Dagger2 para realizar Dependency Injection en nuestro proyecto. Supongo que entiendes que usar Dagger2 es complejo y requiere mucho código estándar. Para simplificar la implementación de dagger2 con un código estándar mínimo, tenemos Hilt, que está construido sobre el marco de dagger. En este tutorial de Android Hilt, quitaremos la daga y usaremos la empuñadura para realizar la inyección de dependencia en nuestro proyecto. Obtenga el código fuente desde aquí 👉 Si tiene problemas para navegar por las diferentes lecciones de este curso de inyección de dependencia de Android, puede ir a este enlace. 👉 Aquí encontrarás todos los videos de este curso en orden cronológico. Si quieres ponerte en contacto conmigo; puedes encontrar mis contactos aquí: 👉 Finalmente, si te encanta mi contenido, COMPARTE mis cursos con tus amigos. Puede compartir este curso en su Facebook, WhatsApp, etc. Para obtener más contenido relacionado con el desarrollo de software, visite mi sitio web. 👉 Puede APOYARME si lo desea 👉 Patreon: 👉 ​​UPI (solo en India): probelalkhan @ okaxis 👉 PayTM (solo en India): +917549161079 👉 PayPal: También puede consultar algunos de mis otros cursos desde estos enlaces 👉 Android Tutorial de prueba: 👉 Tutorial MVVM de Android: 👉 Tutorial de desarrollo de juegos de Android: 👉 Tutorial de actualización de Android: 👉 Tutorial de Android RecyclerView: 👉 Tutorial de Android Room: 👉 Tutorial de autenticación de Firebase: 👉 Tutorial de notificaciones de Android: 👉 Creación de una aplicación de fondo de pantalla completa: Construya Tic Tac Toe en Android: ❤️❤️ Gracias a todos por el amor y el apoyo ❤️❤️ #AndroidDependencyInjection # Dagger2 #Hilt.

Compruebe también

Transfiere datos de Android a iPhone con Switch to iOS #shorts

Más consejos sobre cómo transferir datos de Android a iPhone: Antes de comenzar: 1. Instale …

21 comentarios

  1. 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…

  2. Your content is hidden by you. Your picture should be small please

  3. Good tutorials! Is this hilt integration possible with the login tutorial which was done using the BaseFragment concept?

  4. Koin, hilt, dagger 2, which ine easy to implement? Dagger is really difficult to understand

  5. 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 🙏

  6. Can we pass an activity in constructor to repository with hilt
    I want this functionality for common progress bar show and hide feature

  7. Sir I loved your content. Lots of love….❤❤❤

  8. 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.

  9. 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?

  10. hi buddy – can you please give your thought on hilt ViewModel lifecycle; after screen rotation service is getting called again.

  11. 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?

  12. thank you a lot! going to watch the vid 10 more times, starting to understand something! brilliant channel, subscribed!

  13. 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

  14. 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?

  15. Wow, so we don't need multi-binding in case of injecting dependencies through constructor injection in the ViewModel?

  16. Hi Bilal Khan,
    Your tutorial are awesome please make tutorials about CI/CD Fastlane for android.

  17. I am want to create an android library (without Application Class, Activity and Fragment) using Hilt. Can you please help on that?

  18. Sir please make videos on RXjava/ RxAndroid as I found there are no proper tutorial on this topic. Please please.

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *