Gradle 笔记_1 - Gradle 基础

Gradle 笔记_1 - Gradle 基础

《Gradle for Android 中文版》笔记

理解 Gradle 基础

Gradle 构建的脚本–build.gradle
Gradle 有约定优于配置的原则,即为设置和属性提供默认值。

基于 Groovy 的领域专用语言(DSL)。
Groovy 是一种基于 Java 虚拟机的动态语言。
想创建自己的任务和插件,那么对 Groovy 有更深层次的理解就显得尤为重要。

项目和任务

Gradle 中,最重要的两个概念是项目和任务。

  • 每一次构建都包括至少一个项目,每一个项目有包括一个或多个任务。
  • 每个build.gradle 文件都代表一个项目。
  • 任务定义在构建脚本里。

当初始化构建过程时,Gradle 会基于 build 文件组装项目和任务对象。

一个任务对象包含一系列动作对象,这些动作对象之后会按顺序执行。
一个单独的动作对象就是一个待执行的代码块,它和 Java 中的方法类似。

构建生命周期

为了简化构建过程,构建工具会新建一个动态的模型流(叫做 Directed Acyclic Graph(DAG)),所有任务都会被一个接一个地执行,不循环和重复执行。
在构建的配置阶段会生成依赖关系图,那些没有依赖的任务通常会被优先执行。

Gradle 构建通常有三个阶段:

  • 初始化:创建项目实例,一个项目如有多个模块,每个模块都有 build.gradle 文件,那么创建多个项目实例。
  • 配置:构建脚本会被执行,并为每个项目实例创建和配置任务。
  • 执行:Gradle 将决定哪个任务会被执行。
构建配置文件

整个项目的 build.gradle 文件:

buidscript {
	repositories {
		jcenter()
	}
	dependencies {
		classpath 'com.android.tools.build.gradle:3.5.0'
	}
}

JCenter 是一个预配置的 Marven 仓库,放在 repositories 代码块中代表被配置为整个构建过程的依赖仓库。

Android 应用插件:

apply plugin: 'com.android.application'

每一个安卓项目都应该申请该插件,通常在 app 模块的 build.gradle 文件中。
用于扩展 Gradle 构建脚本的能力。
在项目中应用一个插件,项目就可以使用该插件预定义的一些属性和任务。

Android 依赖库插件:

apply plugin: 'com.android.library'

依赖库的 build.gradle 文件需要声明应用 Android 依赖库插件。
一个模块可以是 Android 应用模块,或者是一个 Android 依赖模块,但不能二者都是,即不能同时申请上面两种插件。

当使用 Android 插件时,不仅可以配置针对 Android 的特殊约定,还可以生成只应用于 Android 的任务。

android {
	compileSdkVersion 29  // 编译的 SDK 版本
	buildToolsVersion "29.0.2" // 构建工具版本
}

这是 Android 特殊约定的一部分。Android 插件为 Android 的开发需求提供了一整套的 DSL。

项目结构

MyApp

  • build.gradle
  • settings.gradle
  • app
    • build.gradle
    • libs
    • src
      • main
      • java
        • com.package.myapp
    • res
      • drawable
      • layout
      • etc.
  • libcommon
    • build.gradle
    • libs
    • src
      • main
      • java
        • com.libs.libcommon
      • res
        • drawable
        • layout
        • etc.

Gradle Wrapper

Gradle 是一个不断发展的工具,新版本可能打破向后兼容性,使用 Gradle Wrapper 可以避免这个问题,并能确保构建时可重复的。

Gradle Wrapper 为 Windows 操作系统提供了一个 batch 文件(为其他操作系统提供了 shell 文件),当运行这段脚本时,需要的 Gradle 版本会被自动下载(如果不存在)和使用。这样就无需手动安装正确的 Gradle 版本。

terminal 上运行

$ gradlew -v

或在命令行中运行

$ gradlew.bat -v

查看 Gradle 版本号和其他额外参数信息。

运行基本构建任务
$ gradlew tasks

打印所有可用的任务列表,添加 --all 参数,会打印每个任务对应依赖的详细介绍。

$ gradlew assembleDebug

其他还有

  • Check:运行所有的检查。
  • Build:触发 assemble 和 check。
  • Clean:清除项目的输出(output)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
About This Book, Create custom Gradle tasks and plugins for your Android projects, Configure different build variants, each with their own dependencies and properties, Manage multi-module projects, and integrate modules interdependently, Who This Book Is For, If you are an experienced Android developer wanting to enhance your skills with the Gradle Android build system, then this book is for you. As a prerequisite, you will need some knowledge of the concepts of Android application development., What You Will Learn, Build new Android apps and libraries using Android Studio and Gradle, Migrate projects from Eclipse to Android Studio and Gradle, Manage the local and remote dependencies of your projects, Create multiple build variants, Include multiple modules in a single project, Integrate tests into the build process, Create custom tasks and plugins for Android projects, In Detail, Gradle is an open source build automation system that introduces a Groovy-based domain-specific language (DSL) to configure projects. Using Gradle makes it easy for Android developers to manage dependencies and set up the entire build process., This book begins by taking you through the basics of Gradle and how it works with Android Studio. Furthermore, you will learn how to add local and remote dependencies to your project. You will work with build variants, such as debug and release, paid and free, and even combinations of these things. The book will also help you set up unit and integration testing with different libraries and will show how Gradle and Android Studio can make running tests easier. Finally, you will be shown a number of tips and tricks on the advanced customization of your application's build process. By the end of this book, you will be able to customize the entire build process, and create your own tasks and plugins for your Gradle builds.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值