Android (Java) Learning Note 126:android Studio Build.gradle Introduction

Source: Internet
Author: User
Tags parse csv file maven central jcenter

1. First we go directly to the code introduction:

//top-level build file where can add configuration options common to all sub-projects/modules.Buildscript {repositories {//Here you can see the indicated 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. jcenter ()} dependencies {//the version of Android Gradle plugin is declared to be Gradle plugin 1.2.3Classpath ' com.android.tools.build:gradle:1.2.3 '//Note:do not place your application dependencies here; they belong//In the individual module build.gradle files}}allprojects {repositories {jcenter ()}}

the declaration in Buildscript is the resource that Gradle runs the script itself (which can be understood as a script-run environment build), the resources that can be declared include dependencies, third-party plug-ins, maven warehouse addresses, and so on. information such as dependencies, warehouse addresses, etc. that are directly declared in the Build.gradle file is the resource that the project needs .

Here we introduce the maven repository First:

First, what is Maven repository?

In the case of MAVEN, for example, when we used ant to build a project, under the project directory, we would often see a subdirectory called/lib, where all kinds of third-party dependent jar files, such as Log4j.jar,junit.jar and so on, are stored. For each project you build, you need to create such a/lib directory, and then copy a pair of jar files, which is obviously duplicated. Repetition is always the starting point for nightmares, and multiple projects that do not share the same jar files will not only result in wasted disk resources, but also make consistency management of versions difficult. Also, if you use version management tools such as SVN (you are not using the version management tool?). Try SVN now, it can help you solve a lot of headaches, you need to submit a large number of jar files to the code base, but the version management tool in the processing of binary files is not good.

The Maven repository is where all the jar files (war,zip,pom, etc.) are placed, and all MAVEN projects can get the dependency jar they need from the same MAVEN repository, which saves disk resources. In addition, because all jars in the Maven repository have their own coordinates, the coordinates tell maven its group ID, artifact ID, version, packaging method, and so on, so MAVEN projects can be easily dependent on version management. Neither do you. To submit the jar file to the SCM repository, you can create an organization-level MAVEN repository for all members to use.

In short, maven repositories can help us manage artifacts (primarily jars).

Gradle is written in the groovy language, supports groovy syntax, has the flexibility to use the various ant plugins already in place, the JVM-based class library, which is more powerful than the build scripts such as Maven and Ant. Although Gradle supports out-of-the-box, if you want to use some third-party plugins, class libraries, etc. in your scripts, you will need to manually add references to those plugins and class libraries yourself. Instead of directly serving projects, these plugins and libraries support the operation of other build scripts. So you should place a reference to this part in the Buildscript code block. Gradle executes the script, it takes precedence over the contents of the Buildscript code block before the rest of the build script is executed.

For example, suppose we want to write a task that parses a CSV file and outputs its contents. Although we can use Gradle to write the code to parse the CSV file, but in fact Apache has a library has implemented a parse CSV file library for us to use directly. If we want to use this library, we need to include a reference to the library in the Gradle.build file.

You use Jcenter () in Buildscript's repositories statement, which he can understand as a new central remote repository, compatible with MAVEN central repository, and better performance

2.

Build.gradle:

  buildscript {repositories {Mavenloca L () mavencentral ()} dependencies {classpath  ' org.apache.commons:commons-csv:1.0 ' ))  for  Span style= "color: #000000;" > (item in records) {print item.get ( 0) + '  println Item.get (  1   

Repositories and dependencies in Buildscript code blocks are used almost exactly the same way as they are used directly in Build.gradle files. The only difference is that you can use the Classpath declaration with dependencies in the Buildscript code block. The Classpath declaration illustrates that class loader can use the dependencies that you provide when executing the rest of the build scripts. This is exactly what we use to Buildscript blocks of code.

If you need to use the class library in your project, you need to define the dependencies code block outside of the Buildscript code block. So it's possible to see the following code appear in Build.gradle:

Build.gradle:

repositories {mavenlocal () mavencentral ()}dependencies {compile' Org.springframework.ws:spring-ws-core:2.2.0.release ',            ' org.apache.commons:commons-csv:1.0 '}buildscript {repositories {mavenlocal () mavencentral ()} dependencies {Classpath ' org.apache.commons:commons-csv:1.0 '    }}Importorg.apache.commons.csv.*Task Printcsv () {dolast {def records= CSVFormat.EXCEL.parse (NewFileReader (' Config/sample.csv '))         for(item in records) {print Item.get (0) + "println Item.get (1)        }    }}

Android (Java) Learning Note 126:android Studio Build.gradle Introduction

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.