【问题标题】:How to get Current Build Type in Gradle如何在 Gradle 中获取当前构建类型
【发布时间】:2018-05-04 16:11:31
【问题描述】:

我的问题很直接,很容易理解。

问题

在 Gradle 中,有什么方法可以在运行时获取当前的构建类型。例如,在运行 assembleDebug 任务时,build.gradle 文件中的任务是否可以根据该任务与调试构建变体相关的事实做出决策?

示例代码

apply plugin: 'com.android.library'
ext.buildInProgress = "" 

buildscript {

repositories {
    maven {
        url = url_here
    }
}

dependencies {
    classpath 'com.android.tools.build:gradle:3.0.1'
}
}


configurations {
     //get current build in progress here e.g buildInProgress = this.getBuildType()
}

android {
     //Android build settings here
}

buildTypes {
         release {
          //release type details here
      }

       debug {
           //debug type details here
       }

    anotherBuildType{
          //another build type details here
    }

   }
}

dependencies {
      //dependency list here
}

repositories{
         maven(url=url2_here)
}


task myTask{
      if(buildInProgress=='release'){
           //do something this way
      }
      else if(buildInProgress=='debug'){
          //do something this way
      }
      else if(buildInProgress=='anotherBuildType'){
         //do it another way
     }
}

总结

有没有办法让我在 myTask{} 中准确获取正在进行的构建类型?

【问题讨论】:

标签: android android-studio gradle android-gradle-plugin build.gradle


【解决方案1】:

您可以通过解析您的applicationVariants 获得确切的构建类型:

applicationVariants.all { variant ->
    buildType = variant.buildType.name // sets the current build type
}

实现可能如下所示:

def buildType // Your variable

android {
    applicationVariants.all { variant ->
        buildType = variant.buildType.name // Sets the current build type
    }
}

task myTask{
    // Compare buildType here
}

您也可以查看thisthis 类似的答案。

更新

Thisthis的回答帮助提问者解决了问题。

【讨论】:

  • 我已经尝试过了,但它不起作用。当我在 myTask 中进行检查时,buildType 为 null
  • 另外,它是一个库项目而不是应用程序,所以我使用了“libraryVariants.all”而不是“applicationVariants.all”
  • 谢谢。这为我减轻了一些压力。在过去的几天里一直在尝试不同的方法来解决它,但没有成功,不知道这是 Gradle 的限制。
  • 我将在稍后相应地更新我的答案以匹配您的问题
  • 这个解决方案是一个魔术。您正在循环所有构建变体,多次分配buildType,最后是/曾经是正确的当前构建类型。
【解决方案2】:

这对我有用

applicationVariants.all { variant ->
    def variantType = variant.buildType.name
    println "Variant type: $variantType"
    if (variantType == "debug") {
       // do stuff
    }
}

【讨论】:

  • 你是在哪里执行的,在 app/build.gradle 中?
  • 是的。它需要在应用级别的 gradle 中。
  • 我运行这个,我得到一个错误:` 无法获取未知属性 'applicationVariants' `
  • 我不确定您的应用程序 gradle 是什么样的。我怀疑这可能是由于 gradle 版本较低,或者您可能正在为库执行此操作。对于库使用“libraryVariants”而不是“applicationVariants”。 Gradle 支持这样的扩展。更多信息在这里google.github.io/android-gradle-dsl/current/…
  • 是否可以在根级 build.gradle 中获取风味名称?
【解决方案3】:

你应该getBuildConfigFields().get("MY_BUILD_TYPE").getValue())

https://stackoverflow.com/a/59994937/5279996

GL

【讨论】:

    【解决方案4】:

    如果你想将 buildtype 名称作为版本名称的后缀(像我一样),只需将此行添加到版本名称:

    debug {
        versionNameSuffix "-debug"
    }
    

    这样您就可以在构建名称中识别构建类型。它无需声明任何其他内容即可工作。

    【讨论】:

      【解决方案5】:

      在 Android 平台的 Kotlin 编程语言中获取当前 buildType 的正确方法(Java 的逻辑相同)

      project.afterEvaluate {
         this.android().variants().all {
      
            this.assembleProvider.configure {
      
               this.doLast{
                  val variant = this@all
      
                  variant.outputs
                             .map
                             .forEach{
                                 //do something with current buildType, or build flavor or whatever
                                 println(variant.flavorName)
                                 println(variant.buildType)
                             }
               }
            }
         }
      }
      

      【讨论】:

        【解决方案6】:

        我检查了其他答案,没有任何效果。 下面的内容会有所帮助。 在你的 build.gradle (:app) 中:

        tasks.all { Task task ->
            if (task.name == "preDebugBuild") {
                doFirst {
                    //for debug build
                }
            } else if (task.name == "preReleaseBuild") {
                doFirst {
                    //for release build
                }
            }
        }
        
        dependencies {
            ...
        }
        

        请注意,您放入的代码不会在您更改构建变体时执行,而是在您构建应用程序时执行。

        【讨论】:

          【解决方案7】:

          我以这种方式获取构建类型:

          BuildConfig.BUILD_TYPE
          

          如果您需要检查当前构建类型是什么,请在您的 utils 包中创建一个枚举类并在您的 if 语句中使用它:

          enum class Environment(val value: String) {
             RELEASE("release"),
             LOCAL("local"),
             STAGING("staging"),
             DEBUG("debug")
          }
          

          你的 if/when 语句:

          if (BuildConfig.BUILD_TYPE == Environment.RELEASE.value) {
               //TODO
          } else if(...)
          

          或通过何时:

          when(BuildConfig.BUILD_TYPE) {
             Environment.RELEASE.value -> { //TODO }
             Environment.LOCAL.value -> { // TODO }
             // etc.
          }
          

          【讨论】:

            【解决方案8】:

            您可以通过解析您的 applicationVariants 获得确切的构建类型:

            applicationVariants.all { variant -> buildType = variant.buildType.name // sets the current build type }

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2015-07-09
              • 2012-07-24
              • 1970-01-01
              • 1970-01-01
              • 2013-02-10
              • 1970-01-01
              • 2022-01-26
              相关资源
              最近更新 更多