Gradle系列(四)-扩展属性
本篇文章主要介绍下gradle中的扩展属性.
扩展属性可以用于在构建脚本中定义自定义的属性,以便在整个项目中共享和使用。
1: ext扩展属性
根目录下新建dependencies.gradle. 添加内容如下:
ext {
androidClassPath = “com.android.tools.build:gradle:4.1.2”
androidCompileSdkVersion = 28
androidApplicationId = ‘com.zh.xpose’
androidminSdkVersion = 16
androidtargetSdkVersion = 28
androidVersionName = “1.0”
androidVersionCode = 1
}
1.1 当前文件下的任务调用
我们在dependencies.gradle下新建任务testExt.
task testExt{
println(“1:
a
n
d
r
o
i
d
C
l
a
s
s
P
a
t
h
"
)
p
r
i
n
t
l
n
(
"
2
:
{androidClassPath}") println("2:
androidClassPath")println("2:{project.ext.androidClassPath}”)
}
如上述代码所示访问方式:
-
直接访问扩展属性
-
通过 ext 对象访问扩展属性
1.2: 根目录build.gradle中调用
首先我们在build.gradle中引入dependencies.gradle,并替换classpath.
具体的代码如下:
buildscript {
apply from:'dependencies.gradle'
repositories {
google()
jcenter()
}
dependencies {
classpath project.ext.androidClassPath
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
这里也是可以直接调用扩展属性的.
也就是我们可以将
classpath project.ext.androidClassPath
替换为:
classpath androidClassPath
1.3 : module的build.gralde中调用.
我们在dependencies.gradle中定义了versioncode,versionname等属性,那如何调用呢?
android {
def ext = rootProject.extensions.getByName("ext")
compileSdkVersion ext.androidCompileSdkVersion
defaultConfig {
applicationId ext.androidApplicationId
minSdkVersion ext.androidCompileSdkVersion
targetSdkVersion ext.androidCompileSdkVersion
versionCode ext.androidCompileSdkVersion
versionName ext.androidVersionName
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
...
}
如上述代码:
我们可以通过rootProject.extensions.getByName(“ext”)来获取项目的扩展属性,通过ext.androidCompileSdkVersion调用我们在ext下定义的各种扩展属性.
2: gradle.properties定义属性
另外我们还可以在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=-Xmx2048m -Dfile.encoding=UTF-8
# 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
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app"s APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
test = "hello world"
如上所示,我们定义test属性.
这里定义的属性我们是可以直接调用的
-
dependencies.gradle中我们添加打印:
task testExt{ println("dependencies:"+test) // println("1:${androidClassPath}") // println("2:${project.ext.androidClassPath}") }
-
根目录的build.gradle中调用
dependencies { println "根目录build.gradle:"+test classpath androidClassPath // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
-
subprojects下调用:
android { println("subprojects :"+test) def ext = rootProject.extensions.getByName("ext") compileSdkVersion ext.androidCompileSdkVersion defaultConfig { applicationId ext.androidApplicationId minSdkVersion ext.androidCompileSdkVersion targetSdkVersion ext.androidCompileSdkVersion versionCode ext.androidCompileSdkVersion versionName ext.androidVersionName testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } ... }
编译输出如下:
> Configure project :
> dependencies:"hello world"
> 根目录build.gradle:"hello world"
> Configure project :app
> subprojects :"hello world"
> Task :prepareKotlinBuildScriptModel UP-TO-DATE
BUILD SUCCESSFUL in 319ms