看了很多资料,还是有点不甚理解,学习本身就是一个混沌的到模糊再到清晰的过程。开始学的时候,找到一点零碎的内容,似懂非懂地看着,感觉可以理解,但是不知道如何应用,找到官网的资料学习,稍微系统了,但是总感觉很吃力,或许是因为英文文档的缘故,和自己内心一丝丝的急迫感,难以静下心来仔细斟酌。终于,系统不系统地一些资料后,彻底晕乎了。静下心来,稍微整理下这写内容,发现并没有那么多,关键还是自己心态和学习方式的问题,特收藏在此,加深印象。
下面是官网对于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
![]() Introduction
Goals of the new Build System
The goals of the new build system are:
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:
Requirements
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:
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.
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:
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:
For the Android plugin, extra files and folders specific to Android:
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:
} 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:
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 { resources.srcDirs = ['src'] |