# Dependency Injection in Legacy Code

DevFeed: [Dependency Injection in Legacy Code](<https://devfeed.tech/articles/dependency-injection-in-legacy-code-24906.md>)

Original publisher: [Read original article](<https://blog.blundellapps.co.uk/dependency-injection-in-legacy-code/>)

Author: blundell

Published: 2023-10-19T12:26:59Z

Content type: tutorial

Language: en

Sources: [Blundell](<https://devfeed.tech/sources/blundell.md>)

Topics: [Dependency injection](<https://devfeed.tech/topics/dependency-injection.md>), [dagger-hilt](<https://devfeed.tech/topics/dagger-hilt.md>), [Android](<https://devfeed.tech/topics/android.md>), [Dagger](<https://devfeed.tech/topics/dagger.md>), [legacy systems](<https://devfeed.tech/topics/legacy-systems.md>), [software-development](<https://devfeed.tech/topics/software-development.md>)

Tags: [android](<https://devfeed.tech/tags/android.md>), [dagger](<https://devfeed.tech/tags/dagger.md>), [dagger-hilt](<https://devfeed.tech/tags/dagger-hilt.md>), [dependency-injection](<https://devfeed.tech/tags/dependency-injection.md>), [di](<https://devfeed.tech/tags/di.md>), [hilt](<https://devfeed.tech/tags/hilt.md>), [intermediate](<https://devfeed.tech/tags/intermediate.md>), [legacy-code](<https://devfeed.tech/tags/legacy-code.md>), [reference](<https://devfeed.tech/tags/reference.md>)

## AI overview

For Android legacy singletons or other classes outside the architecture managed by Hilt, the excerpt describes using a Hilt EntryPoint to retrieve dependencies from the existing Dagger graph. Define an interface annotated as an entry point, add a function for each required binding (including qualifiers), and install it in the appropriate component. The legacy code can then use the entry point through a Context reference. The approach assumes the required Dagger modules and bindings already exist; the excerpt says it does not work if the class cannot obtain a Context.

## Source excerpt

This post explains how you can inject dependencies into Android legacy singletons, or any classes that aren't conforming to your current Android architecture, using Dagger & Hilt. I'll cut straight to the chase, if you have a reference to Context, (which you usually always do in spaghetti legacy code) then you can use your normal [...] The post Dependency Injection in Legacy Code first appeared on Blundell.