agentleFS
wrinfotelwrinfotel/verified-android-skills/skills/dependency-injection/SKILL.md

dependency-injection

Skill0 starsChanged 32 days agoMITView on GitHub

Hilt dependency-injection discipline for Android: constructor injection first, field injection only in entry points, @HiltViewModel with an @Inject constructor, module annotations, and no ViewModels passed to other classes. Use when writing or reviewing DI wiring (Hilt, Dagger, manual DI). Core rules have machine checks in checks/.

14 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use api-versioning, caching and 7 more.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • wpankSkill

    api-versioning

    API versioning strategies — URL path, header, query param, content negotiation — with breaking change classification, deprecation timelines, migration patterns, and multi-version support. Use when evolving APIs, planning breaking changes, or managing version lifecycles.

    Also in 3 projects that use this

  • wpankSkill

    caching

    Caching strategies, invalidation, eviction policies, HTTP caching, distributed caching, and anti-patterns. Use when designing cache layers, choosing eviction policies, debugging stale data, or optimizing read-heavy workloads.

    Also in 3 projects that use this

  • diego-tobalinaSkill

    logging

    Logging best practices for Java (SLF4J/Logback) and Node.js (Winston/Pino). Use for structured logging, log levels, and avoiding sensitive data exposure.

    Also in 4 projects that use this

  • ffsshhttiikkSkill

    openapi

    OpenAPI/Swagger specification and documentation

    Also in 3 projects that use this

  • Model Context ProtocolSkill

    project-structure

    Where things live in the Inspector repo — which client owns which surface, what is in core/ versus clients/web, and what each top-level directory is for. Use when locating code, deciding where a new module belongs, or answering which package owns or contains a given area of the codebase.

    Also in 3 projects that use this

  • harrissohail6Skill

    clean-architecture

    Clean architecture and SOLID principles. Auto-activates when designing systems or refactoring code structure.

    Also in 3 projects that use this

The file

We split this skill into the rules it gives an agent

7 rules. 0 are common, 4 are distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 13 edited versions we read keep these.

  • 4

    Distinctive

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    After ANY DI wiring edit, run the Verification step (bottom of this file).

    line 15
  2. Distinctive:

    MUST use constructor injection for regular classes — it keeps dependencies in the API surface and testable.

    line 19
  3. Very specific:

    MUST annotate the ViewModel constructor with @Inject: "Provide a ViewModel by annotating it with @HiltViewModel and using the @Inject …

    line 56
  4. Very specific:

    MUST put @Provides / @Binds methods inside a class annotated @Module — and every module MUST also carry @InstallIn(Component::class) to say …

    line 84
  5. Distinctive:

    MUST NOT pass a ViewModel to other classes — including another ViewModel's constructor: "Don't pass ViewModels to other classes, functions …

    line 110
  6. Distinctive:

    Run after every DI wiring edit (from the project root):

    line 143
  7. Very specific:

    All four must pass (exit 0) before declaring the task done.

    line 152

“Common” is measured: kept by at least 6 in 10 of the 13 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Dependency Injection

Operational rules for a coding agent. RIGHT/WRONG pairs are canonical. After ANY DI wiring edit, run the Verification step (bottom of this file).

1. Field injection only in entry points

MUST use constructor injection for regular classes — it keeps dependencies in the API surface and testable. Field injection exists only because system-instantiated Android classes (Application, Activity, Service, BroadcastReceiver, ViewModel) cannot take constructor parameters: those classes MUST be Hilt entry points — @AndroidEntryPoint (or @HiltAndroidApp for the Application) — for their @Inject fields to be populated. A @Inject lateinit var in a class that is not an entry point stays null and crashes at runtime.

WRONG:

class LoginActivity : AppCompatActivity() {
    @Inject
    lateinit var loginRepository: LoginRepository   // never populated
}

RIGHT:

@AndroidEntryPoint
class LoginActivity : AppCompatActivity() {
    @Inject
    lateinit var loginRepository: LoginRepository
}

class UserRepository @Inject constructor(
    private val api: UserApi,
)

WHY: "Hilt can provide dependencies to other Android classes that have the @AndroidEntryPoint annotation" — without it there is no container to inject from. The DI guide prefers constructor injection for everything else: dependencies stay verifiable at compile time instead of hidden.

2. @HiltViewModel requires an @Inject constructor

MUST annotate the ViewModel constructor with @Inject: "Provide a ViewModel by annotating it with @HiltViewModel and using the @Inject annotation in the ViewModel object's constructor." The UI obtains it through hiltViewModel() (Compose) or by viewModels() — never by constructing it.

WRONG:

@HiltViewModel
class ProfileViewModel(
    private val userRepository: UserRepository
) : ViewModel()

RIGHT:

@HiltViewModel
class ProfileViewModel @Inject constructor(
    private val userRepository: UserRepository
) : ViewModel()

WHY: @HiltViewModel only registers the ViewModel with the ViewModelComponent; the @Inject constructor is what tells Hilt how to build it. Without it the project fails to compile.

3. Providers live inside modules

MUST put @Provides / @Binds methods inside a class annotated @Module — and every module MUST also carry @InstallIn(Component::class) to say where it is installed. @Binds goes in an abstract class module (interface → implementation); @Provides goes in an object module (third-party classes, builders).

WRONG:

class DataModule {                       // missing @Module
    @Provides
    fun provideAnalyticsService(): AnalyticsService = AnalyticsServiceImpl()
}

RIGHT:

@Module
@InstallIn(SingletonComponent::class)
object DataModule {
    @Provides
    fun provideAnalyticsService(): AnalyticsService = AnalyticsServiceImpl()
}

4. ViewModels are not dependencies

MUST NOT pass a ViewModel to other classes — including another ViewModel's constructor: "Don't pass ViewModels to other classes, functions or other UI components. Because the platform manages them, you should keep them as close to it as you can." A ViewModel that needs another screen's state should share a repository (or an activity-scoped ViewModel obtained by the owner), not receive a ViewModel via injection.

WRONG:

@HiltViewModel
class ProfileViewModel(
    private val userViewModel: UserViewModel   // ViewModel as a dependency
) : ViewModel()

RIGHT:

@HiltViewModel
class ProfileViewModel @Inject constructor(
    private val userRepository: UserRepository  // share data, not ViewModels
) : ViewModel()

5. What the checks cannot see (review-verified)

  • Manual DI is legitimate for small apps (the guide documents it); the service-locator pattern is the discouraged middle ground — global state that makes tests interact with the same locator.
  • Scoping choices (@Singleton, @ViewModelScoped, custom components) and module organization across Gradle modules — verified by review.

Verification

Run after every DI wiring edit (from the project root):

bash <skill-dir>/checks/check_field_injection_entry_point.sh <kotlin-src-dir>
bash <skill-dir>/checks/check_hilt_viewmodel_inject.sh       <kotlin-src-dir>
bash <skill-dir>/checks/check_provides_in_module.sh          <kotlin-src-dir>
bash <skill-dir>/checks/check_no_viewmodel_in_viewmodel.sh   <kotlin-src-dir>

All four must pass (exit 0) before declaring the task done. Checks are deterministic heuristics — they catch the documented anti-patterns above, not every DI bug. §5 is verified by review against references/di.md.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.