我如何能在与Android Studio中的一个模块添加香精? [英] How can I add flavors in a module with Android Studio?

查看:187
本文介绍了我如何能在与Android Studio中的一个模块添加香精?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个套件,使用相同的模块,它包含了几乎所有的实际code项目。该项目是建立这样的:

I have a suite of projects that use the same module, which contains nearly all the actual code. The project is setup like:

project/
  - app/
    - build.gradle
  - libraries/
    - module/
      - build.gradle
  - build.gradle
  - settings.gradle

的依赖都设置正确,我可以构建和运行应用程序很棒,但是我只能口味添加到项目中,这是不理想的解决方案。 settings.gradle 的包含以下内容:

include ':app', ':libraries:module'

在应用程序目录中的 build.gradle 的文件,我增加了以下块:

In the app directory's build.gradle file, I added the following block:

productFlavors {
    alpha
    production
}

使用摇篮0.11,此同步,并创建的 assembleAlphaDebug assembleAlphaRelease assembleProductionDebug assembleProductionRelease 的任务。当我试图做到这一点的模块中,而不是,我得到的错误:

Using gradle 0.11, this syncs and creates assembleAlphaDebug, assembleAlphaRelease, assembleProductionDebug, assembleProductionRelease tasks. When I attempt to do this in the module instead, I get the error:

没有资源的发现,匹配给定的名称(在主题,值为@风格/ MyCustomTheme')

No resource found that matches the given name (at 'theme' with value '@style/MyCustomTheme')

在建应用程序/ src目录/主/ AndroidManifest.xml中。出于某种原因,该模块是不是正在修建,所以自定义主题是行不通的。我究竟做错了什么?

in the built app/src/main/AndroidManifest.xml. For some reason, the module is not being built, so the custom theme is not working. What am I doing wrong?

推荐答案

在库模块的build.gradle,你需要一对额外的线条来告诉它导出的味道,如果不指定,当它建立的变体默认使用被列入从其他模块:

In the library module's build.gradle, you need a couple extra lines to tell it to export the flavors and which build variant to use by default if not specified when being included from another module:

android {
    defaultPublishConfig "productionRelease"
    publishNonDefault true

    productFlavors {
        alpha {
        }
        production {
        }
    }
}

这是 publishNonDefault 位仅在必要的,如果有人想依靠比 productionRelease 变异的东西等。 presumably这种情况下,如果你设置了多口味的资料库摆在首位。

That publishNonDefault bit is only necessary if someone would want to depend on something other than the productionRelease variant. Presumably this is the case if you set up multi-flavors in your library in the first place.

现在,如果你从另一个模块通过本在其build.gradle添加一个依赖:

Now if you add a dependency from another module via this in its build.gradle:

dependencies {
    compile project(':module')
}

这将取决于默认的 productionRelease 变种。如果您想依赖于一个非默认的变体:

it will depend on the productionRelease variant by default. If you'd like to depend on a non-default variant:

dependencies {
    compile project(path: ':module', configuration:'alphaDebug')
}

这篇关于我如何能在与Android Studio中的一个模块添加香精?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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