Gradle

目标:

  1. 初步了解gradle的DSL 语言,能够看懂, 并简单改写。
  2. 了解Android Gradle build 系统
  3. 为Feedback Android SDK 书写gradle 打包系统,发布。
  4. 生成Eclipse 导入环境。 使得在Eclipse 中可用。

导读

Gradle 是一个通用的编译构建工具。 Gradle 编译脚本使用Groovy的DSL 语言编写。 其强调配置 而不是编程

新的Android 工程使用Gradle 来编译, Android 团队提供了一个官方的Gradle plugin 插件来支持Android 工程使用Gradle 来编译。

Android 新的构建系统使得:

Gradle 特征:

基础

一个Gradle 项目使用根目录下的build.gradle 来描述其构建配置。

项目结构

Gradle 利用了习惯而不是配置的原则,尽可能的提供一些合理的默认值。

可以使用”sourceSets” 来配置或者改变默认值

  sourceSets {
      main {
          java {
              srcDir 'src/java'
          }
          resources {
              srcDir 'src/resources'
          }
      }
  }

Android:

  android {
      sourceSets {
          main {
              manifest.srcFile 'AndroidManifest.xml'
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
              aidl.srcDirs = ['src']
              renderscript.srcDirs = ['src']
              res.srcDirs = ['res']
              assets.srcDirs = ['assets']
          }

          androidTest.setRoot('tests')
      }
  }

任务

Android 任务

自定义构建过程

构建类型

debug, release instances of BuildType.

  android {
      buildTypes {
          debug {
              applicationIdSuffix ".debug"
          }

          jnidebug.initWith(buildTypes.debug)
          jnidebug {
              packageNameSuffix ".jnidebug"
              jnidebugBuild true
          }
      }
  }

ProGuard

依赖,Android 类库项目, 多项目配置

Library publication

Testing

Lint support

Build variants

Product flavors

Build Type + Product Flavor = Build Variant

高级自定义

AAR format

The ‘aar’ bundle is the binary distribution of an Android Library Project.

The file extension is .aar, and the maven artifact type should be aar as well, but the file itself a simple zip file with the following entries: /AndroidManifest.xml (mandatory) /classes.jar (mandatory) /res/ (mandatory) /R.txt (mandatory) /assets/ (optional) /libs/*.jar (optional) /jni//*.so (optional) /proguard.txt (optional) /lint.jar (optional) These entries are directly at the root of the zip file.

The R.txt file is the output of aapt with –output-text-symbols.

Java Plugin

  apply plugin: 'java'

Source sets

Dependency management

Eclipse Plugin

References

  1. Gradle Java Plugin
  2. Gradle Plugin User Guide
  3. Gradle Build Language Reference