
[英]How do I get different icons for the two versions of my GPS app generated via gradle build variants?
[英]Understanding different variants of declaring a task in Gradle
在Gradle中声明任务有两种不同的变体。
1。
task myTask() {}
2。
task task3 << {}
为了测试它们的行为,我创建了一个示例android项目
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.android.gradletest"
minSdkVersion 15
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
}
task task1() {
println "Task 1"
}
task task2() {
println "Task 2"
}
task task3 << {
println "Task 3"
}
task task4() {
println "Task 4"
}
第一次尝试正在执行任务1
Task 1
Task 2
Task 4
:app:task1 UP-TO-DATE
BUILD SUCCESSFUL
Total time: 6.462 secs
Process finished with exit code 0
如您所见,不仅执行任务1,而且执行不执行任务3的完整构建。
第二次尝试是直接运行任务3:
Task 1
Task 2
Task 4
:app:task3
Task 3
BUILD SUCCESSFUL
Total time: 6.577 secs
Process finished with exit code 0
结果再次出现,似乎已触发了完整构建,但这一次是在任务3的末尾。
第三次尝试是简单地通过按Android Studio的运行按钮来构建完整的项目
Task 1
Task 2
Task 4
:app:preBuild UP-TO-DATE
:app:preDebugBuild UP-TO-DATE
:app:checkDebugManifest
:app:preReleaseBuild UP-TO-DATE
:app:prepareComAndroidSupportAppcompatV72211Library UP-TO-DATE
:app:prepareComAndroidSupportSupportV42211Library UP-TO-DATE
:app:prepareDebugDependencies
:app:compileDebugAidl UP-TO-DATE
:app:compileDebugRenderscript UP-TO-DATE
:app:generateDebugBuildConfig UP-TO-DATE
:app:generateDebugAssets UP-TO-DATE
:app:mergeDebugAssets UP-TO-DATE
:app:generateDebugResValues UP-TO-DATE
:app:generateDebugResources UP-TO-DATE
:app:mergeDebugResources UP-TO-DATE
:app:processDebugManifest UP-TO-DATE
:app:processDebugResources UP-TO-DATE
:app:generateDebugSources UP-TO-DATE
:app:processDebugJavaRes UP-TO-DATE
:app:compileDebugJava
Note: Recompile with -Xlint:deprecation for details.
:app:compileDebugNdk UP-TO-DATE
:app:compileDebugSources
:app:preDexDebug
:app:dexDebug
:app:validateDebugSigning
:app:packageDebug
:app:zipalignDebug
:app:assembleDebug
Information:BUILD SUCCESSFUL
如您所见,除了任务3,一切都被触发了!
现在我有两个问题:
Gradle构建过程分为三个阶段:
现在,任务定义包含三个阶段:
task myTask {
// Configuration scope
// will be executed during the configuration phase
doFirst {
// Will be executed during the execution phase before the task defined behavior
}
doLast {
// Will be executed during the execution phase after the task defined behavior
}
}
因此,当您定义这样的任务时:
task task1() {
println "Task 1"
}
由于println
语句是配置范围的一部分,因此它将在配置阶段执行,无论是否选择执行此任务。
现在,使用<<
操作符定义任务实际上是定义任务的doLast
闭包的快捷方式,即
task task3 << {
println "Task 3"
}
等效于:
task task3 {
doLast {
println "Task 3"
}
}
然后,根据我上面的解释,在这种情况下,仅在执行阶段才执行println
语句,以防必须像在配置阶段确定的那样执行task3
。
如果您有兴趣了解更多信息,强烈建议您阅读Gradle用户指南中的以下部分:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.