---
title: "Extending Anvil for Fun and Profit"
description: "Slack's Android app replaced dagger-android activity injection with a custom Anvil code generator: annotating an activity with @InjectWith(scope) generates an AnvilInjector wrapping Dagger's intrinsic MembersInjector plus a @ContributesTo binding module that adds it to a scope-level multibinding map."
url: "https://swe.quest/articles/extending-anvil-for-fun-and-profit-7f2a4cd8f637a17a"
canonical: "https://slack.engineering/extending-anvil-for-fun-and-profit/"
source: "Slack Engineering"
published: "2021-07-20"
difficulty: "hard"
kind: "Case study"
reading_minutes: 6
tags: ["mobile","jvm","build-systems","compilers"]
---

# Extending Anvil for Fun and Profit

A post on [Slack Engineering](https://swe.quest/sources/slack.md), published 2021-07-20, 6 min read, Case study. Original: [https://slack.engineering/extending-anvil-for-fun-and-profit/](https://slack.engineering/extending-anvil-for-fun-and-profit/)

## Summary

Slack's Android app replaced dagger-android activity injection with a custom Anvil code generator: annotating an activity with @InjectWith(scope) generates an AnvilInjector wrapping Dagger's intrinsic MembersInjector plus a @ContributesTo binding module that adds it to a scope-level multibinding map. BaseActivity looks up the injector from the scoped component in onCreate() and calls inject(). Built on Anvil 2.3.0's compiler-api, it runs inside kotlinc with no kapt or dagger-android-processor, avoids runtime reflection, and generalizes to any member-injected type such as services and jobs. Anvil's factory generation alone cut incremental build times by up to 25%.

Written by swe.quest, not by the author.

**Difficulty:** Hard: written for specialists in the field

## Topics

- [Mobile](https://swe.quest/tags/mobile.md) (Web & mobile)
- [Java & JVM](https://swe.quest/tags/jvm.md) (Languages)
- [Build systems](https://swe.quest/tags/build-systems.md) (Engineering practices)
- [Compilers](https://swe.quest/tags/compilers.md) (Compilers & runtimes)

## Prerequisites

- Dagger dependency injection ([easy posts on Application security](https://swe.quest/tags/appsec.md?difficulty=easy))
- Kotlin ([easy posts on Java & JVM](https://swe.quest/tags/jvm.md?difficulty=easy))
- Android activity lifecycle ([easy posts on Mobile](https://swe.quest/tags/mobile.md?difficulty=easy))
- Dagger multibindings

## Keywords

Anvil, Dagger, dagger-android, kapt, KotlinPoet, MembersInjector, Hilt

## Related posts

- [Scaling Slack’s Mobile Codebases: Modularization](https://swe.quest/articles/scaling-slack-s-mobile-codebases-modularization-70662aadd976d6a5.md): Slack Engineering · 2022-03-28 · Medium · 11 min read · Mobile, Build systems, Refactoring & migrations, Developer productivity, Swift
  Slack split its monolithic iOS and Android app targets into owned Feature, Service and Library modules with strict linking rules, such as interface/implementation pairs on iOS, to cut build times, untangle dependencies and clarify code ownership.
- [Enabling Kotlin incremental compilation on Buck2](https://swe.quest/articles/enabling-kotlin-incremental-compilation-on-buck2-bd5307852c0570ff.md): Meta Engineering · 2025-08-26 · Medium · 10 min read · Build systems, Java & JVM, Compilers, Developer productivity, Mobile
  Meta integrated the Kotlin incremental compiler into Buck2 via Kotlin's experimental Build Tools API, making some large Android modules build up to 3x faster and improving incremental build times about 30% for the average developer.
- [Scaling Slack’s Mobile Codebases: Modernization](https://swe.quest/articles/scaling-slack-s-mobile-codebases-modernization-950b9c539d5b5770.md): Slack Engineering · 2022-05-04 · Medium · 12 min read · Mobile, Refactoring & migrations, Swift, Java & JVM, Build systems
  Slack's year-and-a-half Duplo initiative concluded by modernizing its iOS and Android codebases: iOS moved to a custom VIPER-style feature architecture, stricter SwiftLint rules banning singletons and NotificationCenter, Combine (via CombineX until iOS 12 was dropped), and broader SlackKit adoption, while Android replaced Gson with Moshi, Android Priority Job Queue with WorkManager, and handwritten API calls with an internal Retrofit-based library, and introduced coroutines and Compose incrementally. iOS ended 68% modernized and 81% modularized with CI stability rising from 77% to 90% and average time-to-merge down 64%, Android reached 92% modularization, and local builds later dropped nearly 50% with Bazel and M1 Macs.
- [How we use Kotlin for backend services at Zalando](https://swe.quest/articles/how-we-use-kotlin-for-backend-services-at-zalando-86a4c360d01f2393.md): Zalando Engineering · 2021-06-30 · Easy · 4 min read · Java & JVM, Engineering culture, API design
  Zalando moved Kotlin from TRIAL to ADOPT on its Tech Radar, making it the third supported JVM language alongside Java and Scala after 100+ new Kotlin applications were written in a year.
- [It’s a non-transitive R class world](https://swe.quest/articles/it-s-a-non-transitive-r-class-world-dc7be71b63e01554.md): Slack Engineering · 2023-02-01 · Medium · 5 min read · Mobile, Build systems, Refactoring & migrations, Static analysis, Developer productivity
  Slack migrated its 400+ module Android codebase to non-transitive R classes, where each module's R class contains only its own resources, yielding ~14% faster incremental builds after resource or layout changes and an ~8.5% (about 5.5MB) APK/DEX size reduction.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
