兼容性
执行 Gradle 需要 8 到 23 之间的 Java 版本。尚不支持 Java 24及更高版本。
Java 6 和 7 可用于编译,但不推荐用于测试。 Gradle 9.0 不支持使用 Java 6 和 7 进行测试。
任何完全支持的 Java 版本都可以用于编译或测试。但是,最新的 Java 版本可能仅支持编译或测试,而不支持运行 Gradle。支持是使用工具链实现的,并适用于支持工具链的所有任务。
请参阅下表了解特定 Gradle 版本支持的 Java 版本:
Java版本 | 支持的工具链 | 支持的Gradle的版本 |
---|---|---|
8 | N/A | 2.0 |
9 | N/A | 4.3 |
10 | N/A | 4.7 |
11 | N/A | 5.0 |
12 | N/A | 5.4 |
13 | N/A | 6.0 |
14 | N/A | 6.3 |
15 | 6.7 | 6.7 |
16 | 7.0 | 7.0 |
17 | 7.3 | 7.3 |
18 | 7.5 | 7.5 |
19 | 7.6 | 7.6 |
20 | 8.1 | 8.3 |
21 | 8.4 | 8.5 |
22 | 8.7 | 8.8 |
23 | 8.10 | 8.10 |
24 | N/A | N/A |
安卓插件对应的gradle的版本
Gradle 使用 Android Gradle 插件 7.3 到 8.4 进行了测试。 Alpha 和 Beta 版本可能有效,也可能无效。
插件版本 | 所需的最低 Gradle 版本 |
---|---|
8.7 | 8.9 |
8.6 | 8.7 |
8.5 | 8.7 |
8.4 | 8.6 |
8.3 | 8.4 |
8.2 | 8.2 |
8.1 | 8.0 |
8.0 | 8.0 |
7.4 | 7.5 |
修改maven下载源
一劳永逸:在init.d文件夹中添加下方内容,也可在项目的bulid.gradle
initscript { // 这是初始脚本特有的
repositories {
maven {
url 'https://maven.aliyun.com/repository/public'
}
mavenCentral()
google()
}
}
allprojects {
repositories {
maven {
url 'https://maven.aliyun.com/repository/public'
}
mavenCentral()
google()
}
}
使用gradlew构建
要满足前提条件
$ gradlew build // Linux or OSX
$ gradlew.bat build // Windows
Maven和Gradle的对应
依赖导入
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.12</version>
</dependency>
</dependencies>
dependencies {
implementation 'log4j:log4j:1.2.12'
// 第一个是范围也就是什么时候该依赖存在
// 第二个对应maven中的坐标
}
- implementation:编译时依赖,不会传递给依赖该项目的其他项目。
- api:编译时依赖,会传递给依赖该项目的其他项目。
- runtimeOnly:运行时依赖,仅在运行时需要。
- testImplementation:测试编译时依赖,仅在测试编译和运行时需要。
- testRuntimeOnly:测试运行时依赖,仅在测试运行时需要。
配置文件中的变量
<properties>
<spring.boot.version>2.5.4</spring.boot.version>
<h2.database.version>1.4.200</h2.database.version>
</properties>
ext { // 要在这个块内定义变量
springBootVersion = '2.5.4'
h2DatabaseVersion = '1.4.200'
}
多模块
<modules>
<module>simple-weather</module>
<module>simple-webapp</module>
</modules>
该文件在
settings.gradle
文件中
rootProject.name = 'simple-multi-module'
// 父模块名称,也就是在父级中的setting.gradle中文件定义的
// 每个项目都有这个,定义当前的项目名称
include 'simple-weather', 'simple-webapp'
// 子模块,后面跟着的就是对应子模块中settings.gradle中写的
// rootProject.name对应的名字
settings.gradle
设置文件是每个 Gradle 项目的入口点。
设置文件的主要目的是将子项目添加到您的构建中。
Gradle 支持单项目和多项目构建。
- 对于单项目构建,设置文件是可选的。
- 对于多项目构建,设置文件是必需的并声明所有子项目。
// 该pluginManagement{}块可以在文件中
// 使用settings.gradle(.kts),它必须是文件中的第一个块:
pluginManagement { (1)
application version "1.8.22"
// 声明插件名和版本,构建文件中就可以使用id不用加版本号
repositories {(2)
gradlePluginPortal()
google()
}
}
plugins { (3)
id("org.gradle.toolchains.fake") version "0.6.0"
}
rootProject.name = "root-project" (4)
dependencyResolutionManagement { (5)
repositories {
mavenCentral()
}
}
include("sub-project-a") (6)
include("sub-project-b")
include("sub-project-c")
(1)当前项目的插件
(2)插件对应的库,这里可以换成镜像源
(3)顶层项目的插件应用,设置的插件仅影响Settings
对象。
(4)当前项目名称
(5)依赖管理,根上面的插件管理一样
(6)子项目
build.gradle
每个 Gradle 构建至少包含一个构建脚本,在该文件中对应的项目
会变成project对象
在构建文件中,可以添加两种类型的依赖项:
- Gradle 和构建脚本所依赖的库和/或插件。
- 项目源(即源代码)所依赖的库。
plugins {
id 'application' // 添加插件
//id 插件名 version 版本, 有些是不用加版本的,如核心插件gradle自带的,就像上面那个
// 插件的仓库是在settings.gralde文件中指定的
}
repositories { // 依赖仓库
mavenCentral()
}
application {
mainClass = 'com.example.Main' // 插件的配置
}
dependencies { // 依赖
testImplementation("org.jetbrains.kotlin:kotlin-test-junit5")
}
buildscript{}
使用块应用插件:貌似这样写的子项目也能用,不在这块里的只是当前项目
buildscript {
repositories { // this is where the plugins are located
mavenCentral()
google()
}
dependencies { // these are the plugins that can be used in subprojects or in the build file itself
classpath group: 'commons-codec', name: 'commons-codec', version: '1.2' // used in the task below
classpath 'com.android.tools.build:gradle:4.1.0' // used in subproject
}
}
还有一种添加插件和依赖的方式
这种需要先创建一个libs.versions.toml文件,位于gradle/libs.versions.toml
假设该文件中有如下内容
[versions]
androidGradlePlugin = "7.4.1"
mockito = "2.16.0"
[libraries]
googleMaterial = { group = "com.google.android.material", name = "material", version = "1.1.0-alpha05" }
mockitoCore = { module = "org.mockito:mockito-core", version.ref = "mockito" }
[plugins]
androidApplication = { id = "com.android.application", version.ref = "androidGradlePlugin" }
那么可以这样添加插件和依赖,libs
代指toml文件
plugins {
alias(libs.plugins.androidApplication)
}
dependencies {
// Dependency on a remote binary to compile and run the code
implementation(libs.googleMaterial)
// Dependency on a remote binary to compile and run the test code
testImplementation(libs.mockitoCore)
}
这玩意就是使用类似变量一样的东西,不同的是通过libs这个对象引用
libs.versions.toml文件结构
[versions]
部分用于定义版本号。这些版本号可以在libraries
和plugins
部分中引用。[versions] kotlin = "1.8.0" androidGradlePlugin = "7.4.0" springBoot = "2.5.4" h2Database = "1.4.200"
[libraries]
部分用于定义依赖项。每个依赖项可以指定模块路径和版本引用。[libraries] kotlinStdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } springBootStarterWeb = { module = "org.springframework.boot:spring-boot-starter-web", version.ref = "springBoot" } h2Database = { module = "com.h2database:h2", version.ref = "h2Database" } # 可以通过version.ref引用version中的值
[plugins]
部分用于定义插件。每个插件可以指定插件 ID 和版本引用。[plugins] androidApplication = { id = "com.android.application", version.ref = "androidGradlePlugin" } kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
GRADLE_USER_HOME
存储全局配置属性、初始化脚本、缓存和日志文件,如gradle的不同版本
还有依赖包,这个需要配置环境变量,变量名就是上面的,或者IDEA中指定这个目录
生命周期
第 1 阶段. 初始化
第 2 阶段. 配置
build.gradle(.kts)
评估参与构建的每个项目的构建脚本。- 为请求的任务创建任务图。
第三阶段:执行
- 安排并执行选定的任务。
- 任务之间的依赖关系决定了执行顺序。
- 任务的执行可以并行发生。
Android配置
兼容性
Android | Java | 支持的 API 和语言功能 |
---|---|---|
14(API 34) | 17 | 核心库 |
13(API 33) | 11 | 核心库 |
12(API 32) | 11 | Java API |
11 及更低版本 | Android 版本 |
plugins {
id 'com.android.application'
}
android {
namespace 'com.example.myapp'
//每个 Android 模块都有一个命名空间,此命名空间用作
//其生成的 和 R 类的 Kotlin 或 Java 软件包名称。
compileSdk 33 // 编译的sdk版本
defaultConfig {
applicationId 'com.example.myapp'
minSdk 21 // 最小的sdk版本可用
targetSdk 33
// 指定一下最佳的运行版本,高于它但不低于编译版本,会以兼容模式运行
versionCode 1 // 当前项目的版本号,自定
versionName "1.0" // 这个应该是表明显示的版本号
}
buildTypes {
// 这应该是构建的时候用的
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
flavorDimensions "tier"
productFlavors {
free {
dimension "tier"
applicationId 'com.example.myapp.free'
}
paid {
dimension "tier"
applicationId 'com.example.myapp.paid'
}
}
}
dependencies {
implementation project(":lib")
implementation 'androidx.appcompat:appcompat:1.7.0'
implementation fileTree(dir: 'libs', include: ['*.jar'])
}