Android Studio:Gradle Product Flavors:定义自定义属性 [英] Android Studio: Gradle Product Flavors: Define custom properties

查看:37
本文介绍了Android Studio:Gradle Product Flavors:定义自定义属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Gradle (Android Studio) 中构建不同产品风格的 Android 应用.

I am building different product flavors of an Android App in Gradle (Android Studio).

因此我定义了以下产品口味:

Hence I defined the following product flavors:

android {

    project.ext.set("customer", "")
    project.ext.set("server", "")

    //Configuration happens here - code removed for readability

    buildTypes {

        debug {
            server = "test"
        }

        release {
            server = "release"
        }
    }

    //Available product flavors
    productFlavors {
        customerA{
            customer = "a"
        }
        customerB{
            customer = "b"
        }
        customerC{
            customer = "c"
        }
    }
}

但是,稍后,当我在我的构建任务之一中访问定义的项目属性customer"(其值在我当前构建的产品风格中设置)时,它始终具有值c",即使 iam建筑 customerA(在这种情况下,物业客户应该是a"而不是c").例如我稍后执行以下任务:

However, later on, when I access the defined project property "customer" (whose value is set in the product flavor i am currently building) in one of my build tasks, it always has the value "c" even though iam building customerA (in which case the property customer should be "a" rather than "c"). For instance I execute the following task later on:

preBuild << {
    println "Building customer: " + customer
}

它总是打印:

建筑客户:c

所以我猜有一些覆盖正在发生?可能和配置VS执行阶段有关?不确定如何/为什么,因此非常感谢任何帮助.

So i am guessing there is some overwriting happening? Possibly related to the configuration VS execution phase? Not sure how/why though, so any help is be greatly appreciated.

更新:或者,它已经可以让我进一步确定产品风味的名称(没有附加构建类型名称)和构建类型(再次:没有预先添加产品风味名称到它)在 gradle 构建的执行阶段.

UPDATE: Alternatively it would already get me further to determine the name of the product flavor (without the build type name attached to it) and the build type (again: without the product flavor name prepended to it) during execution phase of the gradle build.

考虑到上述配置,预期的产品风味名称将是:customerA、customerB 和 customerC.

Considering the above configuration the expected product flavor names would be: customerA, customerB and customerC.

推荐答案

在评估阶段,Gradle 执行 android 块中的所有代码;它不只是执行与您要编译的风格相关的代码.事实上,在评估阶段,它甚至不知道你的口味是什么;它必须对此进行评估才能找出答案.

During evaluation phase, Gradle executes all of the code in your android block; it doesn't just execute the code relevant to the flavors you want to compile. In fact, during evaluation phase, it doesn't even really know what your flavors are; it has to evaluate that to find out.

所以你的所有三行 customer = "a"customer = "b"customer = "c" 都会得到执行.

So all three of your lines customer = "a", customer = "b", and customer = "c" will get executed.

这是 Gradle 的一个微妙之处,使它有点难以学习.

This is one of the subtle things about Gradle that make it a little difficult to learn.

所以我已经解释了为什么你的代码没有按照你期望的方式工作,但是这个答案是不完整的,因为我没有说很多关于如何使它正常工作的内容,但很难说该怎么做这样做是因为我不确定你想要完成什么.总的来说,我可以说你应该考虑尝试使用用户定义的任务来完成你想要的,并设置任务内的依赖关系以确保事情以正确的顺序执行.Android Gradle 构建的一个问题是,即使这些任务直到评估阶段才被定义(在评估构建文件并知道这些风格是什么之前,它无法知道构建所有风格所需的任务),所以这样做一些 SO 调查,看看如何将事情挂接到 Android Gradle 构建任务上——你必须在 Android 插件完成它的事情后,在评估阶段结束时设置你的任务.

So I've explained why your code isn't working the way you expect, but this answer is incomplete because I haven't said a lot about what to do to make it work right, but it's hard to say what to do because I'm not sure what you're trying to accomplish. In general I can say that you should think of trying to accomplish what you want using user-defined tasks, and setting up intra-task dependencies to make sure things get executed in the right order. A gotcha with Android Gradle builds is that even those tasks don't get defined until evaluation phase (it can't know what tasks it needs to build all your flavors until it's evaluated the build file and knows what those flavors are), so do some SO sleuthing to see how to hook things onto Android Gradle build tasks -- you have to set up your tasks at the end of evaluation phase after the Android plugin has done its thing.

这篇关于Android Studio:Gradle Product Flavors:定义自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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