最近做了个项目涉及到多个模块化设计,为了保证build.gradle 的统一管理,做了相应的配置方便修改。
一、首先在项目中创建一个config.gradle文件
在里面写android配置及依赖配置如下:
ext{
//android 配置
android =[
compileSdkVersion : 29,
buildToolsVersion :"29.0.3",
applicationId : "com.bin.meet",
minSdkVersion : 19,
targetSdkVersion :29,
versionCode : 1,
versionName :"1.0"
]
//依赖配置
dependencies = [
"httpclient" : 'org.apache.http.legacy',
"appcompat": 'androidx.appcompat:appcompat:1.1.0',
"eventbus" : 'org.greenrobot:eventbus:3.1.1',
"tablayout" : 'com.google.android.material:material:1.0.0',
]
//创库
maven = [
"bmobUrl" : "https://raw.github.com/bmob/bmob-android-sdk/master"
]
}
二、在整个项目的配置文件中引用刚自定义的config.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
//引入自定义Gradle配置文件
apply from: "config.gradle"
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven{
url rootProject.ext.maven["bmobUrl"]
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
三、在核心代码库中应用依赖如下
apply plugin: 'com.android.library'
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
useLibrary rootProject.ext.dependencies["httpclient"]
defaultConfig {
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
}
buildTypes {
debug{
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation rootProject.ext.dependencies["appcompat"]
api rootProject.ext.dependencies["appcompat"]
api rootProject.ext.dependencies["eventbus"]
api rootProject.ext.dependencies["tablayout"]
api project(path: ':IMLib')
}
其中要用api 依赖,以便其他模块都可以正常编译,否则编译出错。
四、在 app module 中添加以下依赖即可
dependencies {
implementation fileTree(dir: ‘libs’, include: [’*.jar’])
implementation rootProject.ext.dependencies[“appcompat”]
implementation ‘androidx.constraintlayout:constraintlayout:1.1.3’
implementation ‘androidx.appcompat:appcompat:1.1.0’
testImplementation ‘junit:junit:4.12’
androidTestImplementation ‘androidx.test.ext:junit:1.1.1’
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.2.0’
implementation project(path: ‘:framework’) //添加framework依赖
**这样做的目的就是方便api 修改,不需要大量修改文件,节约项目的开发时间和维护的成本。