Get a quick grasp of how Gradle is used in Android Studio

Source: Internet
Author: User
Tags jcenter

Gradle is a new generation of build Systemthat can be used for Android development and is the default build tool for Android Studio.

The Gradle script is based on a JVM language-Groovy, plus a DSL (domain-specific language).

Because groovy is the JVM language, you can use most of the Java language libraries. The so-called DSL is a plug-in developed specifically for Android, such as some new methods (method), closures (Closure) , and so on, outside of the standard Gradle.

Because Gradle's syntax is concise enough and can be used in most Java packages, it is well-deserved to be the next generation Build System.

When you create a new project using Android Studio, two Build.gralde files are generated by default, one located in the project root directory and one in the app directory. There's another file--settings.gradle.

The script file under the root directory is the global configuration for module , and all of the module that the action threshold contains is configured by Settings.gradle.

The App folder is a module, and if you add a new module- Libin the current project, you need to include the new module in the Settings.gralde file.

Basic structure of the Gradle script

In my current project, for example, the root directory of the Build.gradle content is as follows:

 //  top-level build file where you can add Configuration options common to all sub-projects/modules.  buildscript {repositories {jcenter ()} dependencies {Classpath  ' COM.ANDROID.TOOLS.BUILD:GRADLE:1.0.0-RC4 ' //  Note:do your application dependencies here; They belong   in the individual module B Uild.gradle files  }}allprojects {repositories {jcenter () Maven {URL  ' http://mvnrepo.xxx.com/mvn/repository '  

classpath ‘com.android.tools.build:gradle:1.0.0-rc4‘Is the Android-specific plugin, the Maven repository is located via method Jcenter () , which is also the default Maven repository. Of course, you can also add additional MAVEN warehouse addresses, such as those in the above file

maven {    ' http://mvnrepo.xxx.com/mvn/repository '}

Then the Settings.gradle file:

Include ': App '

An app is a module that contains a project, and if you have more than one module, you can add multiple parameters to the Include method.

Finally, the App/build.gradle:

Apply plugin: ' Com.android.application 'Android {compilesdkversion21stbuildtoolsversion"21.1.1"compileoptions {sourcecompatibility javaversion.version_1_7 targetcompatibility JavaVersion.VERSION _1_7} defaultconfig {ApplicationID"Your.application.id"minsdkversion14targetsdkversion21stVersioncode2Versionname"2.0.0"} signingconfigs {release {StoreFile file (' Release.keystore ') Storepassword"Yourstorepassword"Keyalias"Yourkeyalias"Keypassword"Yourkeypassword"} debug {storefile file (' Debug.keystore ')}} buildtypes {release {minifyenabledtrueproguardfiles Getdefaultproguardfile (' Proguard-android.txt '), ' Proguard-rules.pro 'signingconfig signingconfigs.release} debug {signingconfig signingconfigs.debug }} productflavors {inner {ApplicationID"Your.application.inner.id"Versionname"2.0.0"} Market {}}}repositories {flatdir {dirs' Libs '}}dependencies {//GeneralCompile name: ' Volley ', ext: ' AAR 'Compile' com.nostra13.universalimageloader:universal-image-loader:1.9.3 'Compile' Com.alibaba:fastjson:latest.integration '//Project related (deleted)}
Groovy's basic syntax method invocation
Apply plugin: ' Com.android.application '

The above statement apply is a method that passes a parameter to it, and plugin plugin the value is ‘com.android.application‘ .

If there are multiple parameters, they are separated by commas, for example

<code>compile name: ' Volley ', ext: ' AAR ' </code>
Closed Package

The portion of the curly braces contained in groovy becomes a closure (Closure). For example, the following code

compileoptions {    sourcecompatibility javaversion.version_1_7    targetcompatibility JavaVersion.VERSION_1_7}

compileOptionsis a method, its parameters are a closure, this closure in turn executes two methods- sourceCompatibility and targetCompatibility , parameters are JavaVersion.VERSION17 .

Closures can also be nested with

Repositories {    Flatdir {        ' libs '    }}
Common use Method package dependency (AAR)


There are two situations when using AAR

①aar in the local directory

1. First add the calling method to the Android parameter closure repositories

Repositories {    Flatdir {        ' libs '    }}

2. Then add the parameter closure in the dependencies

Compile name: ' Volley ', ext: ' AAR '
②aar located in the remote repository

Here, for example, MAVEN can also use other types of warehouses, such as Ivy.

You just need to add one after the jar package reference @aar .

Compile ' com.alibaba:fastjson:[email protected] '
Package Dependency (JAR)
Compile group: ' Com.alibaba ', module: ' Fastjson ', version: ' Latest.integration '

can be simply written

Compile ' com.alibaba:fastjson:latest.integration '

latest.integrationCan be replaced by a specific version number, here is the latest version on the server.

Remove Duplicate dependencies
Compile ' com.alibaba.fastjson.latest.integration ' {    ' annotations ', group: ' Com.google.android '} 
Using Java7
compileoptions {    sourcecompatibility javaversion.version_1_7    targetcompatibility JavaVersion.VERSION_1_7}
Productflavors

For different app distribution channels, we can define different product flavor. You can also define a build and an external version , which contains debugging code that is not compiled into the final app when it is published. You can also specify different versions for both the build and the external version ApplicationId , so that you can install two simultaneously on the same device to facilitate debugging.

Command line Execution Gradle script

A shell script will be generated automatically in the Android root directory- Gradlew, and the X attribute is added before execution-chomod +x gradlew

The Gradle script contains a number of tasksthat can be specified by the task name to be executed.

    • ./gradlew Build
    • ./gradlew Assemble
    • ./gradlew Assembleinnderdebug
Summarize

I have to say, gradle really good! Although Gradle can be used in conjunction with ANT or MAVEN, it is much more concise and functional than the other two. I am now developing a project commonly used by MAVEN, do not know what reason, when using gradle often encounter some can't get remote dependency package problem, the simplest solution is to put the dependency package download Local.

Get a quick grasp of how Gradle is used in Android Studio (GO)

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.