产品口味和来源层次结构 [英] Product flavors and source hierarchies

查看:88
本文介绍了产品口味和来源层次结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有两种风格,为简单起见,将其称为flavoraflavorb.

My app has two flavors, call it flavora and flavorb for simplicity.

    productFlavors {
        flavora {
            resValue "string", 'app_name', "App Name Flavor A"
            applicationIdSuffix ".flavora"
        }
        flavorb {
            resValue "string", 'app_name', "App Name Flavor B"
            applicationIdSuffix ".flavorb"
        }
    }

我希望两种口味都使用所有相同的布局,所有相同的Java代码,所有相同的资源等,除了我希望flavoraflavorb对布局文件flavor_dependent_layout.xml和Java类使用不同的实现FlavorDependentJavaClass.

I want both flavors to use all the same layouts, all the same java code, all the same resources, etc, EXCEPT I want flavora and flavorb to use different implementations for layout file flavor_dependent_layout.xml and Java class FlavorDependentJavaClass.

这是正确的方法吗?

sourceSets {
    main {
        res.srcDirs = ['src/main/res']
        java.srcDirs = ['src/main/java']
    }
    flavora {
        res.srcDirs = ['src/flavora/res', 'src/main/res']
        java.srcDirs = ['src/flavora/java', 'src/main/java']
    }
    flavorb {
        res.srcDirs = ['src/flavorb/res', 'src/main/res']
        java.srcDirs = ['src/flavorb/java', 'src/main/java']
    }
}

然后,我将创建必要的文件夹来放置相应的文件,并且所有这些都可以从那里自然地工作吗?

And then I'd just create the necessary folders to place the corresponding files, and it'll all work from there naturally? How does it know that I've included the class for both flavors in case I try to perform a method and it only exists for one flavor, for example?

(此外,我还必须明确提及资产文件夹吗?因为我也有`/src/main/assets')

(Also, do I have to explicitly mention assets folders? Since I have `/src/main/assets' as well)

推荐答案

这是正确的方法吗?

Is this the correct way to do it:

否.

只需创建src/flavora/res/src/flavora/java/src/flavorb/res/src/flavorb/java/,然后将内容放入其中即可.它们会自动被添加到适当的构建路径.您无需调整srcDirs.

Just create src/flavora/res/, src/flavora/java/, src/flavorb/res/, and src/flavorb/java/, and put stuff in there. They will automatically be added to the appropriate build paths. You do not need to adjust srcDirs.

例如,如果我尝试执行一种方法,并且仅针对一种口味存在,它怎么知道我已经包含了两种口味的类?

How does it know that I've included the class for both flavors in case I try to perform a method and it only exists for one flavor, for example?

这是一个构建错误.如果您想使用Java代码,则可以使用它:

That's a build error. With Java code, if you want to have it in flavors:

  • main
  • 中不能存在相同的类
  • main引用的所有内容都必须同时存在于flavoraflavorb
  • The same class cannot exist in main
  • Anything referenced from main has to exist in both flavora and flavorb

因此,例如,如果在src/flavora/java/内部某个地方和src/flavorb/java/内部某个地方都有AdStrategy类,则 src/main/java/中也不会具有AdStrategy. mainAdStrategy上引用的任何内容(构造函数,方法等)都必须存在于flavoraflavorbAdStrategy实现中.在构建时,在构建flavora时,会自动使用src/main/java/src/flavora/java/树,并且您需要解析在main中引用的所有内容(对于flavorb构建也是如此).

So, for example, if you have an AdStrategy class somewhere inside src/flavora/java/ and somewhere inside src/flavorb/java/, you would not also have AdStrategy in src/main/java/. And anything that main refers to on AdStrategy (constructors, methods, etc.) has to exist in both flavora and flavorb's implementation of AdStrategy. At build time, when you are building flavora, the src/main/java/ and src/flavora/java/ trees are used automatically, and whatever you refer to in main needs to resolve (likewise for flavorb builds).

这篇关于产品口味和来源层次结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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