简体   繁体   English

Gradle:Android Studio BuildType 资源继承

[英]Gradle: Android Studio BuildType Resource Inheritance

All projects have a debug and release buildType by default.默认情况下,所有项目都有一个调试和发布 buildType。 However often people will want to inherit from these base build types to make other build types with small differences.然而,人们通常希望从这些基本构建类型继承,以制作具有微小差异的其他构建类型。 As we know the "initWith" function allows us to do this, which causes all Gradle configurations to be inherited.正如我们所知,“initWith”函数允许我们这样做,这会导致所有 Gradle 配置被继承。 The problem is it does not appear to work for Android resources.问题是它似乎不适用于 Android 资源。

The use case for this is if you have debug vs release keys for various tools (ie need to be defined as strings), and you don't want to copy/paste strings.xml files across several different resource directories.用例是如果您有各种工具的调试与发布密钥(即需要定义为字符串),并且您不想在几个不同的资源目录中复制/粘贴 strings.xml 文件。

For example, let's say I have the following buildTypes:例如,假设我有以下 buildType:

  • release释放
  • debug调试
  • qa - uses initWith debug qa - 使用 initWith 调试

If I then create resource directories:如果我然后创建资源目录:

  • debug/res/values/strings.xml调试/res/values/strings.xml
  • release/res/values/strings.xml发布/res/values/strings.xml

The qa buildType will NOT have access to the debug strings.xml. qa buildType 将无法访问调试 strings.xml。 I need to copy paste the strings.xml:我需要复制粘贴strings.xml:

  • qa/res/values/strings.xml qa/res/values/strings.xml

And of course, copy pasting is a real bad idea as any changes need to be made in every copy, and sooner or later a mistake will be made.当然,复制粘贴是一个真正的坏主意,因为需要在每个副本中进行任何更改,迟早会犯错误。 I have tried changing "qa" to "qaDebug" and "debugQa" with no luck.我尝试将“qa”更改为“qaDebug”和“debugQa”,但没有成功。 I could use productFlavors, but then I would end up with way more different variants than I need, many of which aren't going to make any sense.我可以使用 productFlavors,但最终我会得到比我需要的更多不同的变体,其中许多变体没有任何意义。 For example, to have a dev, qa, and production build which share just the two base debug and release resource directories, I would need to define something like this:例如,要拥有一个仅共享两个基本调试和发布资源目录的 dev、qa 和生产版本,我需要定义如下内容:

  • devDebug开发调试
  • debRelease释放
  • qaDebug调试
  • qaRelease发布
  • prodDebug调试
  • prodRelease prodRelease

Not only is the naming redundant, half of these build configs are unused.不仅命名是多余的,而且这些构建配置中有一半未使用。 Multiply by the fact that I have a couple other inherited buildTypes than I am mentioning in this simplified example, and you can see it gets out of hand really quickly.乘以我有几个其他继承的 buildTypes 而不是我在这个简化的例子中提到的事实,你可以看到它很快就失控了。

The other option is to define API keys as gradle variables (which I have currrently), but it's always better to use the standard approach to things as it ends up being tidier and new developers to the team can find things more easily.另一种选择是将 API 密钥定义为 gradle 变量(我目前拥有),但使用标准方法总是更好,因为它最终会更整洁,团队的新开发人员可以更容易地找到东西。 Also there are resources other than strings, ex google-services.json or even XML files, which can only be handled with resource directories.此外还有字符串以外的资源,例如 google-services.json 甚至 XML 文件,它们只能通过资源目录处理。

Appreciate everyone's input.感谢大家的投入。

You can specify a different source set for a build variant using sourceSets .您可以使用sourceSets为构建变体指定不同的源集。 In your case it should be as follows:在你的情况下,它应该如下:

android {
   ...
   sourceSets {
        yourBuildVariant {
            res.srcDirs = ['app/src/debug/res']
        }
    }
}

Now yourBuildVariant res folder is "app/src/debug/res" instead of "app/src/yourBuildVariant/res".现在你的BuildVariant res文件夹是“app/src/debug/res”而不是“app/src/yourBuildVariant/res”。

To check the list of build variants and their related source sets, in Android Studio on the right side click on Gradle -> app -> Tasks -> android and click sourceSets twice.要检查构建变体及其相关源集的列表,请在右侧的 Android Studio 中单击 Gradle -> app -> Tasks -> android 并单击 sourceSets 两次。

Here is the Android documentation.是Android文档。

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

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