我试图了解如何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
// 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 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.