简体   繁体   English

受构建类型和风味尺寸限制的ApplicationId

[英]ApplicationId conditioned by build type and flavour dimension

I have an application which has 2 flavour dimensions. 我有一个具有2种口味尺寸的应用程序。 First dimension, lets call it "brand" has two types: "strawberry", "cyan" 第一维,我们称之为“品牌”有两种类型:“草莓”,“青色”

Both have different applicationIds, but we can focus only on one of those. 两者都有不同的applicationIds,但是我们只能专注于其中之一。 Lets say "cyan" has applicationId "com.cyan.app". 假设“青色”的applicationId为“ com.cyan.app”。

The second flavour dimension is called say "environment". 第二个风味维度称为“环境”。 It has two values: "prod", "test". 它有两个值:“ prod”,“ test”。

I also have two build types: "debug", "release" 我也有两种构建类型:“调试”,“发布”

Now what I'm interested in, is how can I go about configuring the gradle script such that whenever I'm building debug versions there will be applicationIdSuffix which will contain both "debug" string and environment name string. 现在,我感兴趣的是如何配置gradle脚本,以便每当构建调试版本时,都会有applicationIdSuffix,其中将同时包含“调试”字符串和环境名称字符串。 So in the above example it would be: com.cyan.app.debug.prod and com.cyan.app.debug.test 因此,在上面的示例中它将是:com.cyan.app.debug.prod和com.cyan.app.debug.test

But if I build release version of an app I want to leave the main applicationId, so com.cyan.app, no matter the environment flavour. 但是,如果我构建应用程序的发行版,则无论环境如何,我都希望保留主要的applicationId(即com.cyan.app)。

Is there any way I can achieve that with gradle and android gradle plugin? 有什么办法可以用gradle和android gradle插件实现呢?

Ok, I've sit down during the weekend and was able to achieve that (and more). 好的,我在周末坐下了,就能做到(甚至更多)。 Sharing here so that others can benefit who would like to achieve the same thing as I posted in the question. 在这里分享,以便其他人可以受益于希望实现与我在问题中发布的内容相同的人。

So first of all we have to attach ourselves to the applicationVariants.all task like so: 因此,首先我们必须将自己附加到applicationVariants.all任务,如下所示:

applicationVariants.all { variant ->
  //We tweak the package name and application name depending on the variants (so that we can have multiple applications installed on the phone, depending on the 
  tweakPackageName(variant);
  replaceInManifest(variant, 'android:label=\"@string/app_name\"', 'android:label=\"@string/
}

I've separated the logic to other methods so that the code is not clogged. 我将逻辑与其他方法分开,以使代码不会被阻塞。 The tweakPackageName method looks pretty simple (as it turned out): tweakPackageName方法看起来非常简单(事实证明):

/**
 * Method that alters the package name in order to have many different variants of application installed simultanously on a device.
 */
ext.tweakPackageName = { variant ->
  def envFlavor = getFlavorOfDimension(variant, flavorDimEnvironmentName);
  variant.mergedFlavor.applicationId = variant.mergedFlavor.applicationId + ".${envFlavor.name.toLowerCase()}";
}

getFlavorOfDimension is a simple method to get the flavor of particular dimension I'm interested in (not a biggie so I won't spam with this code here). getFlavorOfDimension是一种获取我感兴趣的特定维度的风格的简单方法(不是大问题,因此在这里我不会用此代码发送垃圾邮件)。 When we get the flavor we add it to the package name of the mergedFlavor object and we're done. 当我们获得风味时,将其添加到mergedFlavor对象的包名称中,就可以完成了。

I also managed to not only change the package name but also to dynamically change the application launcher name, but there are plenty of solutions of that on StackOverflow so I won't be redundant here. 我不仅设法更改了程序包名称,而且还动态更改了应用程序启动器名称,但是在StackOverflow上有很多解决方案,因此在这里我不再赘述。 All in all the solution to alter the package name works. 总而言之,更改软件包名称的解决方案有效。 Hopefully it will help someone like it helped me. 希望它能帮助像我这样的人。

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

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