How to use the Android dependencies in the Java library for framework development

How to use Android dependencies in the Java library to develop framework for framework development Android's dependency injection (DI) is a design mode to achieve the decoupling and maintenance of code.Dependent injection can unlocked strong coupling between components, and it is easy to test and expand unit.This article will introduce how to use Android dependencies in the Java library for framework for framework development. 1. Add dependency injection library to the project First of all, you need to add Android dependence to the dependence of the Java library project.You can use popular dependency management tools such as Gradle to add dependencies into the construction document of the project. For example, the way to add dependent injecting Dagger 2 in Gradle: as follows: groovy dependencies { implementation 'com.google.dagger:dagger:2.x' annotationProcessor 'com.google.dagger:dagger-compiler:2.x' } 2. Create the core component of the framework In the Java class library, a core component for configuration and initialization dependencies is created.Usually this component is an entrance class, called Application or Framework. public class MyFramework { private static MyFramework instance; private AppComponent appComponent; private MyFramework() { // Initialize dependency injection framework appComponent = DaggerAppComponent.create(); } public static MyFramework getInstance() { if (instance == null) { synchronized (MyFramework.class) { if (instance == null) { instance = new MyFramework(); } } } return instance; } public AppComponent getAppComponent() { return appComponent; } } 3. Create components and modules In the dependency injection framework, the component is responsible for injecting dependencies, while the module defines these providers of these dependencies.In the Java class library, multiple components and modules can be created to meet different needs. For example, creating an APP component and an app module to inject components that depend on the Java class library. @Component(modules = {AppModule.class}) public interface AppComponent { void inject(MyLibraryComponent myLibraryComponent); } @Module public class AppModule { @Provides public ApiService provideApiService() { return new ApiService(); } } 4. Injecting dependencies In the component of the Java library, you can use the @Inject annotation to mark the member variables, construct functions or methods that need to be injected.Then, call the injection method to use these dependencies to automatically complete the dependent injection. For example, the way to inject dependencies in the component in the Java class library is as follows: public class MyLibraryComponent { @Inject ApiService apiService; public MyLibraryComponent() { // Use the injection dependencies apiService.doSomething(); } } 5. Initialization framework In order to use the dependent injection framework in the Java library, the corresponding method needs to be called when the framework initialization is required to complete the dependent injection. For example, the method of initialization framework in the entrance class of the Java class library is as follows: public class MyLibrary { public static void main(String[] args) { // Initialize framework MyFramework.getInstance(); // Create a component instance MyLibraryComponent myLibraryComponent = new MyLibraryComponent(); // Inject dependencies MyFramework.getInstance().getAppComponent().inject(myLibraryComponent); } } In this way, the Android dependencies can be easily used in the Java library for framework development.It decouples the code so that it is easy to maintain and unit test between components, and provide expansion flexibility.