2017-03-07 140 views
1

gradlew tasks給我這些任務(其中包括):爲什麼有些gradle任務無法訪問?

assemble - Assembles all variants of all applications and secondary packages. 
assembleAndroidTest - Assembles all the Test applications. 
assembleDebug - Assembles all Debug builds. 
assembleRelease - Assembles all Release builds. 
build - Assembles and tests this project. 
buildDependents - Assembles and tests this project and all projects that depend on it. 
buildNeeded - Assembles and tests this project and all projects it depends on. 
clean - Deletes the build directory. 
cleanBuildCache - Deletes the build cache directory. 
compileDebugAndroidTestSources 
compileDebugSources 
compileDebugUnitTestSources 
compileReleaseSources 
compileReleaseUnitTestSources 
mockableAndroidJar - Creates a version of android.jar that's suitable for unit tests. 

我可以添加額外的代碼來說,assemble,像這樣:

assemble { 
    doFirst { 
     println "hello" 
    } 
} 

,但不能與其他許多人從此做列表,例如,嘗試添加到assembleDebug給了我這個錯誤:

Error:(65, 0) Could not find method assembleDebug() for arguments [[email protected]] on project ':app' of type org.gradle.api.Project. 

這是爲什麼?

回答

1

這很容易解釋。當你運行gradle腳本時,它按照聲明的順序進行評估。但是 - 有時候,android項目就是一個很好的例子 - 你不會(也不能)知道可以創建什麼任務,因爲它們基於項目的內容(文件,變體等)。因此,這些任務正在創建,只要整個項目評估。這就是爲什麼你不能訪問他們在build.gradle - 因爲項目評估尚未完成。

現在,如果您知道將創建一個名爲assembleDebug的任務,則可以使用[afterEvaluate] 1在評估後配置任務。此時評估整個項目並添加/生成所有任務。

所以:

project.afterEvaluate { 
    assembleDebug { 
     doFirst { 
     println "hello" 
     } 
    } 
} 
相關問題