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. It is easy to use classes 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 are a lot of the same tasks in the Android project, their names are generated based on build types and product flavor.


To solve the problem, Android objects have two properties:

* Applicationvariants (applies only to app plugin)

* Libraryvariants (only applicable to 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 through filters.

    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 descriptive narrative.
DirName String The subdirectory name of the variant must also be unique. There may also be more than one subdirectory, 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 Compile the Java source task.
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. Generally not empty but also agree to null values.
Mergedflavor Productflavor Merger of Android.defaultconfig and Variant.productflavors.
Signingconfig Signingconfig The Signingconfig object used by the variant.
Issigningready Boolean Assuming true indicates that the variant already has all the information that needs to be signed.
Testvariant Buildvariant Will test the variant of the testvariant.
Dex Dex Package the code into a dex task. Assuming the variant is a library, this value can be empty.
Packageapplication Packageapplication Package finally APK's task. Assuming the variant is a library, this value can be empty.
Zipalign Zipalign Zip compresses the APK task. Suppose the variant is a library or an 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. Generally not empty but also agree to null values.
Mergedflavor Productflavor Merger of Android.defaultconfig and Variant.productflavors.
Signingconfig Signingconfig The Signingconfig object used by the variant.
Issigningready Boolean Assuming true indicates that the variant already has all the information that needs to be signed.
Testedvariant Basevariant The basevariant of testvariant test
Dex Dex Package the code into a dex task. Assuming the variant is a library, this value can be empty.
Packageapplication Packageapplication Package finally APK's task. Assuming the variant is a library, this value can be empty.
Zipalign Zipalign Zip compresses the APK task. Suppose the variant is a library or an 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 Run the Android test task on the connected device upstream.
Providerandroidtest Defaulttask Use the Extensions API to run the Android test task.

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

* using "outputFile" directly in the variant object can change the output directory at last.

* Zipalign

* File Inputfile

* File OutputFile

* using "outputFile" directly in the variant object can change the output directory at last.


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, the 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 change these objects in the DSL language to the process of building the ornaments, rather than changing the options for input and task in depth.


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


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


For Gradle Task (Defaulttask,javacompile,copy,zip), refer to the Gradle documentation.


7.3 BuildType and Product Flavor Property Reference (BuildType and product Flavor properties)
Coming soon .... = =
For Gradle Task (Defaulttask,javacompile,copy,zip), refer to the Gradle documentation.

7.4 Using sourcecompatibility 1.7 (JDK) 1.7 version number of sourcecompatibility)
Using the Android KitKat (version 19 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. Settings using the 1.7 version number, you need to change 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 number prior to 19, just that you can only use new language features other than try with resources. Assuming you want to use the try with resources feature, you need to set Minsdkversion to 19 as well.

You also need to confirm that Gradle uses a 1.7 or higher version of the JDK (Android Gradle plugin also requires 0.6.1 or a higher version number).

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.