我是Gradle的新手,我正在阅读文档,但我不理解其中的一些部分。其中一个部分与buildscript块连接。它的目的是什么?

如果构建脚本需要使用外部库,可以将它们添加到构建脚本本身的脚本类路径中。您可以使用buildscript()方法,传入一个声明构建脚本类路径的闭包。 buildscript { 存储库{ mavenCentral () } 依赖关系{ 类路径组:'common -codec',名称:'common -codec',版本:'1.2' } }

好的,但是区别是什么呢:

repositories {
  mavenCentral()
}
dependencies {
  compile group: 'commons-codec', name: 'commons-codec', version: '1.2'
}

例如,为什么需要使用buildscript?


当前回答

我相信buildscript{}中的所有内容都可以用于当前的构建脚本本身以及它的所有子项目。

对于在buildscript{}之外的文件中声明的属性,它不会立即对给定项目本身的buildscript可用,而是对其所有子项目可用。

So if you want to declare something and use it for the buildscript itself right away (current buildscript and not just subproject's buildscript), declare them in the buildscript {} for the current project and it also has the side effect to let subproject use it later on. If you just want to declare something globally (for sub-projects's buildscripts) you can declare them directly as ext {} in parent project. The parent project won't be able to use them for itself's buildscript but it will be available all the subproject to use, in or out of the buildscript clause.

例如在父项目中:

ext {
    kotlin_version_XX = '1.7.10' 
}

buildscript {
    ext {
      kotlin_version = '1.7.10' 
    }
    // kotlin_version will be available here since declared in buildscript{}
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 

    // will NOT be available here -- error due to kotlin_version_XX declared in project 
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version_XX" 
}

如果你有一个子项目:


dependencies {
   
    // both kotlin_version and kotlin_version_XX can be used here, since it was declared in parent project
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version_XX"
}

buildscript {
    
    // both kotlin_version and kotlin_version_XX can even be used here for subproject's script's use, since it was already declared in parent project
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version_XX"
}

其他回答

通过演示Android顶级gradle文件来做更多的解释。

buildscript {
    // this is where we are going to find the libraries defined in "dependencies block" at below
    repositories {
        google()
        jcenter()
        maven { url 'https://dl.bintray.com/kotlin/kotlin-eap' }
    }

    // everything listed in the dependencies is actually a plugin, which we'll do "apply plugin" in our module level gradle file.
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2' // this is android gradle plugin
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // kotlin gradle plugin
    }
}

模块级gradle文件

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

什么是“插件”?它们只是实现Plugin接口的java类。在接口下,它有一个方法“apply”,用于添加多个不同名称的任务对象。Task是一个类,我们可以在其中实现工作流。例如,构建任务由构建应用程序的流程组成。

那么,buildscript是做什么的呢?它定义了在哪里找到插件。插件是做什么的?它包含多个任务。task是做什么的?它为我们提供了构建、安装、lint等。

我的理解可能错了。如果你发现任何误导,请不要犹豫纠正我。

“buildscript”配置部分是针对gradle本身的(即更改gradle如何能够执行构建)。本节通常包括Android Gradle插件。

这有点高,但希望会有所帮助。

对我来说,一旦我开始理解什么是构建块、方法和任务,清晰的区别就开始形成了。语法如何,如何配置等等。我建议你们把这些都看一遍。在此之后,您可以开始理解这种语法。

那么,了解对象构建的类型是非常重要的。gradle (Project类的一个实例),以便了解在构建中可以有什么。gradle文件。这将回答'buildScript'和其他来自哪里。为了扩展你的功能/特性(比如android),看看插件是如何帮助你的。

最后但并非最不重要的是,这里有一个非常好的教程,讨论了闭包,委托,这是理解脚本所必需的概念。

全局级依赖项和存储库部分列出了构建源代码和运行源代码等所需的依赖项。 构建脚本是用于构建的。Gradle文件本身。因此,这将包含创建rpm的依赖项,Dockerfile,以及在所有依赖build.gradle中运行任务的任何其他依赖项

我相信buildscript{}中的所有内容都可以用于当前的构建脚本本身以及它的所有子项目。

对于在buildscript{}之外的文件中声明的属性,它不会立即对给定项目本身的buildscript可用,而是对其所有子项目可用。

So if you want to declare something and use it for the buildscript itself right away (current buildscript and not just subproject's buildscript), declare them in the buildscript {} for the current project and it also has the side effect to let subproject use it later on. If you just want to declare something globally (for sub-projects's buildscripts) you can declare them directly as ext {} in parent project. The parent project won't be able to use them for itself's buildscript but it will be available all the subproject to use, in or out of the buildscript clause.

例如在父项目中:

ext {
    kotlin_version_XX = '1.7.10' 
}

buildscript {
    ext {
      kotlin_version = '1.7.10' 
    }
    // kotlin_version will be available here since declared in buildscript{}
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" 

    // will NOT be available here -- error due to kotlin_version_XX declared in project 
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version_XX" 
}

如果你有一个子项目:


dependencies {
   
    // both kotlin_version and kotlin_version_XX can be used here, since it was declared in parent project
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    implementation "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version_XX"
}

buildscript {
    
    // both kotlin_version and kotlin_version_XX can even be used here for subproject's script's use, since it was already declared in parent project
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version_XX"
}