普通code为不同的Andr​​oid口味 [英] Common code for different android flavors

查看:168
本文介绍了普通code为不同的Andr​​oid口味的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建立4个不同的我的Andr​​oid应用程序的味道。

I am building 4 different flavors of my Android app.

我有一个类 Customization.java 这是相同的3人,不同的1。

I have a class Customization.java that is the same for 3 of them and different for 1.

既然不能把同一类无论是在主文件夹和文件夹的味道,我现在要保持完全相同的类3份为那些3的口味。

Since I cannot put the same class both in the main folder and in the flavor folder, I now have to maintain 3 copies of the exact same class for those 3 flavors.

有没有什么办法,我可以做保持此类的只有两个版本?

Is there any way that I could do with keeping just two versions of this class?

事情我至今认为:

  1. 我看了一下味道尺寸,但事实证明,他们并不适用于这种情况。
  2. 在保持的风格中的一种只有一个文件,并通过我的构建脚本复制它。

我想知道是否有一些清洁的开箱即用。

I am wondering if there is something cleaner out of the box.

推荐答案

我想CommonsWare的评论转化为一个答案。然后我会解释它的​​最终目录设置应该是什么样子。我希望这有助于出来的人通过搜索磕磕绊绊就这个问题。

I would like to convert CommonsWare's comment to an answer. I'll then explain how the final directory setup should look like. I hope this helps out the people stumbling upon this question through search.

好了,你可以覆盖资源的口味。所以,有一个共同的   在主/ RES /布局/ 和风味特异之一    yourFlavorHere / RES /布局/

Well, you can override resources in flavors. So, have the common one in main/res/layout/ and the flavor-specific one in yourFlavorHere/res/layout/.

因此​​,如果自定义活动的布局文件名为 activity_customization.xml ,你会离开它的普通副本在的src /主/ RES /布局目录,并将修改后的布局XML的三种口味中共享使用的话,比如说 flavorFour ,其对应的源代码集目录下的的src / flavorFour / RES /布局

So, if the Customization activity's layout file is called activity_customization.xml, you'll leave its common copy shared among the three flavors under src/main/res/layout directory and place the modified layout xml to be used by, say flavorFour, under its corresponding source set directory src/flavorFour/res/layout.

这工作的方式是,由于味道一至三个(不像味4)没有提供自己版本的 activity_customization.xml 中,他们将继承一个从源组的到来。

The way this works is that since flavor one to three (unlike flavor four) haven't provided their own versions of activity_customization.xml, they'll inherit the one coming from the main source set.

这是得到棘手活动Java类。另一种可能性   这是配置风味相同的活动实施   从两个源目录拉:风味特异一年   常见的一种与普通类实现。

It's the activity Java class that gets tricky. Another possibility for that is to configure the flavors with the same activity implementation to pull from two source directories: a flavor-specific one and a common one with the common class implementation.

与资源,爪哇code文件不会被合并或撤销。所以,你不能有Java的下具有相同的完全限定类名称的文件主要,以及在任何您的味源集。如果你这样做,你会收到一个的重复类错误的。

Unlike resources, Java code files are not merged or overridden. So, you can't have Java files with the same fully qualified class name under main as well as in any of your flavor source sets. If you do, you'll receive a duplicate class error.

要解决此问题,最简单的解决方案是将自定义活动出了进入每种口味源设置。这样做是因为味道目录是相互排斥的(对方,不与),因此避免了冲突。

To resolve this issue, the simplest solution is to move Customization activity out of the main and into each flavor source set. This works because the flavor directories are mutually exclusive (with each other, not with main) hence avoiding the conflict.

但是,这意味着四分之三的四种口味有活性的副本 - 一个维护的噩梦 - 只是因为风格中的一种需要进行一些修改。要解决这个问题,我们可以引进,保持刚才的共同$的三种口味之间共享C $ C文件另一个源目录。

But this means three out of the four flavors have a duplicate copy of the activity - a maintenance nightmare - just because one of the flavors required some changes to it. To resolve this issue we can introduce another source directory that keeps just the common code files shared between the three flavors.

因此​​, build.gradle 脚本会看起来像

So, the build.gradle script would look something like

android {
    ...
    productFlavors {
        flavorOne {
            ...
        }
        flavorTwo {
            ...
        }
        flavorThree {
            ...
        }
        flavorFour {
            ...
        }
    }
    sourceSets {
        flavorOne.java.srcDir 'src/common/java'
        flavorTwo.java.srcDir 'src/common/java'
        flavorThree.java.srcDir 'src/common/java'
    }
}

注意使用 java.srcDir 的(而不是 srcDirs )其中的添加另一种Java源代码目录已经存在默认的的src / flavorX / java的

Notice the use of java.srcDir (and not srcDirs) which adds another Java source directory to the already existing default src/flavorX/java.

现在我们需要做的就是砸在的src /普通/ Java中常见的自定义活动文件来使它的口味一至三个可用。通过 flavorFour 要求修改后的版本会去根据其自己的源设置为的src / flavorFour / java的

Now all we need to do is to drop the common Customization activity file in src/common/java to make it available to the flavors one to three. The modified version required by flavorFour would go under its own source set at src/flavorFour/java.

所以,最终的项目结构将类似于

So, the final project structure would look something like

+ App // module
|- src
   |- common // shared srcDir
      |- java
       |- path/to/pkg
         |- CustomizationActivity.java // inherited by flavors 1, 2, 3
   + flavorOne
   + flavorTwo
   + flavorThree
   + flavorFour
      |- java
       |- path/to/pkg
         |- CustomizationActivity.java // per-flavor activity class
      |- res
         |- layout
            |- activity_customization.xml // overrides src/main/res/layout
   |- main
      + java
      |- res
         |- layout
            |- activity_customization.xml // inherited by flavors 1, 2, 3

这篇关于普通code为不同的Andr​​oid口味的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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