如何在Android Studio模块中添加风味?

时间:2021-08-20 20:51:41

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

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:

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

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

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

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

productFlavors {
    alpha
    production
}

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:

使用gradle 0.11,这将同步并创建assembleAlphaDebug,assembleAlphaRelease,assembleProductionDebug,assembleProductionRelease任务。当我尝试在模块中执行此操作时,我收到错误:

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

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

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?

在构建的app / src / main / AndroidManifest.xml中。由于某种原因,该模块尚未构建,因此自定义主题无效。我究竟做错了什么?

1 个解决方案

#1


65  

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:

在库模块的build.gradle中,如果在另一个模块中包含时没有指定,则需要一些额外的行来告诉它导出flavor和默认使用的构建变量:

android {
    defaultPublishConfig "productionRelease"
    publishNonDefault true

    productFlavors {
        alpha {
        }
        production {
        }
    }
}

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.

只有当有人想要依赖于productionRelease变体以外的东西时,才需要发布AnnounceNonDefault位。如果您首先在库中设置多种口味,可能就是这种情况。

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

现在,如果您通过build.gradle中的另一个模块添加依赖项:

dependencies {
    compile project(':module')
}

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

默认情况下,它将取决于productionRelease变体。如果您想依赖非默认变体:

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

#1


65  

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:

在库模块的build.gradle中,如果在另一个模块中包含时没有指定,则需要一些额外的行来告诉它导出flavor和默认使用的构建变量:

android {
    defaultPublishConfig "productionRelease"
    publishNonDefault true

    productFlavors {
        alpha {
        }
        production {
        }
    }
}

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.

只有当有人想要依赖于productionRelease变体以外的东西时,才需要发布AnnounceNonDefault位。如果您首先在库中设置多种口味,可能就是这种情况。

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

现在,如果您通过build.gradle中的另一个模块添加依赖项:

dependencies {
    compile project(':module')
}

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

默认情况下,它将取决于productionRelease变体。如果您想依赖非默认变体:

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