简体   繁体   English

Groovy/build.gradle: Get if Debug or Release

[英]Groovy/build.gradle: Get if Debug or Release

I'm looking for a simple way to find out if an Android App is being built for Debug or Release mode inside my library's build.gradle script (in a task).我正在寻找一种简单的方法来确定是否正在为我的库的build.gradle脚本(在任务中)中的调试或发布模式构建 Android 应用程序。

I have a task called prepareHermes that extracts a.aar file.我有一个名为prepareHermes的任务,它提取 a.aar 文件。 I currently always extract the debug variant, but in Release builds I want to use the release variant:我目前总是提取调试变体,但在发布版本中我想使用发布变体:

  task prepareHermes() {
    doLast {
      def IS_DEBUG = // TODO: here
      def aarName = IS_DEBUG ? "hermes-debug.aar" : "hermes-release.aar"
      def hermesAAR = file("$hermesPackagePath/android/${aarName}")

      if (!hermesAAR.exists()) {
        throw new GradleScriptException("The hermes-engine npm package is missing \"android/${aarName}\"", null)
      }

      def soFiles = zipTree(hermesAAR).matching({ it.include "**/*.so" })

      copy {
        from soFiles
        from "$reactNative/ReactAndroid/src/main/jni/first-party/hermes/Android.mk"
        into "$thirdPartyNdkDir/hermes"
      }
    }
  }

This task is later used in various places (see full code here ), so making two separate tasks ( prepareHermesDebug and prepareHermesRelease ) is a bit inconvenient since I then have a ton of duplicate code.这个任务后来在不同的地方使用(见完整代码在这里),所以制作两个单独的任务( prepareHermesDebugprepareHermesRelease )有点不方便,因为我有大量的重复代码。 (this task is a dependency for a few other tasks, as seen in the full code.) (此任务是其他一些任务的依赖项,如完整代码所示。)

One approach is to check if any of the executed tasks has expected build type in their name:一种方法是检查是否有任何已执行的任务在其名称中具有预期的构建类型:

gradle.startParameter.taskRequests.toString().contains('Debug')

You also can generate as many tasks as you like:您还可以根据需要生成任意数量的任务:

[ 'debug', 'release' ].each{ name ->

  task "prepareHermes-$name"{
    doLast {
      def aarName = "hermes-${name}.aar"
      def hermesAAR = file "$hermesPackagePath/android/$aarName"
      // the rest...
    }
  }

}

Then if you run gradlew tasks , you will see prepareHermes-debug and prepareHermes-release in the output然后如果你运行gradlew tasks ,你会在 output 中看到prepareHermes-debugprepareHermes-release

Use gradle.startParameter.taskRequests to determine current variant is NOT a nice idea.**使用gradle.startParameter.taskRequests来确定当前变体不是一个好主意。**

  1. Android Gradle Plugin (AGP) is built over Gradle Platform. Android Gradle 插件 (AGP) 构建在 Gradle 平台之上。
  2. Android Variant is a concept from Upper layer. Android Variant 是来自上层的概念。
  3. Gradle gradle.startParameter.taskRequests API is Under layer, and it's not Variant Aware. Gradle gradle.startParameter.taskRequests API是Under layer,它不是Variant Aware。

It does not support the scenario like:它不支持以下场景:

./gradlew clean assemble

The above command produces both Debug and Release artifacts which breaks the check script using taskRequests .上面的命令生成DebugRelease工件,它们使用taskRequests破坏检查脚本。

making two separate tasks (prepareHermesDebug and prepareHermesRelease) is a bit inconvenient...制作两个单独的任务(prepareHermesDebug 和 prepareHermesRelease)有点不方便......

Yes, we are going to take onVariants{} API to do similar thing.是的,我们将采用onVariants{} API 来做类似的事情。 Basically, it iterates all variant objects for you to get a chance to register Variant Aware task respectively:基本上,它会迭代所有变体对象,以便您有机会分别注册 Variant Aware 任务:

androidComponents {
    onVariants { variant ->
        project.tasks.register<AarUploadTask>("${variant.name}AarUpload") {
            aar.set(variant.artifacts.get(SingleArtifact.AAR))
        }
    }
}

abstract class AarUploadTask: DefaultTask() {

    @get:InputFile
    abstract val aar: RegularFileProperty

    @TaskAction
    fun taskAction() {
        println("Uploading ${aar.get().asFile.absolutePath} to fantasy server...")
    }
}

Through the new Variant&Artifact APIs above(In Kotlin DSL sorry, but you can easily transform it to groovy), you can get the corresponding .aar of the Variant and pass it to a custom task conveniently(no hardcode any more).通过上面新的 Variant&Artifact APIs(在 Kotlin DSL 抱歉,但你可以轻松地将其转换为 groovy),您可以获取 Variant 对应的.aar并方便地将其传递给自定义任务(不再使用硬编码)。

Check full scripts from android/gradle-receipt , and more API doc from Extend the Android Gradle plugin .检查来自android/gradle-receipt 的完整脚本,以及来自Extend the Android Gradle 插件的更多 API 文档。

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

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