Official Android Technical Documentation translation-Gradle Plug-In User Guide (7)
Advanced build custom build options Java compilation options
Android {compileOptions {sourceCompatibility = 1.6 targetCompatibility = 1.6 }}
The default value is 1.6 ". This configuration affects all Java source code compilation tasks.
Aapt options
Android {aaptOptions {noCompress 'foo', 'bar' ignoreAssetsPattern !. Svn :!. Git :!. Ds_store :! *. Scc :.*: _*:! CVS :! Thumbs. db :! Picasa. ini :! *~ }}
This configuration affects all tasks that use aapt. Dex options
Android {dexOptions {incremental false
PreDexLibraries = false
JumboMode = false
}}
This configuration affects all tasks that use dex.
Java projects with basic task operations have a limited set of tasks that can work together to create an output.
Where
ClassesThe task is the task that compiles the Java source code.
Slave
Build. gradleYou can easily access it by simply using classes in the script. This is
Project. tasks. classes.
In Android projects, it is more complex, because there may be a large number of identical tasks and their names are based on
Build TypesAnd
Product FlavorsGenerate.
AndroidThere are two objects. Is the original text incorrect ?) The attribute is used to solve this problem:
- ApplicationVariants(Only applicable to application plug-ins)
- LibraryVariants(Applicable only to library plug-ins)
- TestVariants(Applicable to both INS) The three will return the DomainObjectCollection objects of ApplicationVariant, LibraryVariant, and TestVariant respectively.
Note that accessing any of these sets triggers the generation of all tasks. This means that you should not re-configure these collections.
DomainObjectCollection can be used to directly access all objects or filter objects through a filter (which is convenient.
Android. applicationVariants. each {variant-> ....}
All three variant classes have the following attributes:
Attribute name |
Attribute type |
Description |
Name |
String |
The name of variant. It must be unique. |
Description |
String |
Human readable description of variant. |
DirName |
String |
The name of the variant subfolder. It must be unique. It may be multiple folders, namely debug/flavor1. |
BaseName |
String |
The basic output name of variant must be unique. |
OutputFile |
File |
Variant output. This is a readable and writable attribute. |
ProcessManifest |
ProcessManifest |
The task that processes manifest. |
AidlCompile |
AidlCompile |
Compile the AIDL file. |
RenderscriptCompile |
RenderscriptCompile |
Compile the Renderscript file. |
MergeResources |
MergeResources |
The task of merging resources. |
MergeAssets |
MergeAssets |
Merge assets tasks. |
ProcessResources |
ProcessAndroidResources |
A task that processes and compiles resources. |
GenerateBuildConfig |
GenerateBuildConfig |
Generate tasks of the BuildConfig class. |
JavaCompile |
JavaCompile |
Compile Java code. |
ProcessJavaResources |
Copy |
Processing Java resources. |
Assemble |
DefaultTask |
The assemble anchor task of variant. |
The ApplicationVariant class adds the following attributes:
Attribute name |
Attribute type |
Description |
BuildType |
BuildType |
BuildType of variant. |
ProductFlavors |
List |
ProductFlavors of variant. It is always not null, but can be a null set. |
MergedFlavor |
ProductFlavor |
Merge android. defaultConfig and variant. productFlavors |
SigningConfig |
SigningConfig |
SigningConfig object for variant |
IsSigningReady |
Boolean |
True if the variant has all the information required for signature. |
TestVariant |
BuildVariant |
The TestVariant of the variant will be tested. |
Dex |
Dex |
Generate dex tasks by code. If variant is a database, the value can be null. |
PackageApplication |
PackageApplication |
Package the final APK task. If variant is a database, the value can be null. |
ZipAlign |
ZipAlign |
Zipaligns (optimized aligns) Tasks for apk. If variant is a library or APK cannot be signed, the value can be null. |
Install |
DefaultTask |
Installation task. It can be null. |
Uninstall |
DefaultTask |
Detach a task. |
The LibraryVariant class adds the following attributes:
Attribute name |
Attribute type |
Description |
BuildType |
BuildType |
BuildType of variant. |
MergedFlavor |
ProductFlavor |
Defaconfig config Value |
TestVariant |
BuildVariant |
The Build variant of this Variant will be tested |
PackageLibrary |
Zip |
The task of packaging the file into a Library AAR archive file. If it is not a library project, it can be null. |
The following attributes are added to the TestVariant class:
Attribute name |
Attribute type |
Description |
BuildType |
BuildType |
BuildType of variant. |
ProductFlavors |
List |
ProductFlavors of variant. It is always not null, but can be a null set. |
MergedFlavor |
ProductFlavor |
Merge android. defaultConfig and variant. productFlavors |
SigningConfig |
SigningConfig |
SigningConfig object for variant |
IsSigningReady |
Boolean |
True if the variant has all the information required for signature. |
TestedVariant |
BaseVariant |
BaseVariant tested by TestVariant. |
Dex |
Dex |
Generate dex tasks by code. If variant is a database, the value can be null. |
PackageApplication |
PackageApplication |
Package the final APK task. If variant is a database, the value can be null. |
ZipAlign |
ZipAlign |
Zipaligns (optimized aligns) Tasks for apk. If variant is a library or APK cannot be signed, the value can be null. |
Install |
DefaultTask |
Installation task. It can be null. |
Uninstall |
DefaultTask |
Detach a task. |
ConnectedAndroidTest |
DefaultTask |
Run the android test task on a connected device. |
ProviderAndroidTest |
DefaultTask |
Use the extended API to run the android test task. |
Android APIs for specific task types.
- 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
- To modify the final output file, you can directly use "outputFile" on the variant object. (Note: outputs obtained through variant in version 1.0 is a List, you need to traverse it or retrieve the first element variant. outputs [0]. outputFile to obtain the outputFile object ).
- ZipAlign
- File inputFile
- File outputFile
- To modify the final output file, you can directly use "outputFile" on the variant object. (Note: outputs obtained through variant in version 1.0 is a List, you need to traverse it or retrieve the first element variant. outputs [0]. outputFile to obtain the outputFile object ).
Due to the working principle of Gradle and the configuration method of Android plug-ins, APIs for each task type are limited. First, Gradle wants the task to configure only the positions of the input or output and the optional flag that may be used. So here, these tasks can only define some inputs or outputs.
Secondly, the vast majority of input of these tasks is practical, and they often come fromSourceSets,Build TypesAndPruduct Flavor. To keep the build files easy to read and understand, developers can modify the build by slightly adjusting the DSL, rather than going deep into the task options and inputs and modifying them.
Note that, except for ZipAlign task types, private data must be set up for all other types to make them run properly. This means that it is impossible to manually create new tasks of these types.
This API may also be changed. Generally, the current API adds additional processing (if needed) around the output or input (if possible) entry given by the task ). Feedback is welcome, especially for unpredictable demands.
For details about Gradle tasks (DefaultTask, JavaCompile, Copy, Zip), see Gradle documentation.
BuildType and Product Flavor attributes will be available soon. For details about Gradle tasks (DefaultTask, JavaCompile, Copy, Zip), see Gradle documentation.
Using sourceCompatibility 1.7 through Android KitKat (buildToolsVersion 19), you can use the diamond operator (<>), multi-catch, use string, try with resources, and so on in the switch statement. To do this, add the following configuration to your build file:
android {
compileSdkVersion 19
buildToolsVersion 19.0.0
defaultConfig {
minSdkVersion 7
targetSdkVersion 19
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
}
Note: minSdkVersion
The value is set to version 19, but you can only use new language features other than try with resources. If you want to use try with resources, you need minSdkVersion
It is also set to 19.
You also need to confirm that Gradle uses JDK 1.7 or later. (Android Gradle plug-in also requires version 0.6.1 or later .)