[Android Pro] Gradle Tip #3-task Order

Source: Internet
Author: User
<span id="Label3"></p><p><p>Reference to:http://blog.csdn.net/lzyzsd/article/details/46935405</p></p><p><p>Original link</p></p><p><p>I noticed that most of the problems I encountered when using gradle were related to the order in which the task was Executed. Obviously if my build will work better if my task is executed at the right Time. Let's take a closer look at how to change the order in which tasks are Executed.</p></p>DependsOn<p><p>I think the most straightforward way to illustrate the way your task executes depends on other tasks is to use the DependsOn method.<br>For example, in the following scenario, task a already exists, and we want to add a task B, which must be executed after a executes:<br><br>This is a very simple scenario, assuming that A and B are defined as Follows:</p></p><pre><pre>Task A << {println ' Hello from A '<span style="color: #000000;"></span><< {println ' Hello from B '}</pre></pre><p><p>Just a simple call to B.dependson a, you Can.<br>This means that as long as I execute task b,task a will execute First.</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle B:ahello from A:bhello to B</span></pre></pre><p><p>alternatively, you can declare its dependencies in the configuration area of the Task:</p></p><pre><pre>Task A << {println ' Hello from a '<span style="color: #000000;">}task b { dependsOn a dolast { </span>' Hello from B '<span style="color: #000000;"> }</span></pre></pre><p><p>What if we want to insert our task into a task dependency that already exists?</p></p><p><p><br>Process and just like that. Assume that the following task dependencies already exist:</p></p><pre><pre>Task A << {println ' Hello from A '<span style="color: #000000;"></span><< {println ' Hello from B '<span style="color: #000000;"></span><< {println ' Hello from C ' C8>}b.dependson Ac.dependson B</pre></pre><p><p>Join our new Task</p></p><pre><pre>Task B1 << {println ' Hello from B1 '<span style="color: #000000;">}b1.dependson bc.dependson B1</span></pre></pre><p><p>Output:</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle C:ahello from A:bhello to B:b1hello from B1:chello from C</span></pre></pre><p><p>Note that DependsOn adds a task to the dependent collection, so it is no problem to rely on multiple tasks.<br></p></p><pre><pre>Task B1 << {println ' Hello from B1 '<span style="color: #000000;">}b1.dependson bb1.dependson Q</span></pre></pre><p><p>Output:</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle B1:ahello from A:bhello to B:qhello from Q:b1hello from B1</span></pre></pre>Mustrunafter<p><p>Now suppose I have another task, which depends on the other two tasks. Here I use a real scene, I have two tasks, a unit test task, and a task for the UI Test. There is also a task that runs all of the tests, and it relies on the previous two tasks.<br></p></p><pre><pre>Task Unit << {println ' Hello from unit tests '<span style="color: #000000;"></span><< {println ' Hello from UI tests '<span style="color: #000000;"></span><< {prin Tln ' Hello from all tests! ' <span style="color: #000000;">}tests.dependson Unittests.dependson UI</span></pre></pre><p><p>Output:</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle Tests:uihello from the UI Tests:unithello from the unit Tests:testshello</span>from all tests!</pre></pre><p><p>Although Unitest and UI test CBD are executed before the test task, the execution order of the two tasks of the Unit and UI is not Guaranteed. Although it is now performed in alphabetical order, this is dependent on the implementation of gradle, which is absolutely not dependent on the order of your Code.<br>Because the UI test time is much longer than the unit test, I want the unit test to execute First. One solution is to make the UI task dependent on the unit Task.<br></p></p><pre><pre>Task Unit << {println ' Hello from unit tests '<span style="color: #000000;"></span><< {println ' Hello from UI tests '<span style="color: #000000;"></span><< {prin Tln ' Hello from all tests! ' <span style="color: #000000;"></span> <span style="color: #008000;">//</span> <span style="color: #008000;"> <--I added this dependency</span></pre></pre><p><p>Output:</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle Tests:unithello from the unit tests:uihello from the UI Tests:testshello</span>from all tests!</pre></pre><p><p>Unit test is now executed before the UI Test.<br>But here's a nasty question, and my UI test doesn't really depend on unit Test. I want to be able to execute the UI test separately, but every time I execute the UI test, I execute the unit test First.<br>We're going to use the Mustrunafter here. Mustrunafter does not add a dependency, it simply tells the gradle to perform the priority if two tasks exist at the same time. For example, We can specify the Ui.mustrunafter unit here so that if both the UI task and the unit task are present, Gradle executes the unit test first, and if the Gradle UI is executed only, the unit task is not Executed.</p></p><pre><pre>Task Unit << {println ' Hello from unit tests '<span style="color: #000000;"></span><< {println ' Hello from UI tests '<span style="color: #000000;"></span><< {prin Tln ' Hello from all tests! ' <span style="color: #000000;">}tests.dependson unittests.dependson uiui.mustrunafter Unit</span></pre></pre><p><p>Output:</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle Tests:unithello from the unit tests:uihello from the UI Tests:testshello</span>from all tests!</pre></pre><p><p>Dependency relationships such as:<br></p></p><pre><pre>Mustrunafter is still an experimental function in Gradle2.4.</pre></pre>Finalizedby<p><p>Now that we have two task,unit and ui, assuming that the two tasks will output the test report, now I want to combine the two test reports into one:<br></p></p><pre><pre>Task Unit << {println ' Hello from unit tests '<span style="color: #000000;"></span><< {println ' Hello from UI tests '<span style="color: #000000;"></span><< {prin Tln ' Hello from all tests! ' <span style="color: #000000;"></span> << {println ' merging test reports '<span style="color: #000000;">}tests.dependson Unittests.dependson uiui.mustrunafter Unitmergereports.dependson tests</span></pre></pre><p><p>Now if I want to get the UI and unit Test reports, execute task mergereports.</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle Mergereports:unithello from the unit tests:uihello from the UI Tests:testshello</span>from all tests! <span style="color: #000000;">: mergereportsmerging test Reports</span></pre></pre><p><p>This task is capable of working, but it looks so stupid. Mergereports feeling is not particularly good from a user's point of VIEW. I want to execute tests task to get the test report without knowing the existence of Mergereports. Of course I can move the merge logic into the tests task, but I don't want to make the tests task too bloated, I continue to put the merge logic in the Mergereports Task.<br>Finalizeby to Rescue. As the name implies, Finalizeby is the task to be executed after the task has finished Executing. Modify our script as Follows:</p></p><pre><pre>Task Unit << {println ' Hello from unit tests '<span style="color: #000000;"></span><< {println ' Hello from UI tests '<span style="color: #000000;"></span><< {prin Tln ' Hello from all tests! ' <span style="color: #000000;"></span> << {println ' merging test reports '<span style="color: #000000;">}tests.dependson Unittests.dependson uiui.mustrunafter Unitmergereports.dependson Teststests.finalizedby mergereports</span></pre></pre><p><p>Now execute the tests task to get the test report:</p></p><pre><pre><span style="color: #000000;">paveldudka$ Gradle Tests:unithello from the unit tests:uihello from the UI Tests:testshello</span>from all tests! <span style="color: #000000;">: mergereportsmerging test Reports</span></pre></pre><pre><pre>Note that Finalizedby is also an experimental function of Gradle2.4</pre></pre><p><p></p></p><p><p>[Android Pro] Gradle Tip #3-task Order</p></p></span>
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.