Better Programming

Advice for programmers.

Follow publication

Member-only story

Implementing Manual Dependency Injection in Android Libraries

Siva Ganesh Kantamani
Better Programming
Published in
4 min readJul 21, 2021

--

Laptop screen
Photo by Sigmund on Unsplash.

There are numerous resources available for developers to implement DI in Android applications. But when it comes to DI in an Android library, I only found a few — and only a couple of those are actually useful.

So I decided to write an article on why and how I implemented manual dependency injection in an Android library. My experience with DI in libraries goes back to 2018 when the company that I work for made a deal with one of the industry leaders. They now wanted a part of our application to include their applications as a library.

What developers initially don’t give much thought to when it comes to libraries is how restricted we are in development compared to actual Android app development. If you have some dependency on an application class and have created your own application class in the library, the actual app-level application class should extend the library application class. Otherwise, you’ll get a compile-time error.

Coming to the topic of DI in an Android library, it’s very much dependent on the application class. If you take dagger or hilt as your DI framework, you need to trigger the code generation from the application class. This kind of application-level dependency for a library creates friction…

--

--

Write a response