Android Gradle Plugin Guide (vi)--Advanced build Customization

Source: Internet
Author: User

Original address: Http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Advanced-Build-Customization


7, advanced build Customization (high-level building customization)
7.1 Build options (build option)
7.1.1 Java Compilation options (Java compilation option)
    Android {        Compileoptions {            sourcecompatibility = "1.6"            targetcompatibility = "1.6"        }    }

The default value is "1.6". This setting will affect all task compilation Java source code.


7.1.2 aapt options (aapt option)
    Android {        aaptoptions {            nocompress ' foo ', ' Bar '            ignoreassetspattern '!. Svn:!. Git:!. ds_store:!*.scc:.*:<dir>_*:! Cvs:!thumbs.db:!picasa.ini:!*~ "        }    }

This will affect all tasks that use AAPT.


7.1.3 Dex options (DEX option)
    Android {        Dexoptions {            incremental false            Predexlibraries = False            Jumbomode = False        }    }

This will apply all of the tasks that use Dex.


7.2 Manipulation Tasks (Operation Task)

The underlying Java project has a limited set of tasks that are used to process each other to produce an output.

Classes is a task that compiles Java source code. Classes can be easily used in Build.gradle files through scripting. This is the abbreviation of project.tasks.classes .


In Android projects, this is a bit more complicated. Because there will be a large number of identical tasks in the Android project, their names are generated based on build types and product flavor.


To solve this problem, the Android object has two properties:

* Applicationvariants (applies to app plugin only)

* Libraryvariants (only for library plugin)

* Testvariants (for all two plugin)

These three will return a domainobjectcollectionof Applicationvariant, Libraryvariant, and Testvariant objects, respectively.


Note that using one of these three collection will trigger the generation of all corresponding tasks. This means that you do not need to change the configuration after you use collection.


Domainobjectcollection can access all objects directly, or filter by filter.

    Android.applicationVariants.each {variant---...    }

These three variant classes share the following attributes:

Property name Property type Description
Name String The name of the variant must be unique.
Description String Description of the Variant.
DirName String The subfolder name of the variant must also be unique. There may also be more than one subfolder, such as "Debug/flavor1"
BaseName String The base name of the variant output must be unique.
OutputFile File The output of the variant, which is a readable and writable property.
Processmanifest Processmanifest A task that handles manifest.
Aidlcompile Aidlcompile Compiles a task for the Aidl file.
Renderscriptcompile Renderscriptcompile Compiles a task for the Renderscript file.
Mergeresources Mergeresources A task for a mixed resource file.
Mergeassets Mergeassets A task that blends asset.
Processresources Processandroidresources A task that processes and compiles a resource file.
Generatebuildconfig Generatebuildconfig Generates a task for the Buildconfig class.
Javacompile Javacompile A task that compiles Java source code.
Processjavaresources Copy A task that handles Java resources.
Assemble Defaulttask The iconic assemble task of the variant.

The Applicationvariant class also has the following additional properties:

Property name Property type Description
BuildType BuildType Variant of the BuildType.
Productflavors List<productflavor> Variant of the Productflavor. It is generally not empty but also allows null values.
Mergedflavor Productflavor Merger of Android.defaultconfig and Variant.productflavors.
Signingconfig Signingconfig The Signingconfig object used by the variant.
Issigningready Boolean If true, it indicates that the variant already has all the necessary information to sign.
Testvariant Buildvariant Will test the variant's testvariant.
Dex Dex Package the code into a dex task. If the variant is a library, this value can be empty.
Packageapplication Packageapplication Package the task for the final apk. If the variant is a library, this value can be empty.
Zipalign Zipalign Zip compresses the APK task. If the variant is a library or the APK cannot be signed, this value can be empty.
Install Defaulttask The task that is responsible for the installation cannot be empty.
Uninstall Defaulttask The task that is responsible for unloading.

The Libraryvariant class also has the following additional properties:

Property name Property type Description
BuildType BuildType Variant of the BuildType.
Mergedflavor Productflavor The Defaultconfig values
Testvariant Buildvariant Used to test the variant.
Packagelibrary Zip The AAR file used to package the library project. If it is a library item, this value cannot be empty.

The Testvariant class also has the following properties:

Property name Property value Description
BuildType BuildType The variant's build Type.
Productflavors List<productflavor> Variant of the Productflavor. It is generally not empty but also allows null values.
Mergedflavor Productflavor Merger of Android.defaultconfig and Variant.productflavors.
Signingconfig Signingconfig The Signingconfig object used by the variant.
Issigningready Boolean If true, it indicates that the variant already has all the necessary information to sign.
Testedvariant Basevariant Basevariant of the Testvariant test
Dex Dex Package the code into a dex task. If the variant is a library, this value can be empty.
Packageapplication Packageapplication Package the task for the final apk. If the variant is a library, this value can be empty.
Zipalign Zipalign Zip compresses the APK task. If the variant is a library or the APK cannot be signed, this value can be empty.
Install Defaulttask The task that is responsible for the installation cannot be empty.
Uninstall Defaulttask The task that is responsible for unloading.
Connectedandroidtest Defaulttask A task that performs an Android test on the connected device upstream.
Providerandroidtest Defaulttask Task that performs Android testing using the extension API.

Android task-specific types of APIs:

* Processmanifest

* File Manifestoutputfile

* Aidlcompile

* File Sourceoutputdir

* Renderscriptcompile

* File Sourceoutputdir

* File Resoutputdir

* Mergeresources

* File OutputDir

* Mergeassets

* File OutputDir

* Processandroidresources

* File Manifestfile

* File Resdir

* File Assetsdir

* File Sourceoutputdir

* File Textsymboloutputdir

* File Packageoutputfile

* File Proguardoutputfile

* Generatebuildconfig

* File Sourceoutputdir

* Dex

* File OutputFolder

* Packageapplication

* File Resourcefile

* File Dexfile

* File Javaresourcedir

* File Jnidir

* File OutputFile

* use "outputFile" directly in the variant object to change the final output folder.

* Zipalign

* File Inputfile

* File OutputFile

* use "outputFile" directly in the variant object to change the final output folder.


The API for each task type is limited by how Gradle works and how Android plugin is configured.

First, gradle means that the owning task can only configure the path of the input and output and some of the option identifiers that may be used. Therefore, a task can only define some input or output.


Second, most of the task inputs are not single, and generally mix the values in Sourceset, Build type, and product flavor. To maintain the simplicity and readability of the build file, the goal is for developers to modify these objects in the DSL language in order to build the process of the decoration, rather than to delve into the options for input and task.


It is also important to note that except for the Zipalign task type, all other types require private data to be set to run. This means that it is not possible to automatically create new task instances of these types.


These APIs may also be changed. In general, the current API is to add additional processing (if needed) around the input and output portals of a given task. Feedback is welcome, especially those that have not been foreseen.


Refer to the Gradle documentation for Gradle task (defaulttask,javacompile,copy,zip).


7.3 BuildType and Product Flavor Property Reference (BuildType and product Flavor attribute reference)
Coming soon .... = =
Refer to the Gradle documentation for Gradle task (defaulttask,javacompile,copy,zip).

7.4 Using sourcecompatibility 1.7 (JDK 1.7 version of Sourcecompatibility)
With Android KitKat (version 19 of Buildtools) you can use the string in the Diamond Operator,multi-catch,switch, try with Resource and so on: All of the new features of JDK7, please refer to the JDK7 documentation for details. Set up to use version 1.7, you need to modify your build file:
Android {    compilesdkversion    buildtoolsversion "19.0.0"    defaultconfig {        minsdkversion 7        Targetsdkversion    }    compileoptions {        sourcecompatibility javaversion.version_1_7        Targetcompatibility javaversion.version_1_7    }}

Note: You can set the value of Minsdkversion to a version earlier than 19, except that you only have to use new language features other than try with resources. If you want to use the try with resources feature, you'll need to set Minsdkversion to 19 as well.

You also need to make sure that Gradle uses a 1.7 or higher jdk (Android Gradle plugin also requires 0.6.1 or higher versions).
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.