Grade 全局参数定义
实际开发中设置公共的编译依赖参数等。
方法一:
在项目外层的build.gradle文件中定义,格式如下:
文件名:build.gradle
ext {
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
compileSdkVersion = 25
buildToolsVersion = ‘25.0.3’
minSdkVersion = 19
targetSdkVersion = 26
supportVersion = ‘25.3.1’
supportV7CardView = “com.android.support:cardview-v7:${supportVersion}”
supportV4 = “com.android.support:support-v4:${supportVersion}”
supportFragment = “com.android.support:support-fragment:${supportVersion}”
…
}
在各个module的build.grade文件中获取以上定义变量方法为:
rootProject.ext.supportDesign
rootProject.ext.supportFragment
rootProject.ext.supportAnnotations
rootProject.ext.supportV7AppCompat
rootProject.ext.supportV7RecyclerView
rootProject.ext.supportV7CardView
方法二:
在gradle.properties中定义整个工程的全局gradle变量
在gradle.properties中定义如下变量
ANDROID_BUILD_MIN_SDK_VERSION=15
ANDROID_BUILD_TARGET_SDK_VERSION=23
ANDROID_BUILD_SDK_VERSION=23
ANDROID_BUILD_TOOLS_VERSION=23.0.2
在所有build.gradle中可以这样使用:
compileSdkVersion Integer.parseInt(project.ANDROID_BUILD_SDK_VERSION)
buildToolsVersion project.ANDROID_BUILD_TOOLS_VERSION
方法三:
可以在根目录下建个config.gradle:
config.gradle文件内容如下:
ext {
android = [
compileSdkVersion: 23,
buildToolsVersion: “23.0.2”,
minSdkVersion : 14,
targetSdkVersion : 22,
]
dependencies = [
appcompatV7′: ‘com.android.support:appcompat-v7:23.2.1’,
design : ‘com.android.support:design:23.2.1’
]
}
然后只需在根目录下build.gradle最顶部加上下面一行代码,然后同步下,意思就是所有的子项目或者所有的modules都可以从这个配置文件里读取内容。
apply from: “config.gradle”
在app/build.gradle中使用调用如下:
android {
compileSdkVersion rootProject.ext.android.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
defaultConfig {
applicationId “com.example.android”
minSdkVersion rootProject.ext.android.minSdkVersion
targetSdkVersion rootProject.ext.android.targetSdkVersion
versionCode 1
versionName “1.0”
}
…
dependencies {
compile fileTree(dir: ‘libs’, include: [‘*.jar’])
testCompile ‘junit:junit:4.12’
compile rootProject.ext.dependencies.appcompatV7
compile rootProject.ext.dependencies.design
}