用伪代码的方式看下,我们项目的结构
项目根
---- App
---- ---->build.gradle【app】
---- Module 1
---- ----> build.gradle【Module1】
...
---- Module N
---- ---- >build.gradle【module n】
---- >app.gradle 【我们自己抽取的公共gradle】
->build.gradle [root]
抽取Module 包含app的公共变量部分
首先
根目录下的build.gradle 引入公共的gradle
apply from: 'zcw.gradle'
zcw.gradle --->
抽取基本的config
//建立map存储【基本config】
androidID = [
compileSdkVersion : 30,
buildToolsVersion : "30.0.0",
applicationId : "top.zcwfeng.modularprojecct",
minSdkVersion : 21,
targetSdkVersion : 30,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner"
]
抽取appid
appID = [
app : "top.zcwfeng.modularprojecct",
login : "top.zcwfeng.login",
regist: "top.zcwfeng.register"
]
抽取依赖
// 【抽取依赖】 肯能会有很多行
dependenciesID = [
"appcompat" : "androidx.appcompat:appcompat:1.2.0",
"constraintlayout": "androidx.constraintlayout:constraintlayout:2.0.4",
"material" : "com.google.android.material:material:1.2.1",
"vectordrawable" : "androidx.vectordrawable:vectordrawable:1.1.0",
"fragment" : "androidx.navigation:navigation-fragment:2.2.2",
"ui" : "androidx.navigation:navigation-ui:2.2.2",
"extensions" : "androidx.lifecycle:lifecycle-extensions:2.2.0",
]
抽取控制生产环境和测试环境
【不必要在修改代码】如果多个环境我们还可以定义int类型区分
// 正式环境和测试环境
isRelease = true
url = [
debug :"http://192.168.30.111/debug",
release :"https://www.zcwfeng.top/release"
]
--- > 多个模块 测试 能够单独独立运行,生产只能app壳 集成运行,各个module中的配置
if (isRelease) { // 如果是发布版本时,各个模块都不能独立运行
apply plugin: 'com.android.library' // 正式环境 library不能独立运行
} else {
apply plugin: 'com.android.application' // 测试环境 application独立运行
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.0"
defaultConfig {
if(!isRelease) {
applicationId appID.regist// 注册在ext中
}
。。。。
}
--- > 在 app壳 配置交互变量
defaultConfig {
。。。
// 让我的Java代码也可以用
// 给Java代码暴漏,标记,正式环境 和 测试环境 的标记
// 组件化 和 集成化 的时候需要
buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
}
buildTypes {
debug {
buildConfigField("String", "debug", "\"${url.debug}\"")
}
release {
buildConfigField("String", "release", "\"${url.release}\"")
。。。
}
}
java 中通过BuildConfig.isRelease 进行打包切换
--- >在 app壳 里面 eatch 循环 调用依赖
dependenciesID.each{
k,v -> implementation v
}
以上配置,大概可以达到图中效果

我们还可以控制 资源文件和类,但是不建议这么做,不好维护
sourceSets {
main {
if (!isRelease) {
// 如果是组件化模式,需要单独运行时 Debug
manifest.srcFile 'src/main/debug/AndroidManifest.xml' // 生效
} else { // 正式环境下
// 集成化模式,整个项目打包apk
manifest.srcFile 'src/main/AndroidManifest.xml' // 让我们之前 默认的路径下的清单文件再次生效
java {
// release 时 debug 目录下文件不需要合并到主工程
exclude "**/debug/**"
}
}
}
}
-------------------------抽出ext 完整gradle-----------------------------
//Groovy
ext {
// 正式环境和测试环境
isRelease = false
url = [
debug : "http://192.168.30.111/debug",
release: "https://www.zcwfeng.top/release"
]
//建立map存储【基本config】
androidID = [
compileSdkVersion : 30,
buildToolsVersion : "30.0.0",
applicationId : "top.zcwfeng.modularprojecct",
minSdkVersion : 21,
targetSdkVersion : 30,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner"
]
// map 【抽取appid】
appID = [
app : "top.zcwfeng.modularprojecct",
login : "top.zcwfeng.login",
regist: "top.zcwfeng.register"
]
// 【抽取依赖】 肯能会有很多行
dependenciesID = [
"appcompat" : "androidx.appcompat:appcompat:1.2.0",
"constraintlayout": "androidx.constraintlayout:constraintlayout:2.0.4",
"material" : "com.google.android.material:material:1.2.1",
"vectordrawable" : "androidx.vectordrawable:vectordrawable:1.1.0",
"fragment" : "androidx.navigation:navigation-fragment:2.2.2",
"ui" : "androidx.navigation:navigation-ui:2.2.2",
"extensions" : "androidx.lifecycle:lifecycle-extensions:2.2.0",
]
}
我们设计思想是Module 分层从上到下是,每个Module互相是独立的,不能横向产生依赖。
大致的层次图

这几中方式都不是很好
1:方式一 使用 EventBus的方式,缺点是:EventBean维护成本太高,不好去管理:
2:方式二 使用广播的方式,缺点是:不好管理,都统一发出去了
3:方式三 使用隐士意图方式,缺点是:在AndroidManifest.xml里面配置xml写的太多了
4:方式四 使用类加载方式,缺点就是,容易写错包名类名,缺点较少(我们尝试写写这种方式)
5: 方式五 使用全局Map的方式,缺点是,要注册很多的对象(我们尝试写写这种方式)
第三方推荐:CC,ARouter,WMRouter
网友评论