如何使用 Android Studio 在模块中添加风味? [英] How can I add flavors in a module with Android Studio?

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

问题描述

我有一套使用相同模块的项目,其中包含几乎所有实际代码.项目设置如下:

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 包含以下内容:

The dependencies are all setup correctly, and I can build and run apps great, however I can only add flavors to the project, which is not the ideal solution. settings.gradle contains the following:

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

在 app 目录的 build.gradle 文件中,我添加了以下块:

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

productFlavors {
    alpha
    production
}

使用 gradle 0.11,这会同步并创建 assembleAlphaDebugassembleAlphaReleaseassembleProductionDebugassembleProductionRelease 任务.当我尝试在模块中执行此操作时,出现错误:

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:

未找到与给定名称匹配的资源(在 'theme' 处,值为 '@style/MyCustomTheme')

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

在内置的app/src/main/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 变体以外的其他东西时才需要.如果您首先在库中设置了多种口味,大概就是这种情况.

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 中的 this 添加来自另一个模块的依赖项:

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天全站免登陆