android国籍组件,android组件化之路
問題:實際業務變化快,而工程內各個功能模塊耦合度太高,不能對功能模塊進行快速方便地拆分或組裝。團隊共同開發中,可能一個文件同時被多人修改,導致每次更新提交代碼都需要消耗大量時間去merge代碼。每次修改,都需要進行功能測試和系統測試。
目的:解決以上問題,使項目可以靈活配置,功能模塊完全解耦,實踐組件化之路。
實現:在之前的開發中,一個應用程序,我們將全部功能模塊都寫在工程app包中。實踐組件化,我們將各個功能模塊獨立出來,最終以依賴包的形式整合到app主Module中去。在這里我寫了個項目demo,簡單分了幾個功能模塊,應用結構如下圖所示:
項目組件化結構圖
說明:每一個組件module是一個子工程,子工程可以依賴基礎庫baselibrary可獨立運行;也可以作為主工程的依賴庫。
具體實現:1:配置項目的buildscript,如下圖所示:
buildscript配置
關于butterknife的配置及用法:http://jakewharton.github.io/butterknife,butterknife8支持在lib庫中注解。
2:在主app module gradle中如下配置:
applyplugin:'com.android.application'
applyplugin:'android-apt'
android {
compileSdkVersion COMPILE_SDK_VERSION as int
buildToolsVersion BUILD_TOOLS_VERSION
defaultConfig {
applicationId APPLICATION_ID
minSdkVersion MIN_SDK_VERSIONas int
targetSdkVersion TARGET_SDK_VERSIONas int
versionCode 1
versionName "1.0"
}
signingConfigs {
debugConfig {
storeFile file("***.jks")
storePassword "******"
keyAlias "*****"
keyPassword "******"
}
releaseConfig {
storeFile file("******.jks")
storePassword "********"
keyAlias "*****"
keyPassword"*******"
}
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
dependencies {
if(!isDebug.toBoolean()) {//各個模塊作為庫加載
compile project(':loginmodule')
compile project(':startmodule')
compile project(':homemodule')
compile project(':msgmodule')
}else{//各模塊獨立運行
compile project(':baselibrary')
}
//router
apt'com.github.mzule.activityrouter:compiler:1.1.7'
}
說明:關于activityrouter的配置及使用,在后面會說到。
3:baselibrary作為各個模塊共同依賴的基礎庫,其gradle配置如下:
applyplugin:'com.android.library'
applyplugin:'com.jakewharton.butterknife'
applyplugin:'android-apt'
android {
compileSdkVersion24
buildToolsVersion"25.0.2"
defaultConfig {
minSdkVersion15
targetSdkVersion24
versionCode1
versionName"1.0"
}
buildTypes {
release {
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'],dir:'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
excludegroup:'com.android.support',module:'support-annotations'
})
compile'com.android.support:appcompat-v7:24.2.1'
compile'com.jakewharton:butterknife:8.5.1'
apt'com.jakewharton:butterknife-compiler:8.5.1'
compile'com.github.mzule.activityrouter:activityrouter:1.2.2'
}
4:在子工程模塊gradle配置如下(以homemodule為例):
if(isDebug.toBoolean()) {
applyplugin:'com.android.application'
}else{
applyplugin:'com.android.library'
}
applyplugin:'com.jakewharton.butterknife'
applyplugin:'android-apt'
android {
compileSdkVersion24
buildToolsVersion"25.0.2"
defaultConfig {
minSdkVersion15
targetSdkVersion24
versionCode1
versionName"1.0"
testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabledfalse
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
sourceSets {
main{
if(isDebug.toBoolean()) {
manifest.srcFile'src/debug/AndroidManifest.xml'
}else{
manifest.srcFile'src/release/AndroidManifest.xml'
}
}
}
resourcePrefix"home_"
}
dependencies {
compile fileTree(dir:'libs',include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
excludegroup:'com.android.support',module:'support-annotations'
})
compile project(':baselibrary')
apt'com.jakewharton:butterknife-compiler:8.5.1'
//router
apt'com.github.mzule.activityrouter:compiler:1.1.7'
}
說明:當module單獨運行和作為module運行時,其activity在manifest中設置也會不同,這里可以根據isDebug設置不同的manifest。當分別開發模塊時,容易出資源重復命名的問題,可以在build.gradle中設置resourcePrefix "home_",通過給模塊設置不同的資源前綴,可以避免重復命名。
Activity跳轉問題
從上面的配置中,可以看到,項目引入了apt'com.github.mzule.activityrouter:compiler:1.1.7',我們為什么使用ActivityRouter呢,當我們把各個功能模塊抽成獨立的lib的時候,各個模塊之間難免要進行Activity的跳轉及傳參。我們不能再像以前那樣直接通過startActivity來實現跳轉了。關于ActivityRouter的配置及用法:https://github.com/mzule/ActivityRouter
使用在application中注解:@Modules({"app","homeModule","loginModule","startModule","msgModule"})
public class XxxxApplication extends Application {
}
每個module中創建空java類注解:
@Module("homeModule")
public class HomeModule{
}
組件間通信問題:
不同組件Activity之間傳遞大量數據時可以通過EventBus來進行傳遞,EventBus原理及用法:https://github.com/greenrobot/EventBus
編譯運行
當在gradle.properties中設置isDebug=true時,可以獨立運行每個module,獨立運行調試,當設置isDebug=false,可以編譯運行整個project,注意isDebug變量設置改變時,要重新對gradle進行sync。
可能遇到的一些問題:
1:如果遇到/com/github/mzule/activityrouter/router/RouterInit.java javaError:(7, 5) 錯誤: 找不到符號 符號:? 變量 RouterMapping等錯誤,請檢查app是否把各個Module都依賴進來了。
2:使用butterknife注解時,每個Module會對應生成R2文件,即使在base庫里注解過了,Module也要重新注解,組件中不能直接使用。
3:如果項目中使用到menu注意:
@Override
protected void onMenuItemClick(MenuItem item) {
//? ? ? ? switch (item.getItemId()) {
//? ? ? ? ? ? case R2.id.xxx:
//? ? ? ? ? ? ? ? Routers.open(mContext,"");
//? ? ? ? ? ? ? ? break;
//? ? ? ? }
if (item.getItemId() == R.id.xxx){
Routers.open(mContext,"");
}
super.onMenuItemClick(item);
}
尋找對應的menu id時,用if else 代替 switch,注:itemid 和 R2值不一樣,和R是一樣的。
總結
以上是生活随笔為你收集整理的android国籍组件,android组件化之路的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 英语不好,学编程太困难?或许用文言文来编
- 下一篇: MongoDB管理工具studio 3t