Gradle Builds Everything——Task 实例

Author: A Wenxian Sen

Gradle above describes the basic concepts about the task, we began to talk about how the next Task is defined.

For convenience, we context inseparable Gradle and AndroidGradlePlugin, therefore here without departing from the Android environment to introduce Gradle. We tell task dependencies when a Manager of the things mentioned here, we talk about this class is BuildableArtifactsHolder AndroidGradlePlugin offer.

If there is a Task meaningful output, and the product may be used when other Task, we can go BuildableArtifactsHolder Sign up for our product, so there are several registration methods:

createBuildableArtifact/createDirectory/createArtifactFile

These need to pass in api to create a file type, file name, file actions, so as to form createArtifactFile one example:

fun createArtifactFile(
        artifactType:ArtifactType,
        operationType:OperationType,
        taskName:String
        fileName : String) : Provider<RegularFile>

artifactType is a product type

operationType is operation type, it can be initialized (the init), is added (the append), or a change (Transform), the following differences:

  1. If indicates that init, then it can not have any calls before for the same product type
  2. If indicates that append, it is added to the relevant file
  3. If indicates a transform, it is to replace the current file.

You can use different types according to the purpose of your use of the product, because the return value is a FileCollection (collection of files, the concept is not a folder), if the call is to transform, you need to pay attention to issues the order calls for these products in the Configuration phase, If B.transform after A.transform, then get the final product and they will take away the results of B.

BuildableArtifactsHolder provided api general have let you pass the taskName, it is to let you tell it, you produced the product which is generated by the task. According to follow-up when you go to get artifactType product (FileCollection), it will check whether this task, if not executed first, this part of the judgment logical done by the FileCollection of builtBy, managed by Gradle.

Task configuration

Above, we introduced the product and how to make Task Task itself have some relevance, then this association is established, you can see the return value of this function is createArtifactFile Provider <RegularFile>, it represents a destination where we go in this file the product can be in writing; of course, here you can create a folder Similarly, the sample code:

task.outputFile = variantScope.getArtifacts().createArtifactFile(
                   InternalArtifactType.BUNDLE,
                   BuildArtifactsHolder.OperationType.INITIAL,
                   taskName,
                   bundleName)

Then place the task definition:

class Task{
 private Provider<RegularFile> outputFile;
 @OutputFile
 public Flie getOutputFile() {
    return outputFile.get().asFile();
 }
}

In this case, gradle will automatically help you create this file, developers need only call getOutputFile () get File and then written to.

Task linkage

We know that the Task generate a file of the complete process, how do we use this document? In addition we have a Task, the definition of what input

task.input = variantScope.getArtifacts().getArtifactFiles(type); //这里的 type 和上面的type一样,比如 InternalArtifactType.BUNDLE

This is to get a BuildableArtifact object that statement is as follows:

interface BuildableArtifact : Iterable, Buildable, Supplier {
 val files : Set
 fun isEmpty() : Boolean
}

Can I use the get () Gets a FileCollection, when calling FileCollection.getFiles again () method, gradle checks produce this product Task has been executed, if not implemented, it will first perform in front of a Task

At last

Thank you for patience and ability, La miles long-winded reading my article.

Willing to work with you to communicate with each other in learning Android development positions fellow citizens, and common progress!

Here I also share a copy of your collection of finishing Android studying architecture PDF + Video + Interview + document source notes , as well as advanced technical architecture Advanced Brain Mapping, Android interview with thematic development, advanced materials advanced architecture to help you enhance Advanced Learning , but also saves everyone time online in search of information to learn, you can also share with close friends studying together

If you have a need, you can point Like + Comment

Gradle Builds Everything——Task 实例

Guess you like

Origin blog.51cto.com/14573572/2448569