我直接放出我github主要代碼鏈接吧 下面的廢話是給我自己看的 簡單紀錄下
整個項目代碼
config.gradle全部代碼
build.gradle全部代碼
1.創(chuàng)建config.gradle 詳情見github
[]就是一個map集合
//
ext {
//生產(chǎn) /開發(fā) 環(huán)境
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",
]
//生產(chǎn) 測試環(huán)境
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里的參數(shù) 可以去github下載源碼 看看
下面的代碼簡單說一下 我們可以通過如下代碼片段 依賴指定的庫
implementation dependenciesLib.appcompat
也可以通過最簡潔的方式 遍歷dependenciesLib集合 前面說過這個在config.gradle中定義的就是個集合 既然是集合 我們肯定可以去循環(huán)遍歷他 如:
//最簡潔的方式 只需要如下代碼 就依賴了全部的三方庫
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")
}