1. Componentized Advantages The objective of Android APP componentized architecture is to bid farewell to the bloated structure and make each business relatively independent. Business components can be developed independently in the component mode, and can be integrated into the ARR package in the “APP shell project” in the integration mode to form a complete APP. It can be seen from the componentized engineering model that business components are independent and unrelated. In the integration mode, these business components are libraries, which are depended on by app shell engineering and form an APP application with complete business functions. However, in the component development mode, business components become applications. They can be developed and debugged independently, and because in component development mode, business components have far less code than a full project, they can significantly reduce compile time at run time.
2. Detailed explanation of componentized project architecture
The picture on the left is the componentized engineering model of the project
Integration mode: All business components are dependent on “APP shell project” to form a complete APP;
Component mode: Business components can be developed independently, and each business component is an APP;
App shell project: responsible for managing various business components and packaging APK, without specific business functions;
Business components: according to the company’s specific business and independent form a project;
Main component: a service component that specifies the APP startup page and Main interface.
Common component: also is the function component (component_base module), supports the basis of the business component, provides most of the functions required by the business component, such as providing the network request function;
Component_data: Stores the common data associated with the project, such as the bean base class,IntentKV stores the key-value equivalents of the data.
SDK components: integrate wechat, Alipay payment, sharing, push and other commonly used third-party frameworks.
3. Practice in the project
3.1 kotlin+MVP+ componentization to build the project framework
View: UI layer that displays data and reports user behavior to the Presenter layer
Model: Data layer, responsible for logical interaction with the network layer and database layer
Presenter: Take data from the Model and apply it to the UI layer to manage UI state, determine what to display, and respond to user behavior
3.2 Componentized partitioning details
3.3 Project directory division
4.1 Implementation of Switch between Component mode and Integration Mode: 4.1.1 Add variable ext {to config.gradle file
IsMineApplication = false //HomeModule switch, false: exists as Lib, true: exists as application isHomeApplication = false //MineModule switch, False: exists as a Lib component. True: Exists as an application. IsMessageApplication = false //MessageModule switch. True: as application being isPrepareLessonApplication = false / / PrepareLessonModule switch, false as Lib components exist, true: as applicationCopy the code
} 4.1.2 component model and the realization of the integration mode switch (gradle each business module configuration is as follows) : the if (rootProject. Ext isPrepareLessonApplication) {apply plugin: ‘com.android.application’ } else { apply plugin: ‘com.android.library’} 4.1.3 AndroidManifest merge between components: In Android, each component has a corresponding Androidmanifest.xml, which is used to declare the required permissions, Application, Activity, Service, Broadcast, etc. When the project is in component mode, The business component’s Androidmanifest.xml should have all the attributes of an Android APP, especially declaring the Application and Activity to launch, But when the project is in integration mode, we need to create an Androidmanifest.xml file for the business component in component development mode, and then specify the androidmanifest.xml file path according to isModule. Let business components use different Androidmanifest.xml in integration mode and component mode.
sourceSets { main { if (rootProject.ext.isPrepareLessonApplication) { manifest.srcFile ‘src/main/module/AndroidManifest.xml’ } else { manifest.srcFile ‘src/main/AndroidManifest.xml’ } jniLibs.srcDirs = [‘libs’]}} 4.1.4 Application independent initialization problem: When some modules have special functions, some initialization operations need to be performed in the onCreate of Application of a module:
Here, for example, the personal center module (MineMoudle) in our project needs to initialize the wisdom tooth SDK separately. We cannot write the initialization operation in the Library BaseApplication. If the initialization operation is performed in BaseApplication, it is unreasonable to initialize the wisdom tooth SDK in BaseApplication when we package a certain business module (the business module that does not use the wisdom tooth SDK). Solutions are as follows:
Call the following method in the onCreate method of BaseApplication (reflection + polymorphism to initialize method calls for individual modules) :
/ * *
- Initializes the business that each Module needs to initialize separately in the Application
- Each component’s BaseApplicationImp needs to be absolute
- The path is stored in ModuleConfig.MODULELIST for initialization by reflection
- Call onCreate to initialize the individual module
*/ private fun initModuleConfig() { ModuleConfig.MODULE_PATH_LIST.forEach { moduleImpl -> try { val clazz = Class.forName(moduleImpl) val obj = clazz.newInstance() if (obj is BaseApplicationImp) { obj.onCreate(this) } } catch (e: ClassNotFoundException) { e.printStackTrace() } catch (e: IllegalAccessException) { e.printStackTrace() } catch (e: Fragments. InstantiationException) {e.p rintStackTrace ()}}} ModuleConfig. MODULE_PATH_LIST configuration interface as follows ModuleConfig { companion object { val MODULE_APP = “com.tal.firstleap.app.AppImp” val MODULE_MINE = “com.tal.minemodule.app.MineModuleAppImp” val MODULE_HOME = “com.tal.homemodule.app.HomeModuleAppImp”
val MODULE_PATH_LIST = arrayOf(MODULE_APP,MODULE_MINE, MODULE_HOME)
}
Copy the code
} the MODULE_PATH_LIST constant stores the absolute path of the BaseApplicationImp interface implementation class in a module
interface BaseApplicationImp { fun onCreate(application:Application) }
MineModuleAppImp exists in a required service component class MineModuleAppImp: BaseApplicationImp {
Override fun onCreate(Application: Sobotapi.initsobotsdk (Application, constant.sabot_app_key,"")}Copy the code
} 4.1.5 Communication between components uses Ali Arouter to realize page jump and data transfer, details are as follows github.com/alibaba/ARo…