通过全局 gradle 配置所有依赖 每个模块不需要单独引用单独依赖版本
1.首先建立 全局配置文件 config.gralde
配置内容 全局
configVersion 配置各sdk版本号 app版本
configDependenciesVersion 配置依赖版本号
configDependencies 配置依赖
下面是config.gradle
ext{
//全局配置 可以如果单独需要配置某个模块 根据模块名字建立对应的 configVersion
//可以单独配置某个模块的 applicationId
configVersion = [
"buildToolsVersion": "26.0.2",
"compileSdkVersion": 26,
"minSdkVersion" : 16,
"targetSdkVersion" : 26,
"versionCode" : 10,
"versionName" : "1.0.0",
"applicationId" : "com.echronos.maventest"
]
//这里用来配置某个依赖的版本号
configDependenciesVersion = [
appcompatVersion:'1.3.1',
materialVesion:'1.4.0',
constraintlayoutVersion:'2.1.0'
]
//全局依赖 不允许单独在模块内部引用某个第三方库 会导致混乱
configDependencies = [
appcompat : 'androidx.appcompat:appcompat:$rootProject.appcompatVersion',
material : 'com.google.android.material:material:$rootProject.materialVesion',
constraintlayout : 'androidx.constraintlayout:constraintlayout:$rootProject.constraintlayoutVersion',
]
}
配置完成后 在 project的 build.gardle 添加 apply from: "config.gradle" 引用插件
buildscript {
repositories {
google()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:7.0.0"
}
}
//配置全局 config.gradle 所有module通过config.gradle配置 同步所有module配置版本 不允许单独配置某个module版本 避免某个依赖的版本冲突
apply from: "config.gradle"
task clean(type: Delete) {
delete rootProject.buildDir
}
接下来是在app和module中引用的形式
plugins {
id 'com.android.application'
}
android {
//这里配置各种版本号以及name
compileSdk rootProject.ext.configVersion["compileSdkVersion"]
defaultConfig {
applicationId rootProject.ext.configVersion["applicationId"]
minSdk rootProject.ext.configVersion["minSdkVersion"]
targetSdk rootProject.ext.configVersion["targetSdkVersion"]
versionCode rootProject.ext.configVersion["versionCode"]
versionName rootProject.ext.configVersion["versionName"]
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
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 {
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
//这里直接一键引入所有依赖库
rootProject.ext.configDependencies.each { k, v -> implementation v }
}
可以方便的解决module多的情况下 依赖各种重复引用 配置一份文件 所有module基于一个配置文件可以引用全部 不会引起依赖冲突等情况
后续会接着写 组件化+maven+自动化构建的流程