【问题标题】:In Multi Flavour app, how to avoid duplicate resources在 Multi Flavor 应用中,如何避免重复资源
【发布时间】:2016-02-08 17:08:35
【问题描述】:

如果我有 3 种风味风味 1、风味 2 和风味 3,则每种风味都有 Dev、Pat 和 Prod “子风味”版本,它们的参数不同,但每种主要风味都有不同的资源。

所以我现在有 9 种不同的风格,但只有 3 个不同的资源文件夹)。我希望相同的“子口味”使用相同的资源。

我该怎么做?我在有关 flavorDimensions 的文档中看到过,但不确定如何配置资源文件夹。

目前我正在使用类似的东西

sourceSets {

   flavor1_dev{
            res.srcDir  'src/flavor1/res'
        }

   flavor1_prod{
            res.srcDir  'src/flavor1/res'
        }

   flavor2_dev{
            res.srcDir  'src/flavor2/res'
        }

   flavor2_prod{
            res.srcDir  'src/flavor2/res'
        }

}    

【问题讨论】:

  • 是的,它有效,但我想知道如何使用 flavorDimensions ,或者是否有其他方法。
  • 您可以排除来自不同风格构建的资源,请查看stackoverflow.com/questions/33263567/…

标签: android android-gradle-plugin android-productflavors android-flavordimension


【解决方案1】:

您需要 Gradle 风味,例如。 flavor1flavor2 等,以及 devprod 等构建类型。

参见示例:http://developer.android.com/tools/building/configuring-gradle.htmlhttp://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Sourcesets-and-Dependencies

例如:

android.sourceSets.flavor1Debug 位置 src/flavor1Debug/

android.sourceSets.flavor1Release 位置 src/flavor1Release/

android.sourceSets.flavor2Debug 位置 src/flavor2Debug/

android.sourceSets.flavor2Release 位置 src/flavor2Release/

另外,这个问题很像How can I specify per flavor buildType sourceSets?

【讨论】:

【解决方案2】:

只需将公共资源放在 main/res 中即可。 main/res 中的所有资源都将被风味覆盖。 例如如果你想覆盖字符串资源添加,

主字符串文件:/src/main/res/values/strings.xml

<resources>
    <string name="app_name">Main</string>
    <string name="app_name_full">Main app</string>
    <string name="app_email">main@abc.com</string>
    <string name="app_phone">123</string>
    <string name="app_website">www.abc.com</string>
</resources>

和风味字符串文件:/src/flavor1/res/values/strings.xml

<resources>
    <string name="app_name">Flavour App Name</string>
</resources>

只有 app_name 会在风味中发生变化,而 app_full_name 等其他资源仍可在所有风味中访问。

【讨论】:

  • 您不能使用 main 并期望它被覆盖。这行不通。 Main 应该用于“in everything”,否则需要分开。
  • @StarWind 在这里有一点,这将在编译时因“重复资源”而失败
猜你喜欢
  • 1970-01-01
  • 2011-04-03
  • 2019-10-31
  • 2010-10-10
  • 2022-12-25
  • 2015-04-10
  • 2022-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多