android 变体_Android:从活动列表中删除特定的构建变体

android 变体

As projects start to grow and new layers of specifications and configuration are added we might start to use flavors and its dimensions in order to support a higher level of customization.

随着项目的增长和新的规格和配置层的增加,我们可能会开始使用口味及其尺寸,以支持更高级别的定制。

Nonetheless, every time we add a new flavor, the number of possibilities to compile the app automatically duplicates.

但是,每次添加新的样式时,编译应用程序的可能性都会自动重复。

香精 (Flavors)

noun: flavor1. the distinctive taste of a food or drink.2. an indication of the essential character of something.

名词: 风味 1。 食物或饮料的独特口味2。 指示某物的基本特征。

We can see flavors the same way: they are set of rules and behaviors that make your app behave differently depending on if you’re compiling it using the code written for flavorA or the one for flavorB.

我们可以用相同的方式来查看口味:它们是一组规则和行为,这些规则和行为使您的应用程序表现不同,这取决于您使用的是针对flavorA编写的代码还是针对flavorB编写的代码进行编译

A simple example can be the use case where an application is available on the Google Play Store with free and paid versions. If the user buys the paid version we want her to have access to all of its features which typically won’t be available on the free app. Moreover, we don’t want to ship that code on the free version. Otherwise, we increase the risk of the user finding a way to access those features without having paid for them. This can be done either by exploring vulnerabilities in the app, or it can even by a bug from the developer. So it’s better to play safe.

一个简单的例子就是用例,其中在Google Play商店中提供了具有免费和付费版本的应用程序。 如果用户购买了付费版本,我们希望她可以使用其所有功能,而这些功能通常不会在免费应用程序中提供。 而且,我们不想在免费版本上发布该代码。 否则,我们增加了用户找到无需付费就可以访问这些功能的风险。 这可以通过探索应用程序中的漏洞来完成,甚至可以通过开发人员的错误来完成。 所以最好还是安全玩。

When we start a new project we typically have a similar configuration:

当我们开始一个新项目时,我们通常具有类似的配置:

android {
buildTypes {
debug {
...
} release {
...
}
}
}

With this, we can either compile our app as debug or release.

这样,我们可以将我们的应用程序编译为debugrelease

Now, let's add a specific flavor:

现在,让我们添加一个特定的风味:

flavorDimensions "app"
productFlavors {
free {
dimension "app"
}
}

Looking at the build variants list, we can compile the same number of versions but they’re now called freeDebug and freeRelease.

查看构建变体列表,我们可以编译相同数量的版本,但是它们现在称为freeDebugfreeRelease

If we now add a second flavor we will duplicate the number of possibilities:

如果现在添加第二种口味,我们将重复多种可能性:

flavorDimensions "app"
productFlavors {
free {
dimension "app"
}
paid {
dimension "app"
}
}

We now have freeDebug, freeRelease, paidDebug and paidRelease.

现在,我们有了freeDebugfreeReleasepaidDebugpaidRelease

To the initial list of build types, we can add a third and fourth one increasing the number of possibilities here:

在构建类型的初始列表中,我们可以添加第三和第四种,以增加此处的可能性:

buildTypes {
debug {
...
}

dev {
...
} staging {
...
} release {
...
}

And with this, we start to have a never-ending list of possibilities: freeDebug, freeDev, freeRelease, freeStaging, paidDebug, paidDev, paidRelease and paidStaging.

至此,我们开始拥有无穷无尽的可能性列表: freeDebugfreeDevfreeReleasefreeStagingpaidDebugpaidDevpaidReleasepaidStaging

And the list can go on and on as we keep adding new functionalities.

随着我们不断添加新功能,该列表会不断增加。

忽视 (Ignore)

As we keep adding new flavors and build types Gradle will automatically add those new combinations to the list. Nonetheless, not all possibilities might make sense — for instance, we might not need a freeStaging version.

随着我们不断添加新的口味和构建类型,Gradle会自动将这些新组合添加到列表中。 但是,并非所有可能都有意义-例如,我们可能不需要freeStaging版本。

So, how can we remove it from the list? We just need, to ignore them.

那么,如何将其从列表中删除呢? 我们只需要ignore它们即可。

We can ignore an entire build type:

我们可以忽略整个构建类型:

variantFilter { variant ->
def names = variant.flavors*.name if (variant.buildType.name == "staging") {
setIgnore(true)
}
}

Or just a specific combination:

或只是特定的组合:

variantFilter { variant ->
def names = variant.flavors*.name if (variant.buildType.name == "staging") {
if (names.contains("free") {
setIgnore(true)
}
}
}

有关构建变体的更多信息 (More on build variants)

I’ve written another article on build variants, this time om how to define one as default on Android Studio:

我写了另一篇关于构建变体的文章,这次是关于如何在Android Studio中将其定义为默认变量的文章:

Do you have a better approach? Something didn’t quite work with you?

您有更好的方法吗? 您的工作不太正常吗?

Feel free to reply here or send me a message 🙂

随时在这里回复或给我发送消息🙂

翻译自: https://medium.com/swlh/android-remove-a-specific-build-variant-from-the-active-list-f817c28c90e8

android 变体

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值