一、早期的单一分层模式的问题
-
无论怎么分包,随着项目的增大,项目是去层次感。
-
代码耦合度高,包名约束太弱,不同业务包会相互调用。
-
多人开发,容易出现代码覆盖冲突的问题。
二、什么是组件化,组件化的意义。
组件化的意义:不相互依赖,可以相互交互,任意组合,高度解耦,自由拆卸,自由组装,重复利用,分层独立化
三、gradle的东西如何给java用
- 自己创建的my.gradle
ext {
// 现在 Gradle
// 正式环境 和 测试环境
isRelease = false
// 正式环境 和 测试环境 服务器 URL 配置
url = [
"debug" : "https://192.188.22.99/debug",
"release": "https://192.188.22.99/release"
]
// 建立Map存储, key 和 value 都是自定义的
androidID = [
compileSdkVersion : 30,
buildToolsVersion : "30.0.1",
applicationId : "com.derry.derry",
minSdkVersion : 16,
targetSdkVersion : 30,
versionCode : 1,
versionName : "1.0",
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner"
]
// 建立Map存储, key 和 value 都是自定义的
appID = [
app: "com.derry.modularproject",
login: "com.derry.login",
register: "com.derry.register"
]
// 300 行 MAP key value
dependenciesID = [
"appcompat" : "androidx.appcompat:appcompat:1.2.0",
"constraintlayout": "androidx.constraintlayout:constraintlayout:2.0.1",
"material" : "com.google.android.material:material:1.1.0",
"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",
]
}
- 根gradle去引用
// 根目录下的build.gradle 引入 公共的一份 引入过来
apply from : 'my.gradle'
- app.gradle创建
// 完整的方式 性能
def androidID = rootProject.ext.androidID
android {
compileSdkVersion androidID.compileSdkVersion
buildToolsVersion androidID.buildToolsVersion
defaultConfig {
applicationId appID.app
minSdkVersion androidID.minSdkVersion
targetSdkVersion androidID.targetSdkVersion
versionCode androidID.versionCode
versionName androidID.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
// 让我的Java代码也可以用
// 给Java代码暴漏,标记,正式环境 和 测试环境 的标记
// 组件化 和 集成化 的时候需要
buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
}
buildTypes {
debug {
buildConfigField("String", "debug", "\"${url.debug}\"")
}
release {
buildConfigField("String", "release", "\"${url.release}\"")
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
// 300 行
/* implementation "androidx.appcompat:appcompat:1.2.0"
implementation "androidx.constraintlayout:constraintlayout:2.0.1"
implementation "com.google.android.material:material:1.1.0"
implementation "androidx.vectordrawable:vectordrawable:1.1.0"
implementation "androidx.navigation:navigation-fragment:2.2.2"
implementation "androidx.navigation:navigation-ui:2.2.2"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"*/
// 一行搞定300行 循环搞定
dependenciesID.each {k,v -> implementation v}
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
if (isRelease) {
// 依附app壳
implementation project(':login')
implementation project(':register')
} else {
// 不能依附,因为 login register 都能独立运行啊,依附不了,否则报错
}
}
- java使用
BuildConfig.isRelease
四、如何让库可以独立运行和不能独立运行
在单独的模块的build.gradle里
-
对library和application进行切换。
-
对applicationid进行判断设置。
-
对xml的配置启用进行控制
if (isRelease) { // 如果是发布版本时,各个模块都不能独立运行
apply plugin: 'com.android.library' // 正式环境 library不能独立运行
} else {
apply plugin: 'com.android.application' // 测试环境 application独立运行
}
android{
defaultConfig {
// applicationId "" // 有appid 能够独立运行
if (!isRelease) { // 能够独立运行 必须要有appID
applicationId appID.login // 组件化模式能独立运行才能有applicationId
}
minSdkVersion 16
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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/**"
}
}
}
}
}