前言
當(dāng)一個(gè)project下有多個(gè)module時(shí)管削,全局依賴參數(shù)可以統(tǒng)一起來(lái),修改時(shí)只需修改一處就行了饲做,比較方便。當(dāng)然如果格式你看著不爽,可以不這樣寫,畢竟現(xiàn)在是社會(huì)主義社會(huì)嘛唇敞,選擇自由。
配置
1.只想配置defaultConfig的情況和google系統(tǒng)的自帶庫(kù)
首先在項(xiàng)目的根build.gradle下配置ext
ext{
compileSdkVersion =25
buildToolsVersion= '25.0.2'
minSdkVersion = 16
targetSdkVersion = compileSdkVersion
versionCode =1
versionName ='1.0.0'
supportLibVersion = '25.3.1'
}
然后在app/build.gradle下修改defaultConfig
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId "com.xxx"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
}
dependencies {
compile "com.android.support:design:$supportLibVersion"
compile "com.android.support:cardview-v7:$supportLibVersion"
}
2.想配置defaultConfig和所有依賴庫(kù)的情況
在項(xiàng)目的根目錄下創(chuàng)建config.gradle文件
ext{
android=[
applicationId:"com.fsp.aiyagirl",
compileSdkVersion :25,
buildToolsVersion :"25.0.2",
minSdkVersion : 16,
targetSdkVersion : 25,
versionCode : 1,
versionName :"1.0.0"
]
dependencies =[
"appcompat-v7" : "com.android.support:appcompat-v7:25.3.1",
"constraint" : "com.android.support.constraint:constraint-layout:1.0.2",
]
}
然后在根的build.gradle中引用
apply from: "config.gradle"
這樣工程的所有module都能用這個(gè)文件的參數(shù)
最后在app/build.gradle中
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.android.buildToolsVersion
defaultConfig {
applicationId rootProject.ext.android.applicationId
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode rootProject.ext.android.versionCode
versionName rootProject.ext.android.versionName
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile rootProject.ext.dependencies["appcompat-v7"]
compile rootProject.ext.dependencies["constraint"]
}
至此输吏,配置工作已經(jīng)全部結(jié)束它浅,有不足之處可以提出來(lái)共同探討。