我试图了解如何Gradle包装工作。在许多源代码回购中,我看到了以下结构:

projectRoot/
    src/
    build.gradle
    gradle.properties
    settings.gradle
    gradlew
    gradlew.bat
    gradle/
        wrapper/
            gradle-wrapper.jar
            gradle-wrapper.properties

我的问题:

How/when does one generate gradlew/gradlew.bat? Are you supposed to generate them only one time when the project is first created, do you generate them every time you commit/push changes? And how are they generated? Same question above, but for the gradle/wrapper/* files (gradle-wrapper.jar and gradle-wrapper.properties)? Some times I see other *.gradle files inside the project's gradle directory. What are these additional Gradle files and what do they represent/do? Custom plugins? What is the difference in properties that go into settings.gradle vs what should be defined inside gradle.properties?


当前回答

这是用来告诉Gradle升级包装器的命令,这样它就会抓取包含源代码的Gradle jar(只是Gradle,不是库)的分发版本:

./gradlew wrapper --gradle-version <version> --distribution-type all

这现在可以使用Gradle构建本身来完成,改变你的根构建。Gradle包含:

tasks.named('wrapper') {
  distributionType = Wrapper.DistributionType.ALL
  gradleVersion = '7.5'
}

现在升级gradle包装器:只需将代码更改为新版本,并在没有任何参数的情况下运行包装器任务。

用“all”指定分发类型将确保Gradle为自己下载可以被您的开发环境使用的源文件。

优点:

ide可以立即访问Gradle的源代码。例如,Intellij IDEA不会提示您更新构建脚本以包括源发行版(因为这个命令已经做到了这一点)

缺点:

更长的/更大的构建过程,因为它下载Gradle源代码。这是在构建或CI服务器上浪费时间/空间,因为源代码是不必要的(110MB vs 150MB对于Gradle 7.4)。

如果你知道任何命令行选项告诉Gradle不要在构建服务器上下载源代码,请评论或提供其他答案。

其他回答

这是用来告诉Gradle升级包装器的命令,这样它就会抓取包含源代码的Gradle jar(只是Gradle,不是库)的分发版本:

./gradlew wrapper --gradle-version <version> --distribution-type all

这现在可以使用Gradle构建本身来完成,改变你的根构建。Gradle包含:

tasks.named('wrapper') {
  distributionType = Wrapper.DistributionType.ALL
  gradleVersion = '7.5'
}

现在升级gradle包装器:只需将代码更改为新版本,并在没有任何参数的情况下运行包装器任务。

用“all”指定分发类型将确保Gradle为自己下载可以被您的开发环境使用的源文件。

优点:

ide可以立即访问Gradle的源代码。例如,Intellij IDEA不会提示您更新构建脚本以包括源发行版(因为这个命令已经做到了这一点)

缺点:

更长的/更大的构建过程,因为它下载Gradle源代码。这是在构建或CI服务器上浪费时间/空间,因为源代码是不必要的(110MB vs 150MB对于Gradle 7.4)。

如果你知道任何命令行选项告诉Gradle不要在构建服务器上下载源代码,请评论或提供其他答案。

从Gradle 2.4开始,你可以使用Gradle包装器——Gradle version X.X来配置一个特定版本的Gradle包装器,而不需要向你的构建中添加任何任务。gradle文件。下次使用包装器时,它将下载适当的Gradle发行版进行匹配。

You generate it once, and again when you'd like to change the version of Gradle you use in the project. There's no need to generate is so often. Here are the docs. Just add wrapper task to build.gradle file and run this task to get the wrapper structure. Mind that you need to have Gradle installed to generate a wrapper. Great tool for managing g-ecosystem artifacts is SDKMAN!. To generate a gradle wrapper, add the following piece of code to build.gradle file: task wrapper(type: Wrapper) { gradleVersion = '2.0' //version required } and run: gradle wrapper task. Add the resulting files to SCM (e.g. git) and from now all developers will have the same version of Gradle when using Gradle Wrapper. With Gradle 2.4 (or higher) you can set up a wrapper without adding a dedicated task: gradle wrapper --gradle-version 2.3 or gradle wrapper --gradle-distribution-url https://myEnterpriseRepository:7070/gradle/distributions/gradle-2.3-bin.zip All the details can be found here

在Gradle 3.1中,也可以使用distribution-type选项。选项包括binary、all和bin。所有额外包含源代码和文档。当使用IDE时,all也更好,因此编辑器工作得更好。缺点是构建可能会持续更长的时间(需要下载更多的数据,在CI服务器上毫无意义),它将占用更多的空间。

These are Gradle Wrapper files. You need to generate them once (for a particular version) and add to version control. If you need to change the version of Gradle Wrapper, change the version in build.gradle see (1.) and regenerate the files. Give a detailed example. Such file may have multiple purposes: multi-module project, responsibility separation, slightly modified script, etc. settings.gradle is responsible rather for structure of the project (modules, names, etc), while, gradle.properties is used for project's and Gradle's external details (version, command line arguments -XX, properties etc.)

生成Gradle包装器

项目构建gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.

// Running 'gradle wrapper' will generate gradlew - Getting gradle wrapper working and using it will save you a lot of pain.
task wrapper(type: Wrapper) {
    gradleVersion = '2.2' 
}

// Look Google doesn't use Maven Central, they use jcenter now.
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

然后在命令行运行

gradle wrapper

如果你在你的系统上缺少gradle,安装它,否则上面的将不起作用。在Mac上最好通过Homebrew安装。

brew install gradle

在成功运行包装器任务并生成gradlew之后,不要使用您的系统gradle。这会帮你省去很多头疼的事情。

./gradlew assemble

上面看到的gradle插件怎么样?

com.android.tools.build:gradle:1.0.1

您应该将版本设置为最新版本,您可以检查工具页面并相应地编辑版本。

看看Android Studio会生成什么

gradle和最新的Android Studio的加入极大地改变了项目的布局。如果你有一个较旧的项目,我强烈建议你用最新的Android Studio创建一个干净的项目,看看谷歌认为标准项目是什么。

Android Studio有导入旧项目的功能,这也有帮助。

You will generate them once, but update them if you need a new feature or something from a plugin which in turn needs a newer gradle version. Easiest way to update: as of Gradle 2.2 you can just download and extract the complete or binary Gradle distribution, and run: $ <pathToExpandedZip>/bin/gradle wrapper No need to define a task, though you probably need some kind of build.gradle file. This will update or create the gradlew and gradlew.bat wrapper as well as gradle/wrapper/gradle-wrapper.properties and the gradle-wrapper.jar to provide the current version of gradle, wrapped. Those are all part of the wrapper. Some build.gradle files reference other files or files in subdirectories which are sub projects or modules. It gets a bit complicated, but if you have one project you basically need the one file. settings.gradle handles project, module and other kinds of names and settings, gradle.properties configures resusable variables for your gradle files if you like and you feel they would be clearer that way.