Android Development: "Gradle Recipes for Android" reading notes (translation) 4.1--writing your own tasks

Source: Internet
Author: User

Problem:

You want to customize the Gradle build process with your own tasks.

Solution:

Adds a task element to the Gradle build file. The extra property supported by the Android plugin makes development easier.

Discuss:

Gradle's DSL supports the use of task blocks to define their own tasks. The API includes a very wide range of tasks (like Copy,wrapper and exec) that you can simply configure to use.

For example, the copy task contains the From and to properties, and the from block can set files that exclude the specified format file name. Copy all the apk without the signature to the new folder, the task is as follows:

Task Copyapks (type:copy) {from    ("$buildDir/outputs/apk") {        exclude ' **/*unsigned.apk ', ' **/*unaligned.apk '    }    Into '. /apks '}

The BuildDir property is related to the default build directory, and the $ symbol is used to insert it into the groovy string (using double quotation marks). The document for the copy task shows that the exclude block supports Ant-style file directory names, meaning * * matches all subdirectories.

If you want to simply configure an existing Gradle task, you need to understand the difference between the configuration and execution stages in Gradle. In the configuration phase, Gradle builds a DAG based on dependencies. Then perform the feature task. All tasks are configured before they are executed.

Gradle is fond of declarative tasks. Like the example above, you specify what you want to do, not how. If you need to execute a command, add a dolast block to the Gradle task:

Task Printvariantnames () {    Dolast {        Android.applicationVariants.all {variant-            println Variant.name        }     }}

Any dolast blocks that are executed in front of or behind a task will be executed during the configuration. The code inside the Dolast block executes during execution.

Android Plugin adds an Android attribute, corresponding to having a Applicationvariants property can return all combinations of buildtype/flavor. In this case, they can output on the console.

Install all debug flavors on the same device (assuming they have a unique ApplicationID value), you can use the following tasks:

Task Installdebugflavors () {    Android.applicationVariants.all {v-         if (V.name.endswith (' Debug ')}) {               String name = V.name.capitalize ()               dependsOn "Install$name"}}     }

The DependsOn method shows that this is part of the configuration phase, not execution. Each variant of the name, like Friendlydebug, is capitalized (Friendlydebug), and then the corresponding installation task (INSTALLFRIENDLYDEBUG) is added to the installdebugflavors task.

This mission is in the configuration phase, Installarrogantdebug,installfriendlydebug, Installobsquiousdebug are added to installdebugflavors as dependencies. Therefore, performing the Installdebugfalvors task will also require three flavor to be installed.

Writing a custom task requires a bit of groovy just. Further discussion is somewhat beyond the scope of this book. But there are some good books, and the extra groovy concept is introduced in it.

Android Development: "Gradle Recipes for Android" reading notes (translation) 4.1--writing your own tasks

Related Article

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.