美文网首页APP & program
build.gradle 配置项目全局属性

build.gradle 配置项目全局属性

作者: 勤劳的蚂蚁 | 来源:发表于2020-12-09 17:53 被阅读0次

google 文档说明 对应地址: https://developer.android.com/studio/build
ext :extension 扩展,延期

顶层构建文件-配置项目全局属性

对于包含多个模块的 Android 项目,可能有必要在项目级别定义某些属性并在所有模块之间共享这些属性。为此,您可以将额外的属性添加到顶层 build.gradle 文件内的 ext 代码块中。

//定义:
buildscript {...}

allprojects {...}

// This block encapsulates custom properties and makes them available to all
// modules in the project.
翻译后得:
//此块封装通用行为并使得在项目中所有modules 可见(可获得,可使用)。

ext {
    // The following are only a few examples of the types of properties you can define.
翻译后得:
以下仅是您可以定义的属性类型的几个示例。

    compileSdkVersion = 28
    // You can also create properties to specify versions for dependencies.
    // Having consistent versions between modules can avoid conflicts with behavior.
翻译后得:
您还可以创建属性以指定依赖项的版本
模块之间具有一致的版本可以避免与行为发生冲突。
    supportLibVersion = "28.0.0"
    ...
}
...

如需从同一项目中的模块访问这些属性,请在该模块的 build.gradle 文件(您可以在下一部分中详细了解此文件)中使用以下语法。
使用:

android {
  // Use the following syntax to access properties you defined at the project level:
翻译后得:
使用以下语法访问您在项目级别定义的属性:
  // rootProject.ext.property_name

  compileSdkVersion rootProject.ext.compileSdkVersion
  ...
}
...
dependencies {
    implementation "com.android.support:appcompat-v7:${rootProject.ext.supportLibVersion}"
    ...
}

注意:虽然 Gradle 可让您在模块级别定义项目全局属性,但您应避免这样做,因为这样会导致共享这些属性的模块进行耦合。模块耦合使得以后将模块作为独立项目导出更加困难,并实际妨碍 Gradle 利用并行项目执行加快多模块构建。

相关文章

网友评论

    本文标题:build.gradle 配置项目全局属性

    本文链接:https://www.haomeiwen.com/subject/hhohgktx.html