A simple understanding of what Gradle,gradle is, a minute to get started Gradle

Source: Internet
Author: User
Tags version control system jcenter

The Gradle build script uses DSL (Domain specific Language) to describe the build logic, using the language Groovy . To learn about the Gradle build system for Android Studio Engineering, you can start with,,, and Project的settings.gradle Project的build.gradle Module的build.gradle gradle/wrapper analyze these files.

1. Project's Settings.gradle

This file describes which module (s) are included in project.

include ‘:app‘, ‘:lib‘

2. Project's Build.gradle

This file describes the repositories referenced by the Gradle build and the most basic dependencies.

Buildscript{Repositories{ Supported warehouses include Jcenter, mavencentral and Ivyjcenter() } dependencies { //Base dependent classpath ' Com.android.tools.build:gradle : 1.0.1 ' //Note:do not place your application dependencies Here:they belong //in the individual mod Ule build.gradle files }}allprojects { repositories { jcenter()  }}< /c7> 
3. Module's Build.gradle

This file describes some of the configuration of the main module.

Apply Plugin: ‘Com.Android.Application’ Add an Android plugin for the Gradle build processAndroid{ All Android-related build parametersCompilesdkversion19 Compile version numberBuildtoolsversion“19.0.0”Build tool version number. Need to be greater than or equal to Compilesdkversion and targetsdkversionDefaultconfig{ Androidmanifest.xml configuration of the relevant parameters, allowing the configuration of the manifest file to be overwrittenApplicationID“Com.Example.My.App” Unique identification code for different package only in Build.grade fileMinsdkversion8Targetsdkversion19Versioncode1Versionname"1.0" }Buildtypes{Build and package by default with Debug and Release2, where debug package is signed with Debug key, release package default no signatureRelease{MinifyenabledTrueProguardfiles Getdefaultproguardfile(' Proguard-android.txt '), ' Proguard-rules.pro '//File confusion. Where Proguard-android.txt is the default obfuscation configuration, Proguard-rules.pro for the module additional obfuscation configuration}}}dependencies {//Module dependent Library compile project (": Lib")// Library module relies on compile 'com.  Android.  Support:appcompat-v7:19.0.  1'//remote library dependent, in the format group:name:version compile Filetree(dir: ' Libs ',  Include: [' *.jar '])//local library dependency, including all jar files under the App/libs directory. So when the module wants to refer to a jar, just copy the jar to <modulename>/libs}         

ApplicationID can also be used for different product flavour/build types using different ApplicationID scenarios:

productFlavors {    pro { applicationId = "com.example.my.pkg.pro" } free { applicationId = "com.example.my.pkg.free" }}buildTypes { debug { applicationIdSuffix ".debug” //applicationId后缀 }}....
4. Gradle Wrapper

Gradle Wrapper literally understands Gradle packaging, Android Studio uses Gradle wrapper to fully embed the Gradle Android plugin. I understand that Android studio cannot directly use Gradle's Android plugin and needs to wrap a layer of references.

Gradle/wrapper belongs to the project level file (NOTICE: needs to be added to the version control system). Several files are included:

    1. Gradle-wrapper.jar file
    2. Gradle-wrapper.properties file
    3. Shell scripts under the Windows platform (optional)
    4. Shell scripts under the Mac/linux platform (optional)

Android Studio reads the configuration of the properties file and runs the wrapper file under the current directory.

The contents of the Gradle-wrapper.properties file are as follows, if you want to use the specified Gradle, modify the Distributionurl.

distributionBase=GRADLE_USER_HOMEdistributionPath=wrapper/distszipStoreBase=GRADLE_USER_HOMEzipStorePath=wrapper/distsdistributionUrl=https\://services.gradle.org/distributions/gradle-2.4-all.zip

The shell script under wrapper is only valid in the command line or in a non-Android studio environment, not under Android Studio.

5. About Build variants

Build variants I understand that the different apk is built based on the same code. productFlavors buildTypes It is usually built on and two dimensions, and of course there are other dimensions (such as Cpu/abi, which can be defined as needed).

If Productflavors contains Baidu and Wandoujia two kinds, buildtypes containing release and debug two, you can build 4 different packages, respectively, Baidurelease, Baidudebug, Wandoujiarelease, Wandoujiadebug.

Different packages, if there is a difference in functionality, may appear as a form of this style of directory src/main/ , src/<buildType>/ src/<productFlavor>/ . The final package merge is prioritized from high to low src/<buildType>/ -> src/<productFlavor>/ -> src/main/ -> libraries/dependencies的src , and high priority overrides low priority content.

6. Build Tasks

The Android studio build system defines a series of build tasks, top-level tasks include: Assemble, check, build, clean.

A more detailed description of the task can be studied under Gradle official documentation.

7. Meet the differentiated requirements of different build packages

This section is also based on build variants.

The usual procedure is:

    1. In a build.gradle file that defines different channels ofproduct flavors
    2. For differentiated product flavor , create a corresponding source directorysrc/<productFlavor>
    3. src/<productFlavor>Add a differentiated code under
    4. The above actions also apply buildTypes , if necessary. Less involved in actual development, usually only in the case of productflavors and different channels

Suppose Baidu mobile Phone assistant channel package has some differentiation function: There are 2 pages, firstactivity jump to Secondactivity, where firstactivity the same, secondactivity features are different.

(1) First in the Build.grade file productflavors add baidu{}, on behalf of Baidu Mobile phone assistant channel package.

...Android{     ...  Defaultconfig {  ...  } Signingconfigs { Span class= "pun" > ...  } Buildtypes {  ...  } productflavors {{ } full { } }} ...              

(2) in the APP/SRC directory to add Baidu package, Baidu directory structure and main to maintain consistency, according to the actual situation only add the difference part. Here, add the Secondactivity.java file under the Baidu package and customize the implementation.

(3) Since Baidu and the main directory of the Secondactivity package name is the same, so in main firstactivity start.

This article from a learning sister, to the original tribute

A simple understanding of what Gradle,gradle is, a minute to get started Gradle

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.