History of the most detailed Android Studio series tutorial Four--gradle basics

Source: Internet
Author: User
Tags maven central jcenter

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:

The statement is an Android app for apply plugin:' Com.android.application ' Android {Compiling the SDK version compilesdkversion21stBuild Tools Version Buildtoolsversion"21.1.1" Defaultconfig {App's package name ApplicationID"Me.storm.ninegag" minsdkversionTargetsdkversionVersioncode1 versionname"1.0.0"}Java version compileoptions {sourcecompatibility javaversion.version_1_7 targetcompatibility JavaVersion.VERSION_1_7} Buildtypes {release {Whether to confuse minifyenabledfalse //confuse file location proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.txt ' }} //Remove lint Check for error lintoptions {Abortonerror false}}dependencies { //Compile libs directory for all Jar packages compile Filetree (d IR: ' 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 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 file name is Proguard-rules.pro, which is OK, in this file you can declare some third-party dependencies of some confusion 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 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 18 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

sub-projects/modules.buildscript {    repositories {        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 app in the file, Extras:shimmerandroid is the module, if there are other module needs to be added in the format as above.

History of the most detailed Android Studio series tutorial Four--gradle basics

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.