Gradle file parsing of Android Studio

Source: Internet
Author: User

This article reprinted from Stormzhang's blog

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.

Gradle Basic Conceptsbelow 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.gradleThis 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" Minsdkversio        n targetsdkversion versioncode 1 Versionname "1.0.0"}//Java version compileoptions {         Sourcecompatibility javaversion.version_1_7 targetcompatibility Javaversion.version_1_7} buildTypes { Debug {//debug mode} release {//whether to confuse minifyenabled Fals        E//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 (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 the Shimmerandroid module under Extras directory Compile project (': Extras:shi Mmerandroid ')}
Here are a few things to note:
  • file Start Apply plugin is the latest version of Gradle , 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 You can manually change to your existing version or open the SDK Manager to download the corresponding version.
  • ApplicationID represents the package name of the app , but also the latest wording, there is no more to say.
  • 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 newest grammar , very early is runproguard, this also needs to update under.
  • Proguardfiles This part has two paragraphs, the previous section represents the system default Android program for confusing files , the file already contains the basic confusion statement, which eliminates many of our things, the directory of this file in **/tools/proguard/proguard-android.txt**, The latter part is a custom obfuscation file in our project , the directory is in **app/proguard-rules.txt**, if you create a new project with Studio 1.0 by default the file name is **proguard-rules.pro**, this name doesn't matter, In this file 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 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 II – Basic setup and operation, 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/gradleThis 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 the download path and the Gradle version used by the current project are declared, these default paths are not normally changed. The Gradle version indicated in this file is also one of the reasons why many of the lead packages are unsuccessful. .

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 the source of the declaration warehouse , here you can see is the specified Jcenter (), the previous version is Mavencentral (), Jcenter can be understood as a new central remote repository, compatible with the MAVEN central repository, and with 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 project configuration file for the whole office. , which mainly declare 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.



Gradle file parsing of Android Studio

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.