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

Source: Internet
Author: User

Gradle is the new generation build Systemfor Android and the default build tool for Android Studio.

The Gradle script is based on a JVM language-Groovy, plus a DSL (domain-specific language) 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.

Due to the Gradle syntax and the fact that most Java packages can be used, 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 the module contained is configured through Settings.gradle. Because the App folder is a module, 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 not place your application dependencies here; they belong        // in the individual module build.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 {    url ‘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.

Last is App/build.gradle

Apply plugin: ' Com.android.application ' android {compilesdkversion buildtoolsversion "21.1.1" compileoptions { Sourcecompatibility javaversion.version_1_7 targetcompatibility Javaversion.version_1_7} defaultConf        IG {ApplicationID "your.application.id" Minsdkversion targetsdkversion versioncode 2            Versionname "2.0.0"} signingconfigs {release {storefile file (' Release.keystore ')        Storepassword "Yourstorepassword" Keyalias "Yourkeyalias" Keypassword "Yourkeypassword"} Debug {storefile file (' Debug.keystore ')}} buildtypes {release {minify Enabled true Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro ' sign Ingconfig Signingconfigs.release} debug {signingconfig Signingconfigs.debug}} PR oductflavors {inNER {ApplicationID "your.application.inner.id" Versionname "2.0.0"} market {    }}}repositories {flatdir {dirs ' libs '}}dependencies {//General compile 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

compile name: ‘volley‘, ext: ‘aar‘
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 {        dirs ‘libs‘    }}
Common methods of use include AAR packages

There are two situations when using AAR

AAR in local directory
    1. First, add the calling method to the Android parameter closure repositories

      repositories {    flatDir {        dirs ‘libs‘    }}
    2. Then add the dependencies in the parameter closure

      compile name: ‘volley‘, ext: ‘aar‘
AAR located in remote warehouses

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:latest.integration‘
Package dependency
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‘ {    exclude module: ‘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, so when using Gralde often encounter some problems that can not get the remote dependency package, the simplest solution is to take the dependency package to download the local.

Get a quick grasp of how gralde is used in 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.