我直接放出我github主要代码链接吧 下面的废话是给我自己看的 简单纪录下
整个项目代码
config.gradle全部代码
build.gradle全部代码
1.创建config.gradle 详情见github
[]就是一个map集合
//
ext {
//生产 /开发 环境
isRelease = true
androidId = [
compileSdkVersion: 29,
buildToolsVersion: "29.0.3",
applicationId : "com.cw.myapplication",
minSdkVersion : 19,
targetSdkVersion : 29,
versionCode : 1,
versionName : "1.0"
]
appId = [
applicationId: "com.cw.myapplication",
]
//生产 测试环境
url = [
debug : "http://101.2.2.2:debug",
release: "http://101.2.2.20:release",
]
appcompatLibVersion = "1.1.0"
coreKtxLibVersion = "1.2.0"
constraintlayoutLibVersion = "1.1.3"
junitLibVersion = "4.12"
extJunitLibVersion = "1.1.1"
espressoCoreLibVersion = "3.2.0"
dependenciesLib = [
"appcompat" : "androidx.appcompat:appcompat:${appcompatLibVersion}",
"coreKtx" : "androidx.core:core-ktx:${coreKtxLibVersion}",
"constraintlayout": "androidx.constraintlayout:constraintlayout:${constraintlayoutLibVersion}",
"junit" : "junit:junit:${junitLibVersion}",
"extJunit" : "androidx.test.ext:junit:${extJunitLibVersion}",
"espressoCore" : "androidx.test.espresso:espresso-core:${espressoCoreLibVersion}"
]
}
2.在根目录下的build.gradle中依赖config.gradle 详情点击github
apply from: "config.gradle"
buildscript {
ext.kotlin_version = '1.3.50'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.3'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
3.赋值引用 详情点击github
//赋值引用
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def dependenciesLib = rootProject.ext.dependenciesLib
def url = rootProject.ext.url
具体怎么使用config.gradle里的参数 可以去github下载源码 看看
下面的代码简单说一下 我们可以通过如下代码片段 依赖指定的库
implementation dependenciesLib.appcompat
也可以通过最简洁的方式 遍历dependenciesLib集合 前面说过这个在config.gradle中定义的就是个集合 既然是集合 我们肯定可以去循环遍历他 如:
//最简洁的方式 只需要如下代码 就依赖了全部的三方库
dependenciesLib.each { k, v -> implementation v }
以下是全部代码
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
testImplementation dependenciesLib.junit
androidTestImplementation dependenciesLib.extJunit
androidTestImplementation dependenciesLib.espressoCore
/*implementation dependenciesLib.appcompat
implementation dependenciesLib.coreKtx
implementation dependenciesLib.constraintlayout*/
//最简洁的方式
dependenciesLib.each { k, v -> implementation v }
implementation project(":library")
}