AndroidStudio构建系统用户指南

        看了很多资料,还是有点不甚理解,学习本身就是一个混沌的到模糊再到清晰的过程。开始学的时候,找到一点零碎的内容,似懂非懂地看着,感觉可以理解,但是不知道如何应用,找到官网的资料学习,稍微系统了,但是总感觉很吃力,或许是因为英文文档的缘故,和自己内心一丝丝的急迫感,难以静下心来仔细斟酌。终于,系统不系统地一些资料后,彻底晕乎了。静下心来,稍微整理下这写内容,发现并没有那么多,关键还是自己心态和学习方式的问题,特收藏在此,加深印象。

        下面是官网对于Gradle构建系统的用户指南英文原文,如果要看中文版,可以参考: 

        http://blog.csdn.net/qinxiandiqi/article/details/37757065?utm_source=tuicool

        http://blog.csdn.net/qinxiandiqi/article/details/37757475

        ........


        原文地址:http://tools.android.com/tech-docs/new-build-system/user-guide 

        

       

Gradle Plugin User Guide

Contents

  1. Introduction
    1. 1.1 Why Gradle?
  2. Requirements
  3. Basic Project
    1. 3.1 Simple build files
    2. 3.2 Project Structure
      1. 3.2.1 Configuring the Structure
    3. 3.3 Build Tasks
      1. 3.3.1 General Tasks
      2. 3.3.2 Java project tasks
      3. 3.3.3 Android tasks
    4. 3.4 Basic Build Customization
      1. 3.4.1 Manifest entries
      2. 3.4.2 Build Types
      3. 3.4.3 Signing Configurations
      4. 3.4.4 Running ProGuard
      5. 3.4.5 Shrinking Resources
  4. Dependencies, Android Libraries and Multi-project setup
    1. 4.1 Dependencies on binary packages
      1. 4.1.1 Local packages
      2. 4.1.2 Remote artifacts
    2. 4.2 Multi project setup
    3. 4.3 Library projects
      1. 4.3.1 Creating a Library Project
      2. 4.3.2 Differences between a Project and a Library Project
      3. 4.3.3 Referencing a Library
      4. 4.3.4 Library Publication
  5. Testing
    1. 5.1 Unit Testing
    2. 5.2 For the experimental unit testing support added in 1.1, please see this page. The rest of this section describes "instrumentation tests" that can run on a real device (or an emulator) and require a separate, testing APK to be built.
    3. 5.3 Basics and Configuration
    4. 5.4 Running tests
    5. 5.5 Testing Android Libraries
    6. 5.6 Test reports
      1. 5.6.1 Single projects
      2. 5.6.2 Multi-projects reports
    7. 5.7 Lint support
  6. Build Variants
    1. 6.1 Product flavors
    2. 6.2 Build Type + Product Flavor = Build Variant
    3. 6.3 Product Flavor Configuration
    4. 6.4 Sourcesets and Dependencies
    5. 6.5 Building and Tasks
    6. 6.6 Testing
    7. 6.7 Multi-flavor variants
  7. Advanced Build Customization
    1. 7.1 Build options
      1. 7.1.1 Java Compilation options
      2. 7.1.2 aapt options
      3. 7.1.3 dex options
    2. 7.2 Manipulating tasks
    3. 7.3 BuildType and Product Flavor property reference
    4. 7.4 Using sourceCompatibility 1.7

Introduction

Goals of the new Build System
The goals of the new build system are:
  • Make it easy to reuse code and resources
  • Make it easy to create several variants of an application, either for multi-apk distribution or for different flavors of an application
  • Make it easy to configure, extend and customize the build process
  • Good IDE integration

Why Gradle?

Gradle is an advanced build system as well as an advanced build toolkit allowing to create custom build logic through plugins.

Here are some of its features that made us choose Gradle:
  • Domain Specific Language (DSL) to describe and manipulate the build logic
  • Build files are Groovy based and allow mixing of declarative elements through the DSL and using code to manipulate the DSL elements to provide custom logic.
  • Built-in dependency management through Maven and/or Ivy.
  • Very flexible. Allows using best practices but doesn’t force its own way of doing things.
  • Plugins can expose their own DSL and their own API for build files to use.
  • Good Tooling API allowing IDE integration

Requirements

  • Gradle 1.10 or 1.11 or 1.12 with the plugin 0.11.1
  • SDK with Build Tools 19.0.0. Some features may require a more recent version.

Basic Project

A Gradle project describes its build in a file called  build.gradle located in the root folder of the project.

Simple build files

The most simple Java-only project has the following  build.gradle:

apply plugin: 'java'

This applies the Java plugin, which is packaged with Gradle. The plugin provides everything to build and test Java applications.

The most simple Android project has the following build.gradle:

buildscript {
    repositories {
          mavenCentral()
     }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.11.1'
    }
}

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"
}

There are 3 main areas to this Android build file:

buildscript { ... } configures the code driving the build.
In this case, this declares that it uses the Maven Central repository, and that there is a classpath dependency on a Maven artifact. This artifact is the library that contains the Android plugin for Gradle in version 0.11.1
Note: This only affects the code running the build, not the project. The project itself needs to declare its own repositories and dependencies. This will be covered later.

Then, the  android plugin is applied like the Java plugin earlier.

Finally,  android { ... } configures all the parameters for the android build. This is the entry point for the Android DSL.
By default, only the compilation target, and the version of the build-tools are needed. This is done with the  compileSdkVersion and  buildtoolsVersion  properties.
The compilation target is the same as the  target property in the project.properties file of the old build system. This new property can either be assigned a int (the api level) or a string with the same value as the previous  target property.

Important: You should only apply the  android plugin. Applying the  java plugin as well will result in a build error.

Note: You will also need a  local.properties file to set the location of the SDK in the same way that the existing SDK requires, using the  sdk.dir property.
Alternatively, you can set an environment variable called  ANDROID_HOME. There is no differences between the two methods, you can use the one you prefer.

Project Structure

The basic build files above expect a default folder structure. Gradle follows the concept of convention over configuration, providing sensible default option values when possible.

The basic project starts with two components called “source sets”. The main source code and the test code. These live respectively in:
  • src/main/
  • src/androidTest/
Inside each of these folders exists folder for each source components.
For both the Java and Android plugin, the location of the Java source code and the Java resources:
  • java/
  • resources/
For the Android plugin, extra files and folders specific to Android:
  • AndroidManifest.xml
  • res/
  • assets/
  • aidl/
  • rs/
  • jni/
  • jniLibs/
Note:  src/ androidTest /AndroidManifest.xml is not needed as it is created automatically.
Configuring the Structure
When the default project structure isn’t adequate, it is possible to configure it. According to the Gradle documentation, r econfiguring the sourceSets for a Java project can be done with the following:

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

Note:  srcDir will actually add the given folder to the existing list of source folders (this is not mentioned in the Gradle documentation but this is actually the behavior).

To replace the default source folders, you will want to use  srcDirs instead, which takes an array of path. This also shows a different way of using the objects involved:

sourceSets {
    main.java.srcDirs = ['src/java']
    main.resources.srcDirs = ['src/resources']
}

For more information, see the Gradle documentation on the Java plugin  here .

The Android plugin uses a similar syntaxes, but because it uses its own  sourceSets, this is done within the  android object.
Here’s an example, using the old project structure for the main code and remapping the androidTest sourceSet to the  tests  folder:

android {
    sourceSets {
        main {
             manifest.srcFile 'AndroidManifest.xml'
              java.srcDirs = ['src']
              resources.srcDirs = ['src']
               aidl.srcDirs = ['src']
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值