PageSourceSearch

https://www.yolo.blue/assets/js/266128b0.5c451994.js

js yolo.blue collected 2026-09-24 10:42:22 UTC 14,593 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkyolo=self.webpackChunkyolo||[]).push([["8773"],{96602(e,n,a){a.r(n),a.d(n,{metadata:()=>i,default:()=>u,frontMatter:()=>o,contentTitle:()=>t,toc:()=>d,assets:()=>s});var i=JSON.parse('{"id":"notes/develop/android/gradle-task-lazy-configuration","title":"Gradle Task Lazy Configuration","description":"Gradle build \u8017\u65F6\u957F\u662F\u4E2A\u8001\u95EE\u9898\u4E86\uFF0C\u9879\u76EE\u8D8A\u5927\u95EE\u9898\u8D8A\u4E25\u91CD\uFF0CAndroid \u5F00\u53D1\u56E2\u961F\u4E5F\u4E00\u76F4\u5728\u60F3\u529E\u6CD5\u4F18\u5316\u3002\u672C\u6587\u4ECB\u7ECD\u7684 Task Lazy Configuration \u4E5F\u662F\u6700\u65B0\u7684\u4F18\u5316\u624B\u6BB5\u4E4B\u4E00\u3002","source":"@site/docs/notes/develop/android/gradle-task-lazy-configuration.md","sourceDirName":"notes/develop/android","slug":"/notes/develop/android/gradle-task-lazy-configuration","permalink":"/docs/notes/develop/android/gradle-task-lazy-configuration","draft":false,"unlisted":false,"tags":[{"inline":false,"label":"Java","permalink":"/docs/tags/java"},{"inline":false,"label":"Android","permalink":"/docs/tags/android"}],"version":"current","lastUpdatedAt":1730018633000,"frontMatter":{"tags":["java","android"]},"sidebar":"docsSidebar","previous":{"title":"Android Layout Inflation \u4F18\u5316","permalink":"/docs/notes/develop/android/android-layout-inflation-optimization"},"next":{"title":"\u6DF1\u8272\u6A21\u5F0F\u7684\u53D1\u5C55\u5386\u7A0B","permalink":"/docs/notes/develop/dark-mode-evolution"}}'),l=a(74848),r=a(28453);let o={tags:["java","android"]},t="Gradle Task Lazy Configuration",s={},d=[{value:"\u5F15\u5B50",id:"\u5F15\u5B50",level:2},{value:"\u8B66\u544A\u6765\u6E90",id:"\u8B66\u544A\u6765\u6E90",level:2},{value:"Task Configuration Avoidance API",id:"task-configuration-avoidance-api",level:2},{value:"Gradle Build Lifecycle",id:"gradle-build-lifecycle",level:3},{value:"\u793A\u4F8B",id:"\u793A\u4F8B",level:3},{value:"\u610F\u4E49",id:"\u610F\u4E49",level:2},{value:"\u7ED3\u8BBA",id:"\u7ED3\u8BBA",level:2}];function c(e){let n={a:"a",blockquote:"blockquote",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",li:"li",ol:"ol",p:"p",pre:"pre",ul:"ul",...(0,r.R)(),...e.components};return(0,l.jsxs)(l.Fragment,{children:[(0,l.jsx)(n.header,{children:(0,l.jsx)(n.h1,{id:"gradle-task-lazy-configuration",children:"Gradle Task Lazy Configuration"})}),"\n",(0,l.jsx)(n.p,{children:"Gradle build \u8017\u65F6\u957F\u662F\u4E2A\u8001\u95EE\u9898\u4E86\uFF0C\u9879\u76EE\u8D8A\u5927\u95EE\u9898\u8D8A\u4E25\u91CD\uFF0CAndroid \u5F00\u53D1\u56E2\u961F\u4E5F\u4E00\u76F4\u5728\u60F3\u529E\u6CD5\u4F18\u5316\u3002\u672C\u6587\u4ECB\u7ECD\u7684 Task Lazy Configuration \u4E5F\u662F\u6700\u65B0\u7684\u4F18\u5316\u624B\u6BB5\u4E4B\u4E00\u3002"}),"\n",(0,l.jsx)(n.h2,{id:"\u5F15\u5B50",children:"\u5F15\u5B50"}),"\n",(0,l.jsx)(n.p,{children:"\u5728 Gradle \u811A\u672C\u4E2D\u5982\u4E0B\u904D\u5386 build variant \u7684\u4F9D\u8D56 classpath\uFF1A"}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-groovy",children:'android.libraryVariants.each { variant ->\n    if (variant.name.endsWith("release")) {\n        androidJavadoc {\n            classpath += variant.javaCompile.classpath\n        }\n    }\n}\n'})}),"\n",(0,l.jsxs)(n.p,{children:["\u8FD9\u91CC\u7684",(0,l.jsx)(n.code,{children:"variant.javaCompile"}),"\u8C03\u7528\u4F1A\u5F15\u53D1\u5982\u4E0B\u7684\u8B66\u544A\uFF1A"]}),"\n",(0,l.jsxs)(n.blockquote,{children:["\n",(0,l.jsxs)(n.p,{children:["INFO: API 'variant.getJavaCompile()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'.\nIt will be removed at the end of 2019.\nFor more information, see ",(0,l.jsx)(n.a,{href:"https://d.android.com/r/tools/task-configuration-avoidance",children:"https://d.android.com/r/tools/task-configuration-avoidance"}),".\nTo determine what is calling variant.getJavaCompile(), use -Pandroid.debug.obsoleteApi=true on the command line to display more information.\nAffected Modules: library"]}),"\n"]}),"\n",(0,l.jsx)(n.p,{children:"\u8FD9\u4E2A\u8B66\u544A\uFF0C\u5927\u5BB6\u5728\u5199 Gradle \u811A\u672C\u65F6\u53EF\u80FD\u90FD\u89C1\u8FC7\u3002\u73B0\u5728\u5DF2\u7ECF2020\u5E74\u4E86\uFF0C\u4E5F\u5C31\u662F\u8BF4 Gradle \u5B98\u65B9\u5DF2\u7ECF\u53EA\u652F\u6301\u65B0\u7684\u65B9\u5F0F\u4E86\uFF0C\u90A3\u4E48\uFF0C\u8FD9\u5230\u5E95\u610F\u5473\u7740\u4EC0\u4E48\uFF1F\u6211\u4EEC\u53C8\u9700\u8981\u505A\u4EC0\u4E48\u5462\uFF1F"}),"\n",(0,l.jsx)(n.h2,{id:"\u8B66\u544A\u6765\u6E90",children:"\u8B66\u544A\u6765\u6E90"}),"\n",(0,l.jsxs)(n.p,{children:["Android \u4E2D\u5F15\u5165\u8FD9\u4E2A\u6539\u52A8\u7684\u662F ",(0,l.jsx)(n.a,{href:"https://developer.android.com/studio/releases/gradle-plugin#3-3-0",children:"Android Gradle plugin 3.3.0 (2019\u5E741\u6708)"}),"\uFF0C\u6B64\u7248\u672C\u63D2\u4EF6\u5BF9\u5E94\u7684 Gradle \u7248\u672C\u5347\u7EA7\u5230\u4E86",(0,l.jsx)(n.code,{children:"4.10.1+"}),"\u3002"]}),"\n",(0,l.jsx)(n.p,{children:"\u6B64\u7248\u672C\u7684 Behavior changes \u4E2D\u6709\u5982\u4E0B\u4E00\u6761\uFF1A"}),"\n",(0,l.jsxs)(n.blockquote,{children:["\n",(0,l.jsx)(n.p,{children:"Lazy task configuration: The plugin now uses Gradle\u2019s new task creation API to avoid initializing and configuring tasks that are not required to complete the current build (or tasks not on the execution task graph). For example, if you have multiple build variants, such as \u201Crelease\u201D and \u201Cdebug\u201D build variants, and you're building the \u201Cdebug\u201D version of your app, the plugin avoids initializing and configuring tasks for the \u201Crelease\u201D version of your app."}),"\n",(0,l.jsx)(n.p,{children:"Calling certain older methods in the Variants API, such as variant.getJavaCompile(), might still force task configuration. To make sure that your build is optimized for lazy task configuration, invoke new methods that instead return a TaskProvider object, such as variant.getJavaCompileProvider()."}),"\n",(0,l.jsx)(n.p,{children:"If you execute custom build tasks, learn how to adapt to Gradle\u2019s new task-creation API."}),"\n"]}),"\n",(0,l.jsxs)(n.p,{children:["\u7531\u6B64\u6211\u4EEC\u4E86\u89E3\u5230\u5728",(0,l.jsx)(n.code,{children:"Gradle 4.9"}),"\u7248\u672C\u4E2D\uFF0C\u5F15\u5165\u4E86\u65B0\u7684 ",(0,l.jsx)(n.a,{href:"https://docs.gradle.org/current/userguide/task_configuration_avoidance.html",children:"Task Configuration Avoidance API"}),"\u3002"]}),"\n",(0,l.jsx)(n.h2,{id:"task-configuration-avoidance-api",children:"Task Configuration Avoidance API"}),"\n",(0,l.jsxs)(n.blockquote,{children:["\n",(0,l.jsx)(n.p,{children:"In a nutshell, the API allows builds to avoid the cost of creating and configuring tasks during Gradle\u2019s configuration phase when those tasks will never be executed. For example, when running a compile task, other unrelated tasks, like code quality, testing and publishing tasks, will not be executed, so any time spent creating and configuring those tasks is unnecessary. The configuration avoidance API avoids configuring tasks if they will not be needed during the course of a build, which can have a significant impact on total configuration time."}),"\n"]}),"\n",(0,l.jsx)(n.p,{children:"\u7B80\u5355\u6765\u8BF4\uFF0C\u5C31\u662F\u53EA configure \u9700\u8981 execute \u7684\uFF08\u53CA\u5176\u4F9D\u8D56\u7684\uFF09Task\uFF0C\u51CF\u5C11 build \u8FC7\u7A0B\u4E2D configuration \u65F6\u95F4\u3002"}),"\n",(0,l.jsx)(n.p,{children:"\u5173\u4E8E configuration \u548C execution\uFF0C\u90FD\u662F\u5C5E\u4E8E Gradle Build Lifecycle\uFF0C\u6211\u4EEC\u7B80\u5355\u6765\u4E86\u89E3\u4E00\u4E0B\u3002"}),"\n",(0,l.jsx)(n.h3,{id:"gradle-build-lifecycle",children:"Gradle Build Lifecycle"}),"\n",(0,l.jsxs)(n.blockquote,{children:["\n",(0,l.jsx)(n.p,{children:"A Gradle build has three distinct phases."}),"\n",(0,l.jsx)(n.p,{children:"Initialization\nGradle supports single and multi-project builds. During the initialization phase, Gradle determines which projects are going to take part in the build, and creates a Project instance for each of these projects."}),"\n",(0,l.jsx)(n.p,{children:"Configuration\nDuring this phase the project objects are configured. The build scripts of all projects which are part of the build are executed."}),"\n",(0,l.jsx)(n.p,{children:"Execution\nGradle determines the subset of the tasks, created and configured during the configuration phase, to be executed. The subset is determined by the task name arguments passed to the gradle command and the current directory. Gradle then executes each of the selected tasks."}),"\n"]}),"\n",(0,l.jsx)(n.h3,{id:"\u793A\u4F8B",children:"\u793A\u4F8B"}),"\n",(0,l.jsx)(n.p,{children:"\u8BA9\u6211\u4EEC\u6765\u770B\u5177\u4F53\u7684\u4F8B\u5B50\uFF1A"}),"\n",(0,l.jsx)(n.p,{children:"\u9996\u5148\uFF0CAndroid \u9ED8\u8BA4\u67092\u4E2A build type: debug/release\uFF0C\u6211\u4EEC\u518D\u58F0\u660E2\u4E2A product flavor:"}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-groovy",children:'flavorDimensions "version"\nproductFlavors {\n    demo {\n        dimension "version"\n    }\n    full {\n        dimension "version"\n    }\n}\n'})}),"\n",(0,l.jsx)(n.p,{children:"\u8FD9\u6837\u6700\u7EC8\u4F1A\u4EA7\u751F 2 * 2 = 4 \u4E2A build variant \u5982\u4E0B\uFF1A"}),"\n",(0,l.jsxs)(n.ul,{children:["\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"demoDebug"})}),"\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"demoRelease"})}),"\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"fullDebug"})}),"\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"fullRelease"})}),"\n"]}),"\n",(0,l.jsxs)(n.p,{children:["\u7136\u540E\u6211\u4EEC\u518D\u4EFF\u7167",(0,l.jsx)(n.code,{children:"Android Gradle plugin"}),"\u7684\u505A\u6CD5\uFF0C\u4E3A\u6BCF\u4E2A build variant \u751F\u6210\u4E00\u4E2A\u540D\u4E3A",(0,l.jsx)(n.code,{children:"play"}),"\u7684 Task\uFF1A"]}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-groovy",children:'android.applicationVariants.all { variant ->\n    // \u65E7API - \u975Elazy configuration\n    task("play${variant.name.capitalize()}") {\n        println "$name start"\n        sleep(2000) // \u4F11\u772
102\u79D2\uFF0C\u6A21\u62DF\u8017\u65F6\u64CD\u4F5C\n        println "$name stop"\n    }\n}\n'})}),"\n",(0,l.jsx)(n.p,{children:"\u8FD9\u6837\u6211\u4EEC\u5C31\u521B\u5EFA\u4E86\u5982\u4E0B4\u4E2A Task\uFF1A"}),"\n",(0,l.jsxs)(n.ul,{children:["\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"playDemoDebug"})}),"\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"playDemoRelease"})}),"\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"playFullDebug"})}),"\n",(0,l.jsx)(n.li,{children:(0,l.jsx)(n.code,{children:"playFullRelease"})}),"\n"]}),"\n",(0,l.jsx)(n.p,{children:'\u6CE8\u610F\uFF0C\u521B\u5EFA Task \u65F6\u6211\u4EEC\u4F7F\u7528\u7684\u662F\u65E7\u7684 API\uFF0C\u4E5F\u5C31\u662F\u975E"lazy"\u7684\uFF0C\u6211\u4EEC execute \u5176\u4E2D\u7684\u4EFB\u610F1\u4E2A\u65F6\uFF0C\u6240\u67094\u4E2A\u90FD\u4F1A\u88AB configure\u3002\n\u8FD0\u884C\u9A8C\u8BC1\u4E00\u4E0B\uFF1A'}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{children:"./gradlew playDemoDebug\n\n> Configure project :app\nplayDemoDebug start\nplayDemoDebug stop\nplayDemoRelease start\nplayDemoRelease stop\nplayFullDebug start\nplayFullDebug stop\nplayFullRelease start\nplayFullRelease stop\n\nBUILD SUCCESSFUL in 8s\n"})}),"\n",(0,l.jsx)(n.p,{children:"\u53EF\u4EE5\u770B\u5230\uFF0C\u5728 Configuration \u9636\u6BB5\uFF0C4\u4E2A Task \u7684 configure \u903B\u8F91\u90FD\u88AB\u6267\u884C\u4E86\u3002\n\u4E3A\u4E86\u8BA9\u7ED3\u679C\u66F4\u660E\u663E\uFF0C\u6211\u4EEC\u4E3A configure \u8BBE\u7F6E\u4E862\u79D2\u7684\u5EF6\u65F6\uFF0C\u800C\u5176\u4ED6\u73AF\u8282\u6CA1\u6709\u6267\u884C\u4EFB\u4F55\u64CD\u4F5C\uFF0C\u8017\u65F6\u51E0\u4E4E\u4E3A0\u3002\nbuild \u7684\u603B\u8017\u65F68\u79D2\uFF0C\u7EA6\u7B49\u4E8E\u5355\u6B21 Task configuration \u7684\u8017\u65F6(2\u79D2) * Task \u6570\u91CF(4)\uFF0C\u7B26\u5408\u9884\u671F\u3002"}),"\n",(0,l.jsx)(n.p,{children:"\u7136\u540E\u6211\u4EEC\u6539\u7528\u65B0\u7684 API \u6765\u521B\u5EFA Task\uFF1A"}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{className:"language-groovy",children:'android.applicationVariants.all { variant ->\n    // \u65B0API - lazy configuration\n    tasks.register("play${variant.name.capitalize()}") {\n        println "$name start"\n        sleep(2000)\n        println "$name stop"\n    }\n}\n'})}),"\n",(0,l.jsx)(n.p,{children:'\u6B64\u65F6 Task \u7684 configuration \u5E94\u8BE5\u662F"lazy"\u7684\uFF0C\u4E5F\u5C31\u662F\u8BF4\u53EA\u6709\u88AB execute \u7684 Task \u624D\u4F1A\u88AB configure\u3002\n\u518D\u6B21\u8FD0\u884C\u9A8C\u8BC1\u4E00\u4E0B\uFF1A'}),"\n",(0,l.jsx)(n.pre,{children:(0,l.jsx)(n.code,{children:"./gradlew playDemoDebug\nplayDemoDebug start\nplayDemoDebug stop\n\nBUILD SUCCESSFUL in 2s\n"})}),"\n",(0,l.jsxs)(n.p,{children:["\u53EF\u4EE5\u770B\u5230\uFF0C\u786E\u5B9E\u53EA configure \u4E86\u88AB execute \u7684 ",(0,l.jsx)(n.code,{children:"playDemoDebug"})," Task\uFF0Cbuild \u7684\u603B\u8017\u65F6\u4E5F\u76F8\u5E94\u4E0B\u964D\u5230\u4E86\u7EA6\u4E3A\u5355\u6B21 Task configuration \u8017\u65F6\u76842\u79D2\uFF0C\u7B26\u5408\u9884\u671F\u3002"]}),"\n",(0,l.jsx)(n.h2,{id:"\u610F\u4E49",children:"\u610F\u4E49"}),"\n",(0,l.jsx)(n.p,{children:"\u53EF\u4EE5\u660E\u663E\u770B\u5230\uFF0C\u5728 build variant \u8F83\u591A\u4E14 configuration \u8017\u65F6\u8F83\u957F\u7684\u60C5\u51B5\u4E0B\uFF0C\u8FD9\u4E2A\u4F18\u5316\u4F1A\u6781\u5927\u7684\u51CF\u5C11 build \u5355\u4E2A variant \u6240\u9700\u8981\u7684\u65F6\u95F4\u3002"}),"\n",(0,l.jsx)(n.h2,{id:"\u7ED3\u8BBA",children:"\u7ED3\u8BBA"}),"\n",(0,l.jsxs)(n.ol,{children:["\n",(0,l.jsx)(n.li,{children:"\u4FDD\u6301 AGP/Gradle \u7684\u66F4\u65B0\uFF0C\u5BF9\u5F00\u53D1\u6548\u7387\u63D0\u5347\u5DE8\u5927\u3002"}),"\n",(0,l.jsx)(n.li,{children:"\u5982\u679C\u81EA\u5DF1\u7F16\u5199 Gradle \u811A\u672C\u6216\u63D2\u4EF6\u65F6\u521B\u5EFA\u4E86 task \uFF0C\u5E94\u8BE5\u5C3D\u5FEB\u8FC1\u79FB\u4F7F\u7528\u65B0\u7684API\u3002"}),"\n"]})]})}function u(e={}){let{wrapper:n}={...(0,r.R)(),...e.components};return n?(0,l.jsx)(n,{...e,children:(0,l.jsx)(c,{...e})}):c(e)}},28453(e,n,a){a.d(n,{R:()=>o,x:()=>t});var i=a(96540);let l={},r=i.createContext(l);function o(e){let n=i.useContext(r);return i.useMemo(function(){return"function"==typeof e?e(n):{...n,...e}},[n,e])}function t(e){let n;return n=e.disableParentContext?"function"==typeof e.components?e.components(l):e.components||l:o(e.components),i.createElement(r.Provider,{value:n},e.children)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.