Why use gradle?
Gradle is a relatively advanced building system and a good building tool that allows you to customize building logic through plug-ins
The following are the main reasons why Android studio chooses gradle:
- Use domain specific language to describe and process the building logic. (DSL)
- Based on groovy. DSL can mix various declarative elements and use code to manipulate these DSL elements for logical customization.
- Support existing Maven Or Ivy warehouse infrastructure
- It is very flexible. The use of best practices is not mandatory for you to follow its principles.
- Other plug-ins can expose their DSL and APIs for use in gradle build files.
- It is a good API Tool to allow ide integration.
Preparations
- Gradle 1.6 or 1.7
- SDK with build tools 17.0.0 (released 5/16/2013)
Basic Project
Under the root directory of the gradle project, there is a file named build. gradle, which describes the overall construction basis of this project.
Build File
The most basic Java program, its build. gradle file is one sentence:
[Plain]View plaincopy
- Apply Plugin: 'java'
For the most basic Android project, its build. gradle is as follows:
[Plain]View plaincopy
- Buildscript {
- Repositories {
- Mavencentral ()
- }
- Dependencies {
- Classpath 'com. Android. Tools. Build: gradle: 0.5.6'
- }
- }
- Apply Plugin: 'android'
- Android {
- Compilesdkversion 17
- }
Let's analyze the content of the above three parts step by step.
- Buildscript {...} is configured with the driver build code, which is declared to be in the maven central repository, taking a classpath dependency, that is, Android plugin for gradle v0.5.6
- Apply plugin indicates that the plugin used is Android, just as in the previous Java program, the plugin used is Java.
- All Android build parameters are configured in Android {...}, which is the entry point of Android DSL.
By default, only the target compiling environment is necessary, that is, the attribute compilesdkversion. This is similar to the target attribute in project. properties.
It is worth noting that if you write Apply Plugin: Java rather than apply Plugin: Android in the android project, the build will fail.
[Development tool] gradle User Manual: Why should I use gradle?