由于组件化实践中模块比较多,因此配置gradle,添加依赖库时,需要考虑简化工作。
第一步:在Project目录下点击鼠标右键,依次new——>File
第二步:然后输入统一配置文件的名称,以.gradle结尾
第三步:在新建的统一配置文件fjs.gradle里面配置要管理的项,代码如下所示
ext{
isApplication = false //false:作为Lib组件存在, true:作为application存在
android = [
compileSdkVersion :28,
buildToolsVersion :"29.0.0",
minSdkVersion :15,
targetSdkVersion :28,
versionCode :1,
versionName :"1.0"//必须是int或者float,否则影响线上升级
]
version = [
androidSupportSdkVersion:"28.0.0",
]
dependencies = [
"appcompat-v7":"com.android.support:appcompat-v7:${version["androidSupportSdkVersion"]}",
]
}
第四步:在project目录下的build.gradle里添加下面一句话
apply from: "fjs.gradle"
build.gradle完整代码如下所示:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
apply from: "fjs.gradle"
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
第五步:在模块里使用ext来统一配置,如在app目录下的build.gradle里面使用,代码如下:
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.android["compileSdkVersion"]
buildToolsVersion rootProject.ext.android["buildToolsVersion"]
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion rootProject.ext.android["minSdkVersion"]
targetSdkVersion rootProject.ext.android["targetSdkVersion"]
versionCode rootProject.ext.android["versionCode"]
versionName rootProject.ext.android["versionName"]
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
api rootProject.ext.dependencies["appcompat-v7"]
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
最后点击Sync Now
结果如下所示:
统一配置文件是为了让各个模块的各个参数达到一致,利于以后统一修改、管理。