Original link: http://stormzhang.com/devtools/2014/12/18/android-studio-tutorial4/
In fact, very early also wrote a Gradle basic blog, but the time is long, now Gradle has been updated a lot, so for a while Stduio 1.0 official version with the latest Gradle syntax to explain in detail, small partners directly with me step by step to learn it.
What is Gradle?
Gradle is a dependency management tool, based on the groovy language, which is oriented toward Java applications, which discards the cumbersome configuration of XML-based, and replaces it with a groovy-based internal domain-specific (DSL) language.
Installing Gradle
In the Android Studio series Tutorial one--download and install the new project after the success will download Gradle, it seems that the process is not FQ is also available to download, but the visit is particularly slow, recommended FQ download. So where does the downloaded gradle go?
The /users/< user name >/.gradle/wrapper/dists directory is downloaded by default on Mac
The win platform is downloaded to the C:\Documents and settings\< user name >.gradle\wrapper\dists directory by default
You will see this directory has a Gradle-x.xx-all folder, if the download is too slow, but do not want to FQ, you can manually download the corresponding version of the Gradle official website, and then download the. zip file (can also be unzipped) Copy it to the Gradle-x.xx-all folder above, but it is recommended to have it downloaded directly.
Gradle Basic Concepts
Below with my open source project 9GAG to explain in detail the following and gradle related knowledge, and Gradle related to several files are generally as follows:
Red mark section from top to bottom let's step through the analysis:
1.9gag/app/build.gradle
This file is the Gradle configuration file for this module under the App folder, and it can be considered as the main gradle configuration file for the entire project, so let's look at the contents of this file:
Statement is Android App plugin: ' Com.android.application ' Android {//Compile SDK version Compilesdkversion//build Tools version This buildtoolsversion "21.1.1" Defaultconfig {//App package name ApplicationID "Me.storm.ninegag" Minsdkve Rsion targetsdkversion versioncode 1 Versionname "1.0.0"}//Java version compileoptions { Sourcecompatibility javaversion.version_1_7 targetcompatibility Javaversion.version_1_7} buildTypes { Release {//whether to confuse minifyenabled false//confuse file location Proguardfiles getde Faultproguardfile (' Proguard-android.txt '), ' Proguard-rules.txt '}}//Remove lint Check for error lintoptions {a Bortonerror false}}dependencies {//Compiles all jar packages under Libs directory compile Filetree (dir: ' Libs ', include: [' *.jar ']) compi Le ' com.android.support:support-v4:21.0.2 ' compile ' com.etsy.android.grid:library:1.0.5 ' compile ' Com.alexvasilkov: foldable-layout:1.0.1 ' Compile the Shimmerandroid module in the Extras directory Compile project (': Extras:shimmerandroid ')}
Here are a few things to note:
File at the beginning of apply plugin is the latest gradle version of the wording, the previous wording is apply plugin: ' Android ', if the previous wording, please correct it.
Buildtoolsversion This requires you to install the version locally, many people import new third-party libraries, one of the reasons for the failure is that the build version is incorrect, this can be changed manually to your local version or open SDK Manager To download the corresponding version.
ApplicationID represents the application of the package name, but also the latest wording, here is not more said.
Android 5.0 Start by default installation jdk1.7 to compile, but because the Mac system comes with the JDK version is 1.6, so you need to manually download jdk1.7 and configuration, specifically see my blog under the Mac to install and manage Java
Minifyenabled is also the latest syntax, early Runproguard, and this needs to be updated.
Proguardfiles This section has two paragraphs, the previous part represents the system default Android program confusion file, the file already contains the basic confusion statement, eliminates many of our things, the directory of this file in the <sdk directory >/tools/ Proguard/proguard-android.txt , the latter part is the custom obfuscation file in our project, the directory is in app/proguard-rules.txt , if you use Studio 1.0 new project created by default, the name of the file is Proguard-rules.pro , which is OK, in which you can declare some of the confusion rules that are dependent on third parties, because it is an open source project, 9GAG is not confused, The specific confusion of the grammar is not the scope of this blog discussion. The result of the final confusion is that the two parts of the file work together.
Compile project (': Extras:shimmerandroid ') This line is because there are other module in 9GAG, do not know the concept of module can see this blog Android Studio series Tutorial two-basic setup and run, In short you can understand the Android Library, due to the popularity of gradle and the improvement of remote warehouses, this dependence will gradually become very uncommon, but you need to know that there is such a dependency.
The above file is only the basic configuration, in fact, there are many custom parts, such as automatic packaging Debug,release,beta environment, signature, multi-channel packaging, follow-up will be explained separately.
2.9gag/extras/shimmerandroid/build.gradle
Each module needs to have a Gradle configuration file, the syntax is the same, the only difference is that the beginning of the declaration is the apply plugin: ' Com.android.library '
3.9gag/gradle
This directory has a wrapper folder, which can be seen there are two files, we mainly look at the contents of gradle-wrapper.properties this file:
#Thu Dec 16:02:24 CST 2014distributionbase=gradle_user_homedistributionpath=wrapper/distszipstorebase=gradle_ User_homezipstorepath=wrapper/distsdistributionurl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
You can see that the Gradle directory and download path as well as the current project using the Gradle version, these default paths we generally do not change, this file indicates that the Gradle version of the wrong is also a lot of reasons for unsuccessful lead package.
4.9gag/build.gradle
This file is the Gradle basic configuration file for the entire project, let's look at the contents
Top-level build file where can add configuration options common to all Sub-projects/modules.buildscript { repos itories { jcenter () } dependencies { classpath ' com.android.tools.build:gradle:1.0.0 ' }} allprojects { repositories { jcenter () }}
The main content contains two aspects: one is to declare the source of the warehouse, here you can see the indicated Jcenter (), the previous version is Mavencentral (), Jcenter can be understood as a new central remote warehouse, compatible with the MAVEN central warehouse, and better performance. Another is the version that declares Android Gradle plugin, the Android Studio 1.0 Official edition must require support for Gradle Plugin 1.0 version.
5.9gag/settings.gradle
This file is a global project configuration file, which mainly declares some need to join the Gradle module, we look at 9GAG the contents of the file:
Include ': App ', ': extras:shimmerandroid '
The appin the file, extras:shimmerandroid is the module, if there are other module needs to be added in the format as above.
Summarize
The basic knowledge about Gradle is introduced here, and then we will introduce a quick and easy way to compile and view third party open source projects, how to import Android Studio,gradle Common basic commands, multi-channel packaging configuration, etc. Have a question or find a mistake welcome you directly blog message.
Android Studio--gradle Basics (RPM)