一、gradle配置,组件化第一步需要规范自己项目的gradle写法
1.项目根目录创建config.gradle
//添加自定义属性
ext {
//生产/开发环境 false组件化环境 true集成化开发环境
isRelease = false
url = [
"release":"http://release",
"debug":"http://debug"
]
androidId = [
compileSdkVersion:29,
buildToolsVersion:"29.0.3",
minSdkVersion:24,
targetSdkVersion :29,
versionCode :10,
versionName :"1.1.0"
]
appId = [
"app":"com.hxdl.coco",
"demo":"com.hxdl.coco.demo",
"test":"com.hxdl.coco.test"
]
dependencies = [
"appcompat": "androidx.appcompat:appcompat:1.1.0",
"constraint": "androidx.constraintlayout:constraintlayout:1.1.3"
"recyclerview": "androidx.recyclerview:recyclerview:1.1.0",
"cardview":"androidx.cardview:cardview:1.0.0",
"kotlin": "org.jetbrains.kotlin:kotlin-stdlib:1.3.61",
...
]
}
2.引用config.gradle , 项目根gradle添加
apply from: "config.gradle"
为每个moudle gradle 配置引用config配置参数做统一化修改
主Module
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def support = rootProject.ext.dependencies
defaultConfig {
applicationId appId.app
minSdkVersion androidId.minSdkVersion
targetSdkVersion androidId.targetSdkVersion
versionCode androidId.versionCode
versionName androidId.versionName
...
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
if(isRelease){
implementation project(':demo')
implementation project(':test')
}
support.each { k , v -> implementation v}
}
其他moudle
if(isRelease){
apply plugin: 'com.android.library'
}else {
apply plugin: 'com.android.application'
}
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
android {
compileSdkVersion androidId.compileSdkVersion
buildToolsVersion androidId.buildToolsVersion
defaultConfig {
if(!isRelease) {
applicationId appId.demo
}
minSdkVersion androidId.minSdkVersion
targetSdkVersion androidId.targetSdkVersion
versionCode androidId.versionCode
versionName androidId.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
以上gradle配置 isRelease 为true 集成化环境 false 为组件化开发环境 每个module可以独立运行
isRelase控制gradle的动态配置 是否是作依赖库还是作为直接可运行的Moudle
support.each { k , v -> implementation v} 为grdle循环遍历语法 依赖config所配置的第三方依赖库
三、Module之间的通信
使用阿里的ARounter进行Mudle之间页面的跳转和通信https://www.jianshu.com/p/6021f3f61fa6
这样组件化的模型就基本搭建完成
网友评论