Examples of Dagger 2 usage in production apps?

Dagger 2, Dependency Injection, Android Development, Production Apps, Code Examples
Explore practical examples of using Dagger 2 for dependency injection in real Android applications, demonstrating how to enhance modularity and testability in your projects.

        // Example of Dagger 2 usage in an Android application

        @Module
        public class AppModule {

            @Provides
            @Singleton
            Context provideContext(Application application) {
                return application.getApplicationContext();
            }

            @Provides
            @Singleton
            NetworkService provideNetworkService() {
                return new NetworkService();
            }
        }

        @Component(modules = {AppModule.class})
        @Singleton
        public interface AppComponent {
            void inject(MyApplication application);
        }

        public class MyApplication extends Application {
            private AppComponent appComponent;

            @Override
            public void onCreate() {
                super.onCreate();
                appComponent = DaggerAppComponent.builder()
                        .appModule(new AppModule(this))
                        .build();
                appComponent.inject(this);
            }

            public AppComponent getAppComponent() {
                return appComponent;
            }
        }

        public class NetworkService {
            // Network related functions
        }
    

Dagger 2 Dependency Injection Android Development Production Apps Code Examples