我试图了解如何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有导入旧项目的功能,这也有帮助。
这是用来告诉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不要在构建服务器上下载源代码,请评论或提供其他答案。
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.