简体   繁体   English

Android - 仅在发布版本变体上执行Gradle任务

[英]Android - Only execute Gradle task on release build variant

I am trying to configure my build.gradle file to only execute a gradle task when the release build variant is selected. 我正在尝试将build.gradle文件配置为仅在选择发布版本变量时执行gradle任务。 So far, my task always gets executed, whether it is in my debug or release build types or signing configs. 到目前为止,我的任务总是被执行,无论是在我的调试版本还是发布版本类型或签名配置中。 I have tried adding my task inside an applicationsVariants block and check if it is the release variant, but it just loops through all variants. 我已经尝试在applicationsVariants块中添加我的任务,并检查它是否是发布变体,但它只是循环遍历所有变体。

applicationVariants.all { variant ->
            variant.outputs.each { output ->
        ...
    }
}

I know that both the debug and release tasks always run for whichever build variant you choose. 我知道调试和发布任务总是针对您选择的任何构建变体运行。 Is it possible to execute some code only when creating a build for release? 是否可以仅在创建发布版本时执行某些代码? If so, where does that code go? 如果是这样,该代码在哪里? Thanks! 谢谢!

I have read through every Stackoverflow question on this, but none of the answers really did I am wanting. 我已经阅读了关于此问题的每个Stackoverflow问题,但没有一个答案确实是我想要的。 My end goal is when I select the "release" build variant for a Play Store build, a message is posted to our server. 我的最终目标是当我为Play商店版本选择“发布”版本变体时,会向我们的服务器发布一条消息。 I do not want this to happen when just debugging. 我不想在调试时发生这种情况。

Add doFirst or doLast for the build type you are interested in. 为您感兴趣的构建类型添加doFirstdoLast

android.applicationVariants.all {  variant ->
    if ( variant.buildType.name == "release"){
        variant.assemble.doLast { // Can also use doFirst here to run at the start.
            logger.lifecycle("we have successfully built $v.name and can post a messaage to remote server")
        }
    }
}

I had to do something like this to check build version: 我必须做这样的事情来检查构建版本:

buildTypes {
    applicationVariants.all { variant ->
        variant.outputs.each {output ->
            def project = "AppName"
            def separator = "_"
            /*def flavor = variant.productFlavors[0].name*/
            def buildType = variant.variantData.variantConfiguration.buildType.name
            def versionName = variant.versionName
            def versionCode = variant.versionCode
            def date = new Date();
            def formattedDate = date.format('yyyyMMdd_HHmm')
            if (variant.buildType.name == "release"){
                def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + separator + formattedDate + ".apk"
                output.outputFile = new File(output.outputFile.parent, newApkName)
            }
            if (variant.buildType.name == "debug"){
                def newApkName = project + separator + "v" + versionName + separator + versionCode + separator + buildType + ".apk"
                output.outputFile = new File(output.outputFile.parent, newApkName)
            }
        }
    } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM