demo地址
Android組件化專題,詳細講解組件化的使用及配置,以及實現的原理。
本文章講解了組件化的由來及配置,下期講解頁面路由跳轉及路由原理與apt
1. 組件化的由來
模塊化、組件化和插件化的關系?
(摘自百度百科)模塊化是指解決一個復雜的問題時自頂向下逐層把系統劃分為若干個模塊的過程,各個模塊可獨立工作。
在技術開發領域,模塊化是指拆分代碼,當代碼特別臃腫的時候,用模塊化將代碼分而治之、解耦分層。
在Android的領域模塊化具體的實施方法為:組件化和插件化。
更加詳細的講解
組件化和插件化的區別
一套完整的插件化或組件化都必須能夠實現單獨調試、集成編譯、數據傳輸、UI 跳轉、生命周期和代碼邊界這六大功能。插件化和組件化最重要而且是唯一的區別的就是:插件化可以動態增加和修改線上的模塊,組件化的動態能力相對較弱,只能對線上已有模塊進行動態的加載和卸載,不能新增和修改。
2. 怎樣實現組件化
要實現組件化需要考慮的問題主要包括下面幾個:
- 代碼解耦。將一個龐大的工程拆分解耦,這是非常耗時耗力的工作,但這也是最基礎最重要的一步
- 數據傳遞。每個組件都有可能提供給其他組件使用,主項目與組件、組件與組件之間的數據傳遞
- UI跳轉。
- 組件的生命周期。組件加載、卸載和降維的生命周期
- 集成調試。在開發階段如何做到按需的編譯組件?一次調試中可能只有一兩個組件參與集成,這樣編譯的時間就會大大降低,提高開發效率。
- 代碼隔離。如何杜絕耦合的產生。
實現組件化的第一步 整理代碼拆分結構
實現組件化的第一步首先是,整理項目工程結構,明確哪些功能是可以作為組件。
建議畫圖整理項目結構,如下圖:
實現組件化的第二步 在拆分代碼之前進行基礎配置
統一整理builde配置以及組件/集成模式的切換,實現組件的單獨調試
在項目根部新建 config.buildext {//
false 集成模式//
true 組件模式isComponent =
falseandroidConfig = [compileSdkVersion: 27,minSdkVersion : 19,targetSdkVersion : 27,versionCode : 1,versionName :
"1.0"]appIdConfig = [app :
"com.prim.component.demo",moudle1:
"demo.prim.com.moudle1"]supportLibrary =
"27.1.1"dependencies = [appcompatv7:
"com.android.support:appcompat-v7:${supportLibrary}"]
}
復制代碼主build中加入配置apply from:
"config.gradle"復制代碼moudle 配置,調用config.gradle 中的配置//配置apply
if (isComponent) {apply plugin:
'com.android.application'
}
else {apply plugin:
'com.android.library'
}//獲取config文件中的配置 rootProject 項目的主對象
def config = rootProject.ext.androidConfigdef appIdConfig = rootProject.ext.appIdConfigdef dependenciesConfig = rootProject.ext.dependenciesandroid {compileSdkVersion config.compileSdkVersiondefaultConfig {minSdkVersion config.minSdkVersiontargetSdkVersion config.targetSdkVersionversionCode config.versionCodeversionName config.versionName
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"//如果moudle為組件 配置組件的app ID
if (isComponent) {applicationId appIdConfig.app}//配置BuildConfig 代碼中可以調用判斷moudle是否為組件buildConfigField(
"boolean",
"isComponent",String.valueOf(isComponent))//配置資源文件
sourceSets {main {
if (isComponent) {//如果moudle為組件則配置 AndroidManifest manifest.srcFile
'src/main/moudle/AndroidManifest.xml'// 配置組件模式下的java代碼主文件java.srcDirs
'src/main/java',
'src/main/moudle/java'}
else {manifest.srcFile
'src/main/AndroidManifest.xml'}}}}buildTypes {release {minifyEnabled
falseproguardFiles getDefaultProguardFile(
'proguard-android.txt'),
'proguard-rules.pro'}}}dependencies {implementation fileTree(dir:
'libs', include: [
'*.jar'])implementation dependenciesConfig.appcompatv7implementation
'com.android.support.constraint:constraint-layout:+'testImplementation
'junit:junit:4.12'androidTestImplementation
'com.android.support.test:runner:+'androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'
}
復制代碼app 配置,調用組件apply plugin:
'com.android.application'def config = rootProject.ext.androidConfigdef appIdConfig = rootProject.ext.appIdConfigdef dependenciesConfig = rootProject.ext.dependenciesandroid {compileSdkVersion config.compileSdkVersiondefaultConfig {applicationId appIdConfig.appminSdkVersion config.minSdkVersiontargetSdkVersion config.targetSdkVersionversionCode config.versionCodeversionName config.versionName
testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"//配置BuildConfig 代碼中可以調用判斷moudle是否為組件buildConfigField(
"boolean",
"isComponent",String.valueOf(isComponent))}buildTypes {release {minifyEnabled
falseproguardFiles getDefaultProguardFile(
'proguard-android.txt'),
'proguard-rules.pro'}}
}dependencies {implementation fileTree(dir:
'libs', include: [
'*.jar'])implementation dependenciesConfig.appcompatv7implementation
'com.android.support.constraint:constraint-layout:+'testImplementation
'junit:junit:4.12'androidTestImplementation
'com.android.support.test:runner:1.0.2'androidTestImplementation
'com.android.support.test.espresso:espresso-core:3.0.2'if (!isComponent){//當moudle1 不為組件時才允許導入implementation project(
':moudle1')}
}
復制代碼下期講解頁面路由跳轉及路由原理與apt。
Android的組件化專題: 組件化配置
APT實戰
路由框架原理
模塊間的業務通信
總結
以上是生活随笔為你收集整理的Android组件化专题 - 组件化配置的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。