示例代码:https://gitee.com/vigiles/component-app-demo
业务比较庞杂的应用,通常开发团队也很大,人员众多。对业务进行梳理后划分为可独立的n个部分,每个部分由一个人或团队进行开发。开发中这个部分可以进行独立的运行及调试。
业务层有若干可独立的业务组件,业务组件直接没有依赖关系。业务层的组件有library和application两个状态。
基础功能层的组件之间也没有相互依赖;基础功能层供业务层引用。基础功能层组件只有library一个状态。
1.全局配置
包括依赖库,版本相关、app和业务模块id、等等。
配置文件 工程/build.gradle :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
buildscript { repositories { google() jcenter() } dependencies { classpath "com.android.tools.build:gradle:4.1.3" classpath "com.jakewharton:butterknife-gradle-plugin:10.2.3" } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } ext { // 关键字。项目的组件化配置 isDebugState = false; // true当前为开发阶段,业务组件可以作为独立的app;false发布阶段,全部组件作为library。 ard = [ compileSdkVersion: 30, buildToolsVersion: "30.0.3", minSdkVersion : 21, targetSdkVersion : 30, versionCode : 1, versionName : "1.0", ] lib = [ appcompat : 'androidx.appcompat:appcompat:1.1.0', constraintlayout : 'androidx.constraintlayout:constraintlayout:1.1.3', flexbox : 'com.google.android:flexbox:2.0.1', material : 'com.google.android.material:material:1.4.0', rxjava : 'io.reactivex.rxjava2:rxjava:2.2.21', // 响应式编程 rxandroid : 'io.reactivex.rxjava2:rxandroid:2.1.1', rxjava_async_util : 'io.reactivex:rxjava-async-util:0.21.0', reactive_streams : "org.reactivestreams:reactive-streams:1.0.2", eventbus : 'org.greenrobot:eventbus:3.2.0', // 订阅式事件总线 gson : 'com.google.code.gson:gson:2.8.8', // 数据解析 picasso : 'com.squareup.picasso:picasso:2.8', // 图片加载view butterknife : 'com.jakewharton:butterknife:10.2.3', // View注入 butterknife_compiler: 'com.jakewharton:butterknife-compiler:10.2.3', ] appid = [ applicationId: "com.cuiweiyou.componentapplication", moduleLoginId: "com.cuiweiyou.module.login", moduleMainId : "com.cuiweiyou.module.main", ] lib_arouter = 'com.alibaba:arouter-api:1.5.2' // 阿里路由库 https://github.com/alibaba/ARouter/blob/master/README_CN.md lib_arouter_compiler = 'com.alibaba:arouter-compiler:1.5.2' } |
2.基础库组件配置
基础库组件供业务组件引入,以统一整个工程使用的依赖库的版本,及统一一些约束条件。通常没有代码,AndroidManifest.xml是空的。
配置文件 工程/基础功能组件包/基础组件/build.gradle :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
def extend = rootProject.ext // 项目/build.gradle内的ext。 apply plugin: 'com.android.library' // 当前组件仅作为库使用 android { compileSdkVersion extend.ard.compileSdkVersion buildToolsVersion extend.ard.buildToolsVersion defaultConfig { // library没有业务,不需要单独调试,所以没有appid minSdkVersion extend.ard.minSdkVersion targetSdkVersion extend.ard.targetSdkVersion versionCode extend.ard.versionCode versionName extend.ard.versionName } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { api extend.lib.appcompat // 引入全局配置的核心库 api extend.lib.constraintlayout api extend.lib.flexbox api extend.lib.material api extend.lib.rxjava // 引入全局配置的三方库 api extend.lib.rxandroid api extend.lib.rxjava_async_util api extend.lib.reactive_streams api extend.lib.eventbus api extend.lib.gson api extend.lib.picasso api extend.lib.butterknife api extend.lib_arouter } |
3.业务组件配置
业务组件即须要和用户进行交互的模块。根据开发状态,切换对应的AndroidManifest.xml。
业务组件通常是Phone Tablet模块,而基础功能组件则是Android Library模块或。
业务组件配置文件 工程/业务组件包/业务组件/build.gradle :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
def extend = rootProject.ext // 项目/build.gradle内的ext。 if (extend.isDebugState) { apply plugin: 'com.android.application' // 调试模式时,此模块可作为app } else { apply plugin: 'com.android.library' } apply plugin: 'com.jakewharton.butterknife' android { compileSdkVersion extend.ard.compileSdkVersion buildToolsVersion extend.ard.buildToolsVersion defaultConfig { if (extend.isDebugState) { applicationId extend.appid.moduleLoginId // 调试模式时,此模块可作为app,才可有appid } minSdkVersion extend.ard.minSdkVersion targetSdkVersion extend.ard.targetSdkVersion versionCode extend.ard.versionCode versionName extend.ard.versionName javaCompileOptions { annotationProcessorOptions { arguments = [AROUTER_MODULE_NAME: project.getName()] // arouter须要的aty所在模块的注册名 } } } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } sourceSets { main { if (extend.isDebugState) { manifest.srcFile 'src/main/AndroidManifest.xml' // 调试模式时使用application属性的AndroidManifest.xml,正常app的清单。 } else { manifest.srcFile 'src/main/release/AndroidManifest.xml' // 发布模式时使用library属性的AndroidManifest.xml。即空白清单。 } } } } dependencies { implementation project(":moduleBase:libraryBase") // 引入基础功能组件,即与整个团队统一了依赖配置 annotationProcessor extend.lib_arouter_compiler // arouter须要的aty所在模块的注册名解析器 annotationProcessor extend.lib.butterknife_compiler } |
【注意】
apply plugin: 'com.jakewharton.butterknife'
annotationProcessor extend.lib.butterknife_compiler
要在每个用到butterknife的子module里配置。
同时,通常在adapter里不使用butterknife加载item的layout,所以adapter里判断控件id时,使用if(R.id.x == v.getID()),替换switch-case。
4.工程结构
路由模块可以自己开发,也有第三方如阿里巴巴的ARouter。上文代码即引入的arouter,用于业务组件之间的跳转。
整个工程配置好后,就可以结合git,授权每个团队进行自己的模块开发。
- end
本文由崔维友 威格灵 cuiweiyou vigiles cuiweiyou 原创,转载请注明出处:http://www.gaohaiyan.com/3183.html
承接App定制、企业web站点、办公系统软件 设计开发,外包项目,毕设