方式一(推荐)
1.在根目录下的build.gradle文件下添加 ext{ …. } 中的内容
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.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{
//SDK
minSdkVersion = 15
targetSdkVersion = 26
compileSdkVersion = 26
//dependencies
supportLibraryVersion ='26.1.0'
butterknifeVersion = '8.8.1'
}
注:在android studio3.0之后,buildToolsVersion去掉了,等号也别忘记了,要不然会报: Gradle DSL method not found: ‘minSdkVersion()’
2.在app目录下build.gradle中使用
apply plugin: 'com.android.application'
android {
compileSdkVersion rootProject.ext.compileSdkVersion
defaultConfig {
applicationId "com.asialyf.zhiqu"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "com.android.support:appcompat-v7:$rootProject.supportLibraryVersion"
implementation "com.android.support:cardview-v7:$rootProject.supportLibraryVersion"
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
//Butterknife
implementation "com.jakewharton:butterknife:$rootProject.butterknifeVersion"
}
注意:引用的依赖使用的是双引号
方式二
1.在项目根目录下创建.gradle文件,如:config.gradle
2.在根目录下的build.gradle文件中引入创建的配置文件
apply from:”config.gradle”
3.最后config.gradle中文件内容可以自己定义
ext {
// 用于编译的SDK版本
COMPILE_SDK_VERSION = 23
// 用于Gradle编译项目的工具版本
BUILD_TOOLS_VERSION = "24.0.2"
// 最低支持Android版本
MIN_SDK_VERSION = 14
// 目标版本
TARGET_SDK_VERSION = 23
APPCOMPAT_VERSION = '23.2.1'
}
4.在app目录下的build.gradle中使用
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile "com.android.support:cardview-v7:${APPCOMPAT_VERSION}"
compile "com.android.support:appcompat-v7:${APPCOMPAT_VERSION}"
compile "com.android.support:design:${APPCOMPAT_VERSION}"
compile 'com.github.bumptech.glide:glide:3.7.0'
}