Android gradle build: how to set global variables

后端 未结 4 1237
不思量自难忘°
不思量自难忘° 2020-12-13 16:57

How can I set a global variable that can be accessed from build.gradle and tasks?

4条回答
  •  遥遥无期
    2020-12-13 17:25

    To set a global variable

    project.ext.set("variableName", value)
    

    To access it from anywhere in the project:

    project.variableName
    

    For instance:

    project.ext.set("newVersionName", versionString)
    

    and then...

    println project.newVersionName
    

    For more information see: http://www.gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html

    EDIT: As commented by Dmitry, In new versions you can use the following shorthand:

    project.ext.variableName = value
    

提交回复
热议问题