如何在build.gradle中尽早获得buildtype [英] How to get the buildtype in a build.gradle EARLY ON

查看:1294
本文介绍了如何在build.gradle中尽早获得buildtype的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个React Native应用程序,我正试图指定用于在project.ext.react中存储生成的源映射的文件夹,就像这样(我需要它来实现一个可帮助我调试JS错误的库)

I have a React Native application and I'm trying to specify the folder for storing the generated source map in project.ext.react like so (I need it to implement a library which will help me debug JS errors)

project.ext.react = [
        ...
        extraPackagerArgs    : [ "--sourcemap-output", "$buildDir/intermediates/assets/$buildType/index.android.bundle.map" ]
]

很明显,路径应该类似于<buildDir>/intermediates/assets/<buildType>/index.android.bundle.map

As evident, the path should be something like <buildDir>/intermediates/assets/<buildType>/index.android.bundle.map

(例如 如果是<buildDir>/intermediates/assets/release/index.android.bundle.map,则为<buildDir>/intermediates/assets/debug/index.android.bundle.map;如果是发布,则为<buildDir>/intermediates/assets/debug/index.android.bundle.map)

(e.g. <buildDir>/intermediates/assets/release/index.android.bundle.map in case of release and <buildDir>/intermediates/assets/debug/index.android.bundle.map in case of debug)

在参考了StackOverflow上和外部的几个答案之后,我在build.gradle中通过首先声明然后分配它来获取buildType:

After referring to several answers on StackOverflow and outside, I'm getting the buildType in the build.gradle by declaring it first and then assigning it:

//3rd line of build.gradle
def buildType
....
//much later
   applicationVariants.all { variant ->
    buildType = variant.buildType.name 
....

但是,这会导致一个问题,在使用buildType后会对其进行很多初始化,因此输出路径变成类似于<buildDir>/intermediates/assets/null/index.android.bundle.map的形式,从而使我的整个过程失败了.有没有办法早点获得构建类型?

However, this leads to a problem where buildType is initialized much after it is used, and so the output path becomes something like <buildDir>/intermediates/assets/null/index.android.bundle.map, thereby failing the whole process for me. Is there a way to get the build type earlier on?

推荐答案

我终于找到了一种方法(尽管不是很干净)来查看gradle任务数组:

I finally found a way (though its not a very clean one) to look through the array of gradle tasks:

def buildType = gradle.startParameter.taskNames.any{it.toLowerCase().contains("debug")}?"debug":"release"

这篇关于如何在build.gradle中尽早获得buildtype的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆