1 gradle.properties详解
gradle.properties是项目级别的Gradle配置文件。在使用Android Studio新建Android项目之后,在项目根目录下会默认生成一个gradle.properties文件,我们可以在里面做一些Gradle文件的全局性的配置,也可以将比较私密的信息放在里面,防止泄露。
1.1 gradle.properties文件模板
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1536m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
1.2 在各个模块的build.gradle里面直接引用
gradle.properties里面定义的属性是全局的,可以在各个模块的build.gradle里面(app)直接引用,
(1)在gradle.properties文件中新增下面属性:
COMPILE_SDK_VERSION=28
MIN_SDK_VERSION=15
TARGET_SDK_VERSION=28
注意:在gradle.properties中定义的属性默认是String类型的,如果需要int类型,需要添加XXX as int后缀。
(2)在根目录的settings.gradle中引用:
// 输出Gradle对象的一些信息
def printGradleInfoInRoot(){
println "COMPILE_SDK_VERSION settings : " + COMPILE_SDK_VERSION
}
printGradleInfoInRoot()
(3)在app/build.gradle文件中引用:
android {
compileSdkVersion COMPILE_SDK_VERSION as int
defaultConfig {
applicationId "com.tinytongtong.gradle"
minSdkVersion MIN_SDK_VERSION as int
targetSdkVersion TARGET_SDK_VERSION as int
...
}
...
println "COMPILE_SDK_VERSION app : " + compileSdkVersion
}
(4)结果
1.3 另外一种配置全局方法
项目级别的build.gradle中跟目录下配置:
// 设置全局sdk版本号
ext {
compileSdkVersion = 28
buildToolsVersion = "28.0.3"
minSdkVersion = 14
// noinspection ExpiringTargetSdkVersion
targetSdkVersion = 26
}
在各个模块的build.gradle里面使用:
android {
compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion
}