Android 理清 Gradle、AGP、Groovy 和构建文件之间的关系

在 Android 开发中,我们常常会接触到一系列看似相近却各有分工的名词,比如:GradleGroovyAGPgradle-wrapper.propertiesbuild.gradlesettings.gradle 等等。
它们彼此之间到底是什么关系?各自又承担了什么角色?本文将用清晰的结构,一次性帮你理清!


一、Gradle 是什么?

Gradle 是一种现代化的构建工具,它负责自动化完成软件项目的编译、打包、依赖管理、测试执行等任务。

和早期的 Maven 相比,Gradle 拥有更高的灵活性和更快的构建速度,而且支持使用不同语言(如 Groovy、Kotlin)来编写构建脚本。

简而言之:

有了 Gradle,我们不需要自己写一堆 shell 脚本或者手动管理复杂的构建逻辑。


二、Groovy 是什么?

Groovy 是一种运行在 JVM 上的动态脚本语言。
它与 Java 兼容性极好,语法更为简洁灵活。

Gradle 的默认配置脚本(如 build.gradlesettings.gradle)早期都是用 Groovy 编写的,因此如果你看到很多 build.gradle 文件里有像 apply plugin:dependencies {} 这样的语法,那其实就是 Groovy 代码。

后来 Gradle 也支持了 Kotlin DSL(.gradle.kts 后缀),但很多项目依然以 Groovy 为主流。


三、AGP(Android Gradle Plugin)是什么?

单靠 Gradle,还无法直接理解如何去构建一个 Android 项目。这时,需要 Google 提供的构建扩展 —— Android Gradle Plugin(AGP)

AGP 让 Gradle 能够理解:

  • 什么是 Android 应用、Library
  • 如何处理 AndroidManifest
  • 如何执行编译、签名、打包 APK 等 Android 特有的任务

比如在 build.gradle 中看到的:

plugins {
    id 'com.android.application' version '8.2.0' apply false
}

这就是在引入 AGP 插件。

注意

  • AGP 也有自己的版本号,比如 8.2.0
  • AGP 和 Gradle 版本之间存在兼容关系,升级时需要参考官方对应表。

四、gradle-wrapper.properties 是干什么的?

Gradle 官方强烈推荐每个项目使用 Gradle Wrapper(包装器)。

Gradle Wrapper 的作用

  • 保证团队成员或 CI 系统使用 相同版本 的 Gradle,而不是各自本地安装的版本。
  • 通过一个小脚本自动下载、安装指定版本的 Gradle。

配置文件路径通常是:

gradle/wrapper/gradle-wrapper.properties

内容示例:

distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-bin.zip

这里指定了下载哪个版本的 Gradle。

这样一来,别人 clone 项目之后,直接运行 ./gradlew build,不需要自己手动安装 Gradle,体验大大提升。


五、build.gradle 和 settings.gradle 的角色

1. build.gradle

Gradle 最核心的构建配置文件。分为两种层次:

  • 项目根目录的 build.gradle(项目级别)
  • 各模块(如 app 模块)的 build.gradle(模块级别)

常见内容包括:

  • 应用插件(如 com.android.application
  • 配置编译参数(如 compileSdkVersion
  • 配置依赖库(如 dependencies {}

简单例子:

plugins {
    id 'com.android.application'
}

android {
    namespace 'com.example.myapp'
    compileSdkVersion 34
}

dependencies {
    implementation 'androidx.core:core-ktx:1.10.0'
}

2. settings.gradle

告诉 Gradle 有哪些模块项目名称是什么,还可以定义一些共享仓库地址等。

例子:

rootProject.name = "MyAwesomeProject"
include ':app', ':library'
  • rootProject.name 定义了项目名。
  • include 定义了需要参与构建的模块。

六、它们之间的关系图

可以用一张简洁的结构图来总结它们之间的关系:

                    Android 项目
                        |
         --------------------------------
         |                              |
   gradle-wrapper.properties       settings.gradle
         |                              |
   指定Gradle版本                   定义子模块
         |
        Gradle (核心构建工具)
         |
     +---+---+
     |       |
  AGP插件   普通Gradle插件
     |
  帮助编译Android
     |
 build.gradle (各种模块的配置,如依赖、编译选项)

七、一句话总结

Gradle 是构建工具,Groovy 是脚本语言,AGP 是让 Gradle 支持 Android 项目的插件,gradle-wrapper.properties 指定了 Gradle 版本,而 build.gradle 和 settings.gradle 则定义了构建的具体逻辑。

[DEBUG]: Remove directory and subdirectory src/main/assets [DEBUG]: Create directory src/main/assets Compiling '/root/android/.buildozer/android/app/sitecustomize.py'... Compiling '/root/android/.buildozer/android/app/main.py'... [DEBUG]: Remove directory and subdirectory /tmp/p4a-extra-env-w25uqkn9 [DEBUG]: Remove directory and subdirectory src/main/res [INFO]: Detected highest available build tools version to be 36.0.0 [DEBUG]: -> running gradlew clean assembleDebug [DEBUG]: [DEBUG]: > Task :processDebugMainManifest [DEBUG]: /root/android/.buildozer/android/platform/build-arm64-v8a/dists/myapp/src/main/AndroidManifest.xml:59:18-50 Warning: [DEBUG]: android:extractNativeLibs should not be specified in this source AndroidManifest.xml file. See https://d.android.com/guide/topics/manifest/application-element#extractNativeLibs for more information. [DEBUG]: The AGP Upgrade Assistant can remove the attribute from the AndroidManifest.xml file and update the build file accordingly. See https://d.android.com/studio/build/agp-upgrade-assistant for more information. [DEBUG]: [DEBUG]: > Task :compileDebugJavaWithJavac [DEBUG]: Note: Some input files use or override a deprecated API. [DEBUG]: Note: Recompile with -Xlint:deprecation for details. [DEBUG]: [DEBUG]: Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0. [DEBUG]: [DEBUG]: You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins. [DEBUG]: [DEBUG]: See https://docs.gradle.org/8.0.2/userguide/command_line_interface.html#sec:command_line_warnings [DEBUG]: [DEBUG]: BUILD SUCCESSFUL in 14s [DEBUG]: 33 actionable tasks: 33 executed [INFO]: <- directory context /root/android/.buildozer/android/platform/python-for-android [DEBUG]: All possible dists: [<Distribution: name myapp with
03-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值