配置 build
admin2025-07-28 02:44:35【世界杯比赛赛】
基本知识
打造 AI 体验
使用 Gemini API 等构建 AI 赋能的 Android 应用。
开始使用
开始使用
首先,构建您的第一个应用。然后,通过我们的培训课程更深入地了解相关知识,或者自行探索应用开发。
Hello World
培训课程
教程
团队版 Compose
Android 版 Kotlin
利用 Play 变现 ↗️
按设备扩展
构建可让用户在手机、平板电脑、手表、头戴设备等设备上获享无缝体验的应用。
自适应应用
Android XR
Wear OS
Android for Cars
Android TV
ChromeOS
按类别构建
了解如何按照 Google 的明确规范化指导,针对您的使用场景构建应用。
游戏
相机与媒体
社交与消息
健康与健身
工作效率
企业应用
掌握最新资讯
及时了解一年中发布的新版本,加入我们的试用计划,并向我们提供反馈。
最新动态
实验性更新
Android Studio 预览版
Jetpack 库和 Compose 库
Wear OS 版本
Privacy Sandbox ↗️
设计和规划
出色体验
为优质用户打造优质体验。
了解详情
界面设计
遵循 Android 最佳实践设计精美的界面。
为 Android 设计
移动
自适应界面
Android XR
widget
Wear OS
Android TV
架构
设计可靠、可测试且可维护的应用逻辑和服务。
简介
库
导航
模块化
测试
质量
妥善规划,打造高品质应用并符合 Play 商店准则。
概览
核心价值
用户体验
无障碍
技术质量
出色体验
安全
保护用户免受威胁,确保安全的 Android 体验。
概览
隐私权
权限
身份
防范欺诈
开发
Android Studio 中的 Gemini
用于 Android 开发的 AI 开发助手。
了解详情
获取 Android Studio
核心领域
获取所需功能的相关示例和文档。
示例
界面
后台工作
数据和文件
连接
所有核心领域 ⤵️
工具和工作流
您可以使用 IDE 编写和构建应用,或创建自己的流水线。
编写和调试代码
构建项目
测试应用
性能
命令行工具
Gradle 插件 API
设备技术
针对不同设备规格编写代码。连接设备并共享数据。
自适应界面
Wear OS
Android XR
Android 运行状况
Android for Cars
Android TV
所有设备 ⤵️
库
浏览 API 参考文档,了解各方面的详细信息。
Android 平台
Jetpack 库
Compose 库
Google Play 服务 ↗️
Google Play SDK 索引 ↗️
Google Play
Play 管理中心
在 Google Play 上发布应用或游戏并拓展业务。
转到控制台
了解详情 ↗️
基础知识
了解如何吸引用户、创收并保护应用安全。
Play 结算服务
Play Integrity
Play 政策
Play 计划 ↗️
游戏开发中心
开发和交付游戏。获取工具、下载内容和示例。
概览
Play Asset Delivery
Play 游戏服务
Play 游戏电脑版
库
浏览 API 参考文档,了解各方面的详细信息。
Play Feature Delivery
Play 应用内更新
Play 应用内评价
Play Install Referrer
Google Play 服务 ↗️
Google Play SDK 索引 ↗️
工具和资源
用于发布、推广和管理应用的工具。
Android App Bundle
品牌与营销
Play 管理中心 API ↗️
Play Points
社区
/
English
Deutsch
Español
Español – América Latina
Français
Indonesia
Italiano
Polski
Português – Brasil
Tiếng Việt
Türkçe
Русский
עברית
العربيّة
فارسی
हिंदी
বাংলা
ภาษาไทย
中文 – 简体
中文 – 繁體
日本語
한국어
Android Studio
Android Studio
下载
IDE 指南
Android Studio 中的 Gemini
适合企业使用的 Gemini
提示库
Gradle build 指南
SDK 工具指南
预览版
基本知识
更多
设计和规划
更多
开发
更多
下载
IDE 指南
Android Studio 中的 Gemini
适合企业使用的 Gemini
提示库
Gradle build 指南
SDK 工具指南
预览版
Google Play
更多
社区
Android Studio
Android build 的新变化
Android Gradle 插件 8.11 版本说明Android Gradle 插件路线图Android Gradle 插件 API 更新过往的 Android Gradle 插件版本
Android build 概览
Gradle 构建概览Android build 结构工具和库的相互依存关系Gradle 依赖项解析
管理构建
配置 Android 构建系统添加注解处理器Android build 中的 Java 版本配置应用模块管理清单文件应用自定义构建逻辑配置 build 变体添加原生符号D8 和 R8 编译器版本兼容性使用 Android Gradle 插件升级助理使用 Android SDK 升级助理
管理依赖项
添加 build 依赖项添加原生依赖项管理远程仓库验证 build 依赖项升级 build 依赖项
运行 build
从命令行构建您的应用构建应用以向用户发布在 Android Studio 中构建和运行应用
优化 build
概览排查 build 性能问题分析 build 性能优化应用性能 ⍈启用 MultiDex
扩展 build
编写 Gradle 插件Gradle 提示与诀窍集成自定义 C/C++ 构建系统
发布库
概览准备要发布的库配置发布内容变体配置测试固件以供发布将多个 Android 库作为一个库发布上传库
迁移 build
迁移至 Kotlin DSL迁移至版本目录迁移至 Kotlin Symbol Processing
排查问题
概览调试依赖项解析错误
Android Gradle 插件 API 参考文档
打造 AI 体验
开始使用
开始使用
Hello World
培训课程
教程
团队版 Compose
Android 版 Kotlin
利用 Play 变现 ↗️
按设备扩展
自适应应用
Android XR
Wear OS
Android for Cars
Android TV
ChromeOS
按类别构建
游戏
相机与媒体
社交与消息
健康与健身
工作效率
企业应用
掌握最新资讯
最新动态
实验性更新
Android Studio 预览版
Jetpack 库和 Compose 库
Wear OS 版本
Privacy Sandbox ↗️
出色体验
了解详情
界面设计
为 Android 设计
移动
自适应界面
Android XR
widget
Wear OS
Android TV
架构
简介
库
导航
模块化
测试
质量
概览
核心价值
用户体验
无障碍
技术质量
出色体验
安全
概览
隐私权
权限
身份
防范欺诈
Android Studio 中的 Gemini
了解详情
获取 Android Studio
核心领域
示例
界面
后台工作
数据和文件
连接
所有核心领域 ⤵️
工具和工作流
编写和调试代码
构建项目
测试应用
性能
命令行工具
Gradle 插件 API
设备技术
自适应界面
Wear OS
Android XR
Android 运行状况
Android for Cars
Android TV
所有设备 ⤵️
库
Android 平台
Jetpack 库
Compose 库
Google Play 服务 ↗️
Google Play SDK 索引 ↗️
Play 管理中心
转到控制台
了解详情 ↗️
基础知识
Play 结算服务
Play Integrity
Play 政策
Play 计划 ↗️
游戏开发中心
概览
Play Asset Delivery
Play 游戏服务
Play 游戏电脑版
库
Play Feature Delivery
Play 应用内更新
Play 应用内评价
Play Install Referrer
Google Play 服务 ↗️
Google Play SDK 索引 ↗️
工具和资源
Android App Bundle
品牌与营销
Play 管理中心 API ↗️
Play Points
此页面由 Cloud Translation API 翻译。
Android Developers
开发
Android Studio
Gradle build 指南
配置 build
Android 构建系统会编译应用资源和源代码,然后将它们打包成 APK 或 Android App Bundle 文件,供您测试、部署、签名和分发。
在 Gradle build 概览和 Android build 结构中,我们讨论了 build 概念和 Android 应用的结构。现在,该配置 build 了。
Android build 术语表
Gradle 和 Android Gradle 插件可帮助您完成 build 以下方面的配置:
build 类型
build 类型定义 Gradle 在构建和打包应用时使用的某些属性。通常针对开发生命周期的不同阶段进行配置。
例如,调试 build 类型会启用调试选项,并会使用调试密钥为应用签名;而发布 build 类型则可能会缩减应用大小、对应用进行混淆处理,并使用发布密钥为应用签名以进行分发。
如要构建应用,您必须至少定义一个 build 类型。Android Studio 默认会创建调试 build 类型和发布 build 类型。如要着手为应用自定义打包设置,不妨了解如何配置 build 类型。
产品变种
产品变种代表您可以向用户发布的不同应用版本,如应用的免费版和付费版。您可以自定义产品变种以使用不同的代码和资源,同时共享并重用所有应用版本共用的部分。产品变种是可选的,您必须手动创建。如要开始创建应用的不同版本,请了解如何配置产品变种。
build 变体
build 变体是 build 类型与产品变种的交叉产物,也是 Gradle 用来构建应用的配置。利用 build 变体,您可以在开发期间构建产品变种的调试版本,以及构建产品变种的已签名发布版本以供分发。虽然您无法直接配置 build 变体,但可以配置构成 build 变体的 build 类型和产品变种。创建额外的 build 类型或产品变种也会产生额外的 build 变体。如需了解如何创建和管理 build 变体,请参阅配置 build 变体概览。
清单条目
您可以在 build 变体配置中为清单文件的某些属性指定值。这些 build 值会覆盖清单文件中的现有值。如果您要为应用生成多个变体,让每一个变体都具有不同的应用名称、最低 SDK 版本或目标 SDK 版本,便可运用这一技巧。当存在多个清单时,清单合并工具会合并清单设置。
依赖项
构建系统会管理来自本地文件系统以及来自远程仓库的项目依赖项。这样一来,您就不必手动搜索、下载依赖项的二进制文件包以及将它们复制到项目目录中。如需了解详情,请参阅添加 build 依赖项。
签名
构建系统既允许您在 build 配置中指定签名设置,也可以在构建流程中自动为应用签名。构建系统通过已知凭据使用默认密钥和证书为调试版本签名,以避免在构建时提示输入密码。除非您为此 build 明确定义签名配置,否则,构建系统不会为发布版本签名。如果您没有发布密钥,可以按为应用签名中所述生成一个。通过大多数应用商店分发应用时都需要使用已签名的发布 build。
代码和资源缩减
构建系统允许您为每个 build 变体指定不同的 ProGuard 规则文件。在构建应用时,构建系统会应用一组适当的规则以使用其内置的缩减工具(如 R8)缩减您的代码和资源。
缩减代码和资源有助于缩减 APK 或 AAB 大小。
多 APK 支持
通过构建系统可以自动构建不同的 APK,并让每个 APK 只包含特定屏幕密度或应用二进制接口 (ABI) 所需的代码和资源。如需了解详情,请参阅构建多个 APK。不过,我们建议的方法是发布单个 AAB,因为它除了让您可以按屏幕密度和 ABI 进行拆分以外,还可以让您按语言进行拆分,同时无需将多个工件上传到 Google Play。2021 年 8 月之后提交的所有新应用都必须使用 AAB。
Android build 中的 Java 版本
无论您的源代码是用 Java、Kotlin 还是同时使用这两种语言编写的,您都必须在多个位置为 build 选择 JDK 或 Java 语言版本。如需了解详情,请参阅 Android build 中的 Java 版本。
build 配置文件
如果您创建自定义 build 配置,就需要对一个或多个 build 配置文件做出更改。这些纯文本文件使用领域特定语言 (DSL) 以 Kotlin 脚本(Kotlin 语言的一个变种)描述和操纵构建逻辑。您还可以使用 Groovy(一种适用于 Java 虚拟机 (JVM) 的动态语言)来配置 build。
您无需了解 Kotlin 脚本或 Groovy 即可开始配置 build,因为 Android Gradle 插件引入了您需要的大多数 DSL 元素。如需详细了解 Android Gradle 插件 DSL,请参阅 DSL 参考文档。Kotlin 脚本还依赖于底层 Gradle Kotlin DSL
开始新项目时,Android Studio 会自动为您创建其中的部分文件,并为其填充合理的默认值。如需大致了解创建的文件,请参阅 Android build 结构。
Gradle 封装容器文件
Gradle wrapper (gradlew) 是源代码中包含的一个小型应用,用于下载和启动 Gradle 本身。这样可以实现更一致的 build 执行。开发者下载应用源代码并运行 gradlew。这会下载所需的 Gradle 发行版,并启动 Gradle 来构建您的应用。
gradle/wrapper/gradle-wrapper.properties 文件包含一个属性 distributionUrl,用于描述运行 build 时使用的 Gradle 版本。
提示: 如果同时处理多个项目,请尽可能确保所有项目都使用相同的 Gradle 版本。否则,Gradle 会为每个 Gradle 版本创建 Gradle 守护程序副本,此外还会为用于运行 Gradle 的每个 JDK 创建单独的副本。这会增加内存和 CPU 使用量,可能会拖慢 build 速度或影响机器上的其他工作。distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Gradle 设置文件
settings.gradle.kts 文件(对于 Kotlin DSL)或 settings.gradle 文件(对于 Groovy DSL)位于项目的根目录下。此设置文件会定义项目级代码库设置,并告知 Gradle 在构建应用时应将哪些模块包含在内。多模块项目需要指定应包含在最终 build 中的每个模块。
对于大多数项目,该文件默认如下所示:
Kotlin
pluginManagement {
/**
* The pluginManagement.repositories block configures the
* repositories Gradle uses to search or download the Gradle plugins and
* their transitive dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use
* local repositories or define your own remote repositories. Here we
* define the Gradle Plugin Portal, Google's Maven repository,
* and the Maven Central Repository as the repositories Gradle should use to look for its
* dependencies.
*/
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
/**
* The dependencyResolutionManagement.repositories
* block is where you configure the repositories and dependencies used by
* all modules in your project, such as libraries that you are using to
* create your application. However, you should configure module-specific
* dependencies in each module-level build.gradle file. For new projects,
* Android Studio includes Google's Maven repository and the Maven Central
* Repository by default, but it does not configure any dependencies (unless
* you select a template that requires some).
*/
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include(":app")
Groovy
pluginManagement {
/**
* The pluginManagement.repositories block configures the
* repositories Gradle uses to search or download the Gradle plugins and
* their transitive dependencies. Gradle pre-configures support for remote
* repositories such as JCenter, Maven Central, and Ivy. You can also use
* local repositories or define your own remote repositories. Here we
* define the Gradle Plugin Portal, Google's Maven repository,
* and the Maven Central Repository as the repositories Gradle should use to look for its
* dependencies.
*/
repositories {
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
/**
* The dependencyResolutionManagement.repositories
* block is where you configure the repositories and dependencies used by
* all modules in your project, such as libraries that you are using to
* create your application. However, you should configure module-specific
* dependencies in each module-level build.gradle file. For new projects,
* Android Studio includes Google's Maven repository and the Maven Central
* Repository by default, but it does not configure any dependencies (unless
* you select a template that requires some).
*/
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
rootProject.name = "My Application"
include ':app'
顶层 build 文件
顶层 build.gradle.kts 文件(对于 Kotlin DSL)或 build.gradle 文件(对于 Groovy DSL)位于项目的根目录下。它通常用于定义项目中模块使用的插件的通用版本。
以下代码示例说明了创建新项目后可在顶层 build 脚本中找到的默认设置和 DSL 元素:
Kotlin
plugins {
/**
* Use `apply false` in the top-level build.gradle file to add a Gradle
* plugin as a build dependency but not apply it to the current (root)
* project. Don't use `apply false` in sub-projects. For more information,
* see Applying external plugins with same version to subprojects.
*/
id("com.android.application") version "8.11.0" apply false
id("com.android.library") version "8.11.0" apply false
id("org.jetbrains.kotlin.android") version "2.1.20" apply false
}
Groovy
plugins {
/**
* Use `apply false` in the top-level build.gradle file to add a Gradle
* plugin as a build dependency but not apply it to the current (root)
* project. Don't use `apply false` in sub-projects. For more information,
* see Applying external plugins with same version to subprojects.
*/
id 'com.android.application' version '8.11.0' apply false
id 'com.android.library' version '8.11.0' apply false
id 'org.jetbrains.kotlin.android' version '2.1.20' apply false
}
模块级 build 文件
模块级 build.gradle.kts(对于 Kotlin DSL)或 build.gradle 文件(对于 Groovy DSL)位于每个 project/module/ 目录中。用于为其所在的特定模块配置 build 设置。您可以通过配置这些 build 设置提供自定义打包选项(如额外的 build 类型和产品变种),以及替换 main/ 应用清单或者顶层 build 脚本中的设置。
Android SDK 设置
应用的模块级 build 文件包含一些设置,用于指示编译时使用的 Android SDK 版本、选择平台行为以及指定应用运行的最低版本。
图 1.build 中的 Android SDK
compileSdk
compileSdk 决定了在编译源代码时可用的 Android 和 Java API。如需使用最新的 Android 功能,请在编译时使用最新的 Android SDK。
某些 Android 平台 API 可能无法在旧版 API 级别中使用。您可以
有条件地保护对新功能的使用,也可以使用
AndroidX 兼容性库,以便在较低的 Android API 级别中使用新功能。
每个 Android SDK 都提供一个 Java API 子集,供您在应用中使用。
我可以在 Java 或 Kotlin 源代码中使用哪些 Java API 表格显示了基于 Android SDK 版本的可用 Java API 级别。
较新的 Java API 通过脱糖在早期版本的 Android 上受支持,您必须在 build 中启用脱糖。
如果您的 compileSdk 与当前版本的 Android Studio、AGP 或项目的库依赖项要求相冲突,Android Studio 会显示警告。
minSdk
minSdk 用于指定您希望应用支持的最低 Android 版本。设置 minSdk 可限制哪些设备可以安装您的应用。
支持较低版本的 Android 可能需要在代码中进行更多条件检查,或者更多地使用 AndroidX 兼容性库。您应权衡支持较低版本的维护成本与仍在使用这些较低版本的用户百分比。如需了解当前版本的使用百分比,请参阅 Android Studio 的“New project”向导中的版本图表。
当您在 Android Studio 中编辑代码或在 build 期间运行检查时,lint 会针对您使用的在 minSdk 中不可用的 API 发出警告。您应通过
使新功能成为有条件的功能或使用
Appcompat 实现向后兼容性来修复这些问题。
targetSdk
targetSdk 有以下两种用途:
它用于设置应用的运行时行为。
用于证明您已针对哪个 Android 版本进行测试。
如果您在搭载的 Android 版本高于 targetSdk 的设备上运行应用,Android 会以兼容模式运行您的应用,该模式的行为方式与 targetSdk 中指示的较低版本类似。例如,当 API 23 引入运行时权限模型时,并非所有应用都已准备好立即采用该模型。通过将 targetSdk 设置为 22,这些应用可以在 API 23 设备上运行,而无需使用运行时权限,并且可以使用最新 compileSdk 版本中包含的功能。Google Play 分发政策会
针对目标 API 级别强制执行其他政策。
targetSdk 的值必须小于或等于 compileSdk 的值。
注意:compileSdk 和 targetSdk 的值不必相同。请谨记以下基本原则:
compileSdk 可让您访问新的 API
targetSdk 用于设置应用的运行时行为
targetSdk 必须小于或等于 compileSdk
示例应用模块 build 脚本
以下 Android 应用模块 build 脚本简要说明了一些基础 DSL 元素和设置:
Kotlin
/**
* The first section in the build configuration applies the Android Gradle plugin
* to this build and makes the android block available to specify
* Android-specific build options.
*/
plugins {
id("com.android.application")
}
/**
* Locate (and possibly download) a JDK used to build your kotlin
* source code. This also acts as a default for sourceCompatibility,
* targetCompatibility and jvmTarget. Note that this does not affect which JDK
* is used to run the Gradle build itself, and does not need to take into
* account the JDK version required by Gradle plugins (such as the
* Android Gradle Plugin)
*/
kotlin {
jvmToolchain(11)
}
/**
* The android block is where you configure all your Android-specific
* build options.
*/
android {
/**
* The app's namespace. Used primarily to access app resources.
*/
namespace = "com.example.myapp"
/**
* compileSdk specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*/
compileSdk = 33
/**
* The defaultConfig block encapsulates default settings and entries for all
* build variants and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
// Uniquely identifies the package for publishing.
applicationId = "com.example.myapp"
// Defines the minimum API level required to run the app.
minSdk = 21
// Specifies the API level used to test the app.
targetSdk = 33
// Defines the version number of your app.
versionCode = 1
// Defines a user-friendly version name for your app.
versionName = "1.0"
}
/**
* The buildTypes block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies ProGuard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the default ProGuard rules file.
*/
getByName("release") {
isMinifyEnabled = true // Enables code shrinking for the release build type.
proguardFiles(
getDefaultProguardFile("proguard-android.txt"),
"proguard-rules.pro"
)
}
}
/**
* The productFlavors block is where you can configure multiple product flavors.
* This lets you create different versions of your app that can
* override the defaultConfig block with their own settings. Product flavors
* are optional, and the build system does not create them by default.
*
* This example creates a free and paid product flavor. Each product flavor
* then specifies its own application ID, so that they can exist on the Google
* Play Store or an Android device simultaneously.
*
* If you declare product flavors, you must also declare flavor dimensions
* and assign each flavor to a flavor dimension.
*/
flavorDimensions += "tier"
productFlavors {
create("free") {
dimension = "tier"
applicationId = "com.example.myapp.free"
}
create("paid") {
dimension = "tier"
applicationId = "com.example.myapp.paid"
}
}
/**
* To override source and target compatibility (if different from the
* toolchain JDK version), add the following. All of these
* default to the same value as kotlin.jvmToolchain. If you're using the
* same version for these values and kotlin.jvmToolchain, you can
* remove these blocks.
*/
//compileOptions {
// sourceCompatibility = JavaVersion.VERSION_11
// targetCompatibility = JavaVersion.VERSION_11
//}
//kotlinOptions {
// jvmTarget = "11"
//}
}
/**
* The dependencies block in the module-level build configuration file
* specifies dependencies required to build only the module itself.
* To learn more, go to Add build dependencies.
*/
dependencies {
implementation(project(":lib"))
implementation("androidx.appcompat:appcompat:1.7.1")
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
}
Groovy
/**
* The first line in the build configuration applies the Android Gradle plugin
* to this build and makes the android block available to specify
* Android-specific build options.
*/
plugins {
id 'com.android.application'
}
/**
* Locate (and possibly download) a JDK used to build your kotlin
* source code. This also acts as a default for sourceCompatibility,
* targetCompatibility and jvmTarget. Note that this does not affect which JDK
* is used to run the Gradle build itself, and does not need to take into
* account the JDK version required by Gradle plugins (such as the
* Android Gradle Plugin)
*/
kotlin {
jvmToolchain 11
}
/**
* The android block is where you configure all your Android-specific
* build options.
*/
android {
/**
* The app's namespace. Used primarily to access app resources.
*/
namespace 'com.example.myapp'
/**
* compileSdk specifies the Android API level Gradle should use to
* compile your app. This means your app can use the API features included in
* this API level and lower.
*/
compileSdk 33
/**
* The defaultConfig block encapsulates default settings and entries for all
* build variants and can override some attributes in main/AndroidManifest.xml
* dynamically from the build system. You can configure product flavors to override
* these values for different versions of your app.
*/
defaultConfig {
// Uniquely identifies the package for publishing.
applicationId 'com.example.myapp'
// Defines the minimum API level required to run the app.
minSdk 21
// Specifies the API level used to test the app.
targetSdk 33
// Defines the version number of your app.
versionCode 1
// Defines a user-friendly version name for your app.
versionName "1.0"
}
/**
* The buildTypes block is where you can configure multiple build types.
* By default, the build system defines two build types: debug and release. The
* debug build type is not explicitly shown in the default build configuration,
* but it includes debugging tools and is signed with the debug key. The release
* build type applies ProGuard settings and is not signed by default.
*/
buildTypes {
/**
* By default, Android Studio configures the release build type to enable code
* shrinking, using minifyEnabled, and specifies the default ProGuard rules file.
*/
release {
minifyEnabled true // Enables code shrinking for the release build type.
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
/**
* The productFlavors block is where you can configure multiple product flavors.
* This lets you create different versions of your app that can
* override the defaultConfig block with their own settings. Product flavors
* are optional, and the build system does not create them by default.
*
* This example creates a free and paid product flavor. Each product flavor
* then specifies its own application ID, so that they can exist on the Google
* Play Store or an Android device simultaneously.
*
* If you declare product flavors, you must also declare flavor dimensions
* and assign each flavor to a flavor dimension.
*/
flavorDimensions "tier"
productFlavors {
free {
dimension "tier"
applicationId 'com.example.myapp.free'
}
paid {
dimension "tier"
applicationId 'com.example.myapp.paid'
}
}
/**
* To override source and target compatibility (if different from the
* tool chain JDK version), add the following. All of these
* default to the same value as kotlin.jvmToolchain. If you're using the
* same version for these values and kotlin.jvmToolchain, you can
* remove these blocks.
*/
//compileOptions {
// sourceCompatibility JavaVersion.VERSION_11
// targetCompatibility JavaVersion.VERSION_11
//}
//kotlinOptions {
// jvmTarget = '11'
//}
}
/**
* The dependencies block in the module-level build configuration file
* specifies dependencies required to build only the module itself.
* To learn more, go to Add build dependencies.
*/
dependencies {
implementation project(":lib")
implementation 'androidx.appcompat:appcompat:1.7.1'
implementation fileTree(dir: 'libs', include: ['*.jar'])
}
Gradle 属性文件
Gradle 还包含两个属性文件,它们位于项目的根目录下,可用于指定 Gradle 构建工具包本身的设置:
gradle.properties
您可以在其中配置项目全局 Gradle 设置,如 Gradle 守护程序的最大堆大小。如需了解详情,请参阅构建环境。
local.properties
为构建系统配置本地环境属性,其中包括:
ndk.dir - NDK 的路径。此属性已被废弃。NDK 的所有下载版本都会安装在 Android SDK 目录下的 ndk 目录中。
sdk.dir - Android SDK 的路径。
cmake.dir - CMake 的路径。
ndk.symlinkdir - 在 Android Studio 3.5 及更高版本中,创建指向 NDK 的符号链接,该符号链接的路径可比 NDK 安装路径短。
注意:local.properties 文件专用于 Android Gradle 插件专用的属性。在此文件中添加您自己的值可能会导致出现问题。如果您需要定义自己的本地属性,请创建一个单独的属性文件并手动加载该文件。
将 NDK 重新映射到较短的路径(仅限 Windows)
在 Windows 中,已安装 NDK 文件夹中的工具(例如 ld.exe)最终会获得长路径。这些工具不能很好地支持长路径。
如要创建较短的路径,请在 local.properties 中设置 ndk.symlinkdir 属性,以请求 Android Gradle 插件创建指向 NDK 的符号链接。该符号链接的路径可比现有 NDK 文件夹的路径短。例如,ndk.symlinkdir = C:\ 会生成以下符号链接:
C:\ndk\19.0.5232133
将项目与 Gradle 文件同步
当您在项目中对 build 配置文件进行更改时,Android Studio 会要求您同步项目文件,以便它导入 build 配置更改并执行一些检查以确保您的配置不会造成 build 错误。
如要同步项目文件,请点击做出更改后显示的通知栏中的 Sync Now(如图 2 所示),或者点击菜单栏中的 Sync Project 图标 。如果 Android Studio 发现您的配置有任何错误(例如,您的源代码使用了只有在 compileSdkVersion 以上的 API 级别中才会提供的 API 功能),Messages 窗口就会说明相应的问题。
图 2. 在 Android Studio 中将项目与 build 配置文件同步。
注意: 当 Android Studio 检测到尚未同步的 build 配置更改时,它会停用运行配置,以避免出现令人困惑的失败情况。如果您仍想在不进行同步的情况下运行或调试,请选择忽略这些更改以重新启用这些操作。
源代码集
Android Studio 按逻辑关系将每个模块的源代码和资源分组为源代码集。当您创建新模块时,Android Studio 会在该模块内创建一个 main/ 源代码集。模块的 main/ 源代码集包含其所有 build 变体共用的代码和资源。
其他源代码集目录是可选的,在您配置新的 build 变体时,Android Studio 不会自动为您创建这些目录。不过,创建类似于 main/ 的源代码集有助于组织 Gradle 仅在构建特定应用版本时才应使用的文件和资源:
src/main/
此源代码集包含所有 build 变体共用的代码和资源。
src/buildType/
创建此源代码集可加入特定 build 类型专用的代码和资源。
src/productFlavor/
创建此源代码集可加入特定产品变种专用的代码和资源。
注意:如果配置 build 以组合多个产品变种,则可以为变种维度之间的每个产品变种组合创建源代码集目录:src/productFlavor1ProductFlavor2/
src/productFlavorBuildType/
创建此源代码集可加入特定 build 变体专用的代码和资源。
例如,如需生成应用的“fullDebug”版本,构建系统需要合并来自以下源代码集的代码、设置和资源:
src/fullDebug/(build 变体源代码集)
src/debug/(build 类型源代码集)
src/full/(产品变种源代码集)
src/main/(主源代码集)
注意:当您在 Android Studio 中使用 File > New 菜单选项新建文件或目录时,可以针对特定源代码集进行创建。可供您选择的源代码集取决于您的 build 配置,如果所需的目录尚不存在,Android Studio 会自动创建。
如果不同源代码集包含同一文件的不同版本,Gradle 将按以下优先顺序决定使用哪一个文件。左侧源代码集替换右侧源代码集的文件和设置:
build 变体 > build 类型 > 产品变种 > 主源代码集 > 库依赖项
这样一来,Gradle 便可使用专用于您试图构建的 build 变体的文件,同时重用与其他应用版本共用的 activity、应用逻辑和资源。
在合并多个清单时,Gradle 会使用相同的优先顺序,这样每个 build 变体都能在最终清单中定义不同的组件或权限。如需详细了解如何创建自定义源代码集,请参阅创建源代码集。
版本目录
如果您的 build 包含具有共同依赖项的多个模块,或者您有多个具有共同依赖项的独立项目,我们建议您使用版本目录或物料清单 (BOM) 来指定共同版本。
其他构建系统
Android 应用可以使用 Bazel 来构建,但这不是官方支持的方式。Android Studio 未正式支持 Bazel 项目。
如需更好地了解目前使用 Bazel 构建应用时存在的限制,请参阅已知问题。
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-21。
[null,null,["最后更新时间 (UTC):2025-07-21。"],[],[]]
微信
在微信中关注 Android 开发者
X
在 X 上关注 @AndroidDev
YouTube
在 YouTube 上访问“Android Developers”频道
关于 Android
Android
适用于企业的 Android
安全
源代码
新闻
博客
播客
发现
游戏
机器学习
健康与健身
相机与媒体
隐私权政策
5G
Android 设备
大屏幕
Wear OS
ChromeOS 设备
Android for Cars
Android TV
版本
Android 15
Android 14
Android 13
Android 12
Android 11
Android 10
Pie
文档和下载
Android Studio 指南
开发者指南
API 参考
下载 Studio
Android NDK
支持
报告平台错误
报告文档错误
Google Play support
参加调查研究
Android
Chrome
Firebase
Google Cloud Platform
所有产品
隐私权政策
许可
品牌指南
ICP证合字B2-20070004号
Manage cookies
通过电子邮件接收资讯和提示
订阅
English
Deutsch
Español
Español – América Latina
Français
Indonesia
Italiano
Polski
Português – Brasil
Tiếng Việt
Türkçe
Русский
עברית
العربيّة
فارسی
हिंदी
বাংলা
ภาษาไทย
中文 – 简体
中文 – 繁體
日本語
한국어