Android Studio Tutorial (iv): Gradle Foundation _android

Source: Internet
Author: User
Tags wrapper jcenter

In fact, very early also wrote a Gradle basic blog, but the time is very long, now Gradle has been updated a lot, so the combination of Stduio 1.0 official version and the latest Gradle grammar to explain in detail, small partners directly with me step by step to learn.

What is Gradle?

Gradle is a dependency management tool, based on the groovy language, which is focused on Java applications, discarding the complex xml-based configuration and replacing it with an in-house domain-specific (DSL) language based on groovy.

Install Gradle

In the Android Studio series Tutorial-download and install the new project will be downloaded after the success of the Gradle, seemingly this process is also available to download the wall, but the visit is particularly slow, the proposed rollover wall download. So where does the gradle of downloads go?

The default download to the **/users/< user name >/.gradle/wrapper/dists** directory on the Mac

The win platform downloads 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 turn the wall, you can manually to gradle website download the corresponding version, and then download the. zip file (also can extract) Copy to the Gradle-x.xx-all folder above, but it is recommended that you download it directly.

Gradle Basic Concepts

The following is my open source project 9GAG to explain in detail and gradle related knowledge, and gradle related to several documents generally have the following:

Red mark part from top to bottom let's take a step-by-step analysis:

1.9gag/app/build.gradle

This file is the Gradle configuration file for this module under the App folder, or the most important gradle configuration file for the entire project, and we'll look at the contents of this file:

The statement is the Android application plugin: ' Com.android.application ' Android {//Compiler SDK version compilesdkversion//build Tools
    Version buildtoolsversion "21.1.1" Defaultconfig {//Application package name ApplicationID "Me.storm.ninegag" Minsdkversion 14 Targetsdkversion versioncode 1 Versionname "1.0.0"}//Java version compileoptions {Sourcecompatibili Ty javaversion.version_1_7 targetcompatibility javaversion.version_1_7} buildtypes {debug {//Debu G Mode} release {//whether to confuse minifyenabled false//confuse the location of the file Proguardfiles Getdefaultprog Uardfile (' Proguard-android.txt '), ' Proguard-rules.txt '}}//Remove lint Check error lintoptions {Abortonerror FA LSE}} dependencies {//compile all jar packs in Libs directory compile Filetree (dir: ' Libs ', include: [' *.jar ']) compile ' 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 Extras directory under SHImmerandroid Module Compile project (': Extras:shimmerandroid ')} 

Here are a few points to note:

File opening Apply plugin is the latest gradle version of the writing, the previous writing is apply plugin: ' Android ', if the previous wording, please correct over.

Buildtoolsversion This requires you to install the version locally, many people import a new Third-party library, one of the reasons for failure is build version of the wrong, this can be manually changed 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 to say more.

Android 5.0 starts with the default installation jdk1.7 to compile, but since the MAC system has a JDK version of 1.6, you need to manually download jdk1.7 and configure it to see my blog mac install and manage Java

Minifyenabled is also the newest syntax, which is runproguard early on, and this needs to be updated.

Proguardfiles This section has two paragraphs, the previous part represents the system default Android program's confusing file, which already contains the basic obfuscation statement, eliminates many of our things, this file's directory in **/tools/proguard/ proguard-android.txt**, the latter part is the custom confusing file in our project, the directory is in **app/proguard-rules.txt**, if you create a new project with Studio 1.0 The default generated file name is * * proguard-rules.pro**, this name does not matter, in this file you can declare some third-party dependencies of some confusing rules, because it is open source project, 9GAG is not confused, the specific confusion of the syntax is not the scope of this blog discussion. The end result of the confusion is that the two parts of the document work together.

Compile project (': Extras:shimmerandroid ') is because there are other modules in the 9GAG, the concept of module is not known to see this blog Android Studio Series II-Basic setup and running, In short, you can understand that the Android Library, due to the popularity of gradle and the improvement of the remote warehouse, is becoming very uncommon, but you need to be aware of this dependency.

The contents of the above file is only the basic configuration, in fact, there are many custom parts, such as automatic packaging debug,release,beta, such as environment, signature, multi-channel packaging, and so on, the follow-up will be taken out alone to explain.

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 initial statement is Apply plugin: ' Com.android.library '

3.9gag/gradle

This directory has a wrapper folder, which can see two files, we mainly look at the contents of the gradle-wrapper.properties file:

#Thu Dec 16:02:24 CST 2014
distributionbase=gradle_user_home
distributionpath=wrapper/dists
Zipstorebase=gradle_user_home
zipstorepath=wrapper/dists
distributionurl=https\://services.gradle.org /distributions/gradle-2.2.1-all.zip

You can see that the Gradle directory and download path and the Gradle version used by the current project are declared inside, and these default paths are generally not changed, and the Gradle version that is indicated in this file is not one of the reasons for the failure of many packages.

4.9gag/build.gradle

This file is the Gradle basic configuration file for the entire project, so let's take a look at what's inside.

Top-level build file where your can add configuration options common to all sub-projects/modules.

Buildscript {
  repositories {
    jcenter ()
  }
  dependencies {
    classpath ' com.android.tools.build: gradle:1.0.0 '
  }
}

allprojects {
  repositories {
    jcenter ()
  }
}

The content consists of two aspects: one is the source of the declaration warehouse, here is the specified Jcenter (), the previous version is Mavencentral (), Jcenter can be understood as a new central remote warehouse, compatible with the MAVEN center warehouse, and better performance. The other is a version of the Android Gradle plugin, and the Android Studio 1.0 official version must require support for the Gradle Plugin 1.0 version.

5.9gag/settings.gradle

This file is a global project configuration file that mainly declares some module that needs to be added to the gradle, and we look at the contents of the 9GAG file:

Include ': App ', ': extras:shimmerandroid '

The appin the file, extras:shimmerandroid is all module, and if there are other module, it needs to be added in the format.

Summarize

The basic knowledge about Gradle is introduced here, next, I will introduce a quick and convenient way of compiling and viewing the third party open source project, how to import the Android Studio,gradle Common basic commands, multi-channel package configuration, etc. Have questions or find errors Welcome to the direct Blog message.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.