Gradle 学习一 学习资料准备

一 概述

目前开发android项目,使用as开发、gradle构建已经成了标配。默认新建的as 工程会给我们生成基础的build.gradle文件,里面包含了构建一个app所需要的最基本的配置声明。实际项目中,需求变化莫测,尝尝需要扩展gradle脚本来实现一些特定功能,这就需要我们对gradle比较熟悉。因此,只有真正明白gradle的原理,android app里面build.gradle的涵义才能做到以不变应万变。

二 资料

要想无障碍灵活定制gradle脚本来实现android app各种场景,需要学习以下知识:
1. groovy语法规则,熟悉基本的语法规范,主要包括基本数据类型、集合、闭包等
groovy官方api查询文档
2. gradle实战,熟悉gradle project task 依赖等理论知识
gradle官方查询api文档
3. 使用文档 Android Plugin DSL,通过api查询所需要的功能。比如applicationVariants返回app所有构建变体的集合,我们可以利用这个特性动态修改每一个构建变体的定制,比如针对不同的构建变体将生成的apk放入不同的文件夹。variantFilter用来描述哪些构建变体需要过滤掉。

Android Plugin DSL文档主要包括

  • Android Plugin DSL
    • Configuration blocks
    • Extension types
      • AppExtension
      • LibraryExtension
      • TestExtension
      • FeatureExtension

Configuration blocks

声明一些脚本代码块,主要包括:

aaptOptions { } apk打包工具aapt的配置项

adbOptions { } apk调试工具adb的配置项

buildTypes { } 封装了工程的所有构建类型,比如realease develop等

compileOptions { } java编译配置,比如jdk版本

dataBinding { } data binding library的配置

defaultConfig { } 描述所有构建变体的通用配置,在创建不同product flavor的时候可以重载defaultConfig来修改基础配置

扫描二维码关注公众号,回复: 2263472 查看本文章

dexOptions { } dex工具配置,比如可以打开library的pre-dexing

externalNativeBuild { } 配置c系列语言的构建,使用CMake或者ndk-build

jacoco { } 已经废弃

lintOptions { } lint工具配置,用作代码检查

packagingOptions { } 配置,用来描述哪些文件最终打包到apk

productFlavors { } 工程对应的所有产品风味配置,可以用来配置多渠道

signingConfigs { } apk签名配置

sourceSets { } 资源配置

如果您的源未组织到 Gradle 期望的默认源集文件结构中(如上面的创建源集部分中所述),您可以使用 sourceSets {} 代码块更改 Gradle 希望为源集的每个组件收集文件的位置。您不需要重新定位文件;只需要为 Gradle 提供相对于模块级 build.gradle 文件的路径,Gradle 应当可以在此路径下为每个源集组件找到文件。要了解您可以配置哪些组件,以及是否可以将其映射到多个文件或目录,请阅读适用于 Gradle 的 Android 插件参考。
下面的代码示例可以将 app/other/ 目录中的源映射到 main 源集的某些组件,并更改 androidTest 源集的根目录。

sourceSets {
    // Encapsulates configurations for the main source set.
    main {
      // Changes the directory for Java sources. The default directory is
      // 'src/main/java'.
      java.srcDirs = ['other/java']

      // If you list multiple directories, Gradle uses all of them to collect
      // sources. Because Gradle gives these directories equal priority, if
      // you define the same resource in more than one directory, you get an
      // error when merging resources. The default directory is 'src/main/res'.
      res.srcDirs = ['other/res1', 'other/res2']

      // Note: You should avoid specifying a directory which is a parent to one
      // or more other directories you specify. For example, avoid the following:
      // res.srcDirs = ['other/res1', 'other/res1/layouts', 'other/res1/strings']
      // You should specify either only the root 'other/res1' directory, or only the
      // nested 'other/res1/layouts' and 'other/res1/strings' directories.

      // For each source set, you can specify only one Android manifest.
      // By default, Android Studio creates a manifest for your main source
      // set in the src/main/ directory.
      manifest.srcFile 'other/AndroidManifest.xml'
      ...
    }

splits { } 构建多apk,或者apk分割。可以用来配置不同的apk包含不同分辨率下的资源和代码

/**
   * The splits {} block is where **********you********** can configure different APK builds that
   * each contain only code and resources for a supported screen density or
   * ABI. You'll also need to configure your build so that each APK has a
   * different versionCode.
   */
 splits {
    // Screen density split settings
    density {

      // Enable or disable the density split mechanism
      enable false

      // Exclude these densities from splits
      exclude "ldpi", "tvdpi", "xxxhdpi", "400dpi", "560dpi"
    }
  }

testOptions { } 单元测试配置

AppExtension

com.android.application 插件工程的扩展,这也是as开发android工程最常用的,module目录build.gradle文件头一句可以看到apply plugin: ‘com.android.application’,代表导入这个插件工程,然后可以直接使用插件工程里面已经开发好的功能。com.android.application用来构建apk工程。

android {
    compileSdkVersion 23
    buildToolsVersion '25.0.0'

    、、、、、
}

LibraryExtension

com.android.library 插件工程的扩展。用来构建library工程,生成aar文件。

TestExtension

com.android.test 插件工程的扩展

FeatureExtension

com.android.feature 插件工程的扩展

关于gradle构建app项目,谷歌官方也提供了文档说明:

google官方学习文档

三 总结

本文描述了需要学习、了解哪些知识和文档才能真正弄懂android app工程构建的原理。主要三方面:熟悉groory语法、了解gradle常用知识点、学习android plugin dsl。所有知识建议阅读官方文档,这样有助于更系统的了解gradle构建原理。有了这些知识作为基础,加上实战,一定会事半功倍。

四 参考

google官方学习文档

Android Plugin DSL

猜你喜欢

转载自blog.csdn.net/rambomatrix/article/details/78553793