Skip to content

dagger hilt injecttion anywhere and everywhere

License

Notifications You must be signed in to change notification settings

dewantawsif/flexible-hilt

Repository files navigation

Guide

Compatibility

The library requires your app's minSdk to be 21+

Add dependencies

plugins {
    alias(libs.plugins.ksp)
}

dependencies {
    implementation("me.tawsif.hilt:flexible-hilt-core:0.60")
    ksp("me.tawsif.hilt:flexible-hilt-compiler:0.60")
}

Prepare class/interface

Make your base class/interface inherit FlexibleHiltItem

class Pet @Inject constructor(): FlexibleHiltItem {
    val type = "dog"
}

interface Human: FlexibleHiltItem {
    val profession: String
}

Enjoy the flexibility!

fun printPetType(pet: Pet = FlexibleHilt.get()) {
    println(pet.type)
}

Note: There's also FlexibleHilt.getLazy() which you can use to get a lazy instance of the class. Helpful in Activity classes where you can only use hilt classes after super.onCreate()

License

Copyright (c) 2024 Dewan Tawsif

This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.