Skip to main content

Tangle binds Android components for Dagger with Anvil.

Get Started      
version badge Gradle Plugin Portal license

Fragments

@ContributesFragment(AppScope::class)class MyFragment @FragmentInject constructor(  val repository: MyRepository) : Fragment() {
  val name: String by arg("name")
  @FragmentInjectFactory  interface Factory {    fun create(      @TangleParam("name") name: String    ): MyFragment  }}

Use constructor injection in Fragments, with optional AssistedInject-like factories for type-safe Bundle arguments. Bindings are created automatically.

Read more

ViewModels

class MyViewModel @VMInject constructor(  val repository: MyRepository,  @TangleParam("userId")  val userId: String) : ViewModel()
@Composablefun MyComposable(  navController: NavController,  viewModel: MyViewModel = tangleViewModel()) { /* ... */ }
class MyFragment : Fragment() {   val viewModel: MyViewModel by tangleViewModel() }

Inject ViewModels, including scoped SavedStateHandle arguments. Use the TangleParam annotation to automatically extract navigation/Bundle arguments and inject them explicitly.

Read more

Member Injection

@TangleScope(UserScope::class)class UserActivity : Activity() {
  @Inject lateinit var logger: MyLogger
  override fun onCreate(savedInstanceState: Bundle?) {    // inject MyLogger    TangleGraph.inject(this)
    super.onCreate(savedInstanceState)
    logger.log("started UserActivity")  }}

Inject scoped dependencies into "entry point" Android components using@TangleScope and any desired scope.

Read more

WorkManager

@TangleWorkerclass MyWorker @AssistedInject constructor(  @Assisted context: Context,  @Assisted params: WorkerParameters) : CoroutineWorker(context,params) {  override suspend fun doWork(): Result {    /* ... */  }}

Use Dagger's @AssistedInject and @Assisted annotations and@TangleWorker to inject any ListenableWorker.

Read more