Gradle系列(四)-扩展属性

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}”)
}

如上述代码所示访问方式:

  1. 直接访问扩展属性

  2. 通过 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属性.

这里定义的属性我们是可以直接调用的

  1. dependencies.gradle中我们添加打印:

    task testExt{
        println("dependencies:"+test)
    //    println("1:${androidClassPath}")
    //    println("2:${project.ext.androidClassPath}")
    }
    
  2. 根目录的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
    }
    
  3. 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
  • 18
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值