Original address: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Using-sourceCompatibility-1.7
Translation: Android Studio Chinese Group (sledgehammer translation)
Given that Android Studio uses Gradle to build projects, there are a number of headaches that can be encountered during the IDE's use. Sledgehammer is also the first contact, in order to have a basic understanding, special to find the official Android manual, and we learn together. Translation of the wrong place, I hope to point out.
Why do you use Gradle?
Gradle is a more advanced build system and a good build tool that allows custom build logic through plugins
The following are the main reasons why Android Studio chooses Gradle:
- Use domain-specific languages (domain specific Language) to describe and manipulate building logic. (hereinafter referred to as DSL)
- Based on groovy. DSLs can mix various declarative elements and manipulate these DSL elements in code to achieve logical customization.
- Support for existing MAVEN or Ivy warehouse infrastructure
- Very flexible, allowing the use of best practices, does not force you to follow its principles.
- Other plugins can expose their own DSL and API to let Gradle build files for use.
- Allows IDE integration, is a good API tool
Need to prepare:
- Gradle 1.6 or 1.7
- SDK with Build Tools 17.0.0 (released 5/16/2013)
Basic Project
In the root directory of the Gradle project, there is a file called Build.gradle, which describes the overall building base of the project.
Build File
The most basic Java program, its build.gradle file is a word:
Apply plugin: ' java '
The most basic Android project, it's Build.gradle as follows:
Buildscript {
repositories {
Mavencentral ()
}
dependencies {
Classpath ' com.android.tools.build:gradle:0.5.6 '
}
}
Apply plugin: ' Android '
Android {
Compilesdkversion 17
}
Let's take a step-by-step look at the contents of the above three sections.
- buildscript{.} Configures the code for the drive build, which declares that it will take a classpath dependency in the Maven central repository, that is, Android plugin for Gradle v0.5.6
- Apply plugin indicates that the plugin used is Android, as in the previous Java program, plugin is Java
- Android{...} All Android built parameters are configured here, which is the entry point for the Android DSL.
By default, only the target compilation environment is necessary, that is, the Compilesdkversion property. This is similar to the target property used in Project.Properties.
It is important to note that if you write apply Plugin:java instead of apply plugin:android on an Android project, the build will fail.
[Transfer]-gradle User Manual (a): Why use Gradle?