一、组件化的介绍
组件化的意义:不相互依赖,可以相互交互,任意组合,高度解耦,自由拆卸,自由组装,重复利用,分层独立化。
二、公共的配置可以放到一份单独的gradle文件中去,lgj.gradle
// Groovy 语言
ext {
// 正式环境和测试环境的区分
isRelease = true
// 正式环境 和 测试环境 服务器 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.0",
minSdkVersion : 16,
targetSdkVersion : 30,
versionCode : 1,
versionName : "1.0",
]
appID = [
app: "com.lgj.zujianhua",
login : "com.lgj.login",
register : "com.lgj.register"
]
dependenciesID = [
appcompat : "androidx.appcompat:appcompat:1.2.0",
constraintlayout: "androidx.constraintlayout:constraintlayout:2.0.4"
]
}
项目根目录的gradle需要引用,自己定义的gradle文件,大妈如下
// 引入自己的gradle 文件
apply from: ‘lgj.gradle’
三、APP外壳的gradle配置
apply plugin: 'com.android.application'
// 这句话也可以不写,可以提高性能,如果不写,每次都会从lgj.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 行的依赖
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 都能独立运行否则报错
}
}
四、登录模块的gradle配置
if (isRelease) {
apply plugin: 'com.android.library'// 测试环境
}else {
// 测试环境的话,就需要独立运行
apply plugin: 'com.android.application'
}
android {
compileSdkVersion androidID.compileSdkVersion
buildToolsVersion androidID.buildToolsVersion
defaultConfig {
if (!isRelease) { // 如果不是正式环境就要独立运行,就必须要有APPID
applicationId appID.login // 有appid 是可以独立运行
}
minSdkVersion androidID.minSdkVersion
targetSdkVersion androidID.targetSdkVersion
versionCode androidID.versionCode
versionName androidID.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// 打包过程中源代码进行设置
sourceSets {
main {
if (!isRelease) {
// 如果是组件化,需要单独运行debug 环境
// 可以这么理解,指定编译过程中 需要打包manifest的路径
manifest.srcFile 'src/main/debug/AndroidManifest.xml'
}else {
// 集成化打包,整个项目打包apk
manifest.srcFile 'src/main/AndroidManifest.xml'
// 正式环境下,屏蔽测试代码,不让测试代码打包进apk,避免apk过大
java {
// release 时 debug 目录下文件不需要合并到主工程
exclude "**/debug/**"
}
}
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
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'
}
五、注册模块的gradle配置
if (isRelease) {
apply plugin: 'com.android.library'// 测试环境
}else {
// 测试环境的话,就需要独立运行
apply plugin: 'com.android.application'
}
android {
compileSdkVersion androidID.compileSdkVersion
buildToolsVersion androidID.buildToolsVersion
defaultConfig {
if (!isRelease) { // 如果不是正式环境就要独立运行,就必须要有APPID
applicationId appID.register // 有appid 是可以独立运行
}
minSdkVersion androidID.minSdkVersion
targetSdkVersion androidID.targetSdkVersion
versionCode androidID.versionCode
versionName androidID.versionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
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'
}