Android模块化与组件化--多模块区分编译

模块化与组件化--多模块区分编译

示例地址:github.com/JackyAndroi…
Android-Architecture-Fairy开源项目重点分析主流技术与架构设计,欢迎star

有时我们一个项目中存在多个产品形态,且不同产品需要不同的编译环境,这是模块化组件化的基础。最普通的情况便是在主模块里面加入渠道统计,但此时如果我们有多个Library,多种产品形态的主模块需要编译多种产品形态的Library,怎么办?先看下官方文档如下:

By default a library only publishes its release variant. This variant will be used by all projects referencing the library, no matter which variant they build themselves. This is a temporary limitation due to Gradle limitations that we are working towards removing. You can control which variant gets published:

android {
    defaultPublishConfig "debug"
}复制代码

Note that this publishing configuration name references the full variant name. Release and debug are only applicable when there are no flavors. If you wanted to change the default published variant while using flavors, you would write:

android {
    defaultPublishConfig "flavor1Debug"
}复制代码

It is also possible to publish all variants of a library. We are planning to allow this while using a normal project-to-project dependency (like shown above), but this is not possible right now due to limitations in Gradle (we are working toward fixing those as well).
Publishing of all variants are not enabled by default. The snippet below enables this feature:

android {
    publishNonDefault true
}复制代码

It is important to realize that publishing multiple variants means publishing multiple aar files, instead of a single aar containing multiple variants. Each aar packaging contains a single variant. Publishing a variant means making this aar available as an output artifact of the Gradle project. This can then be used either when publishing to a maven repository, or when another project creates a dependency on the library project.

Gradle has a concept of default" artifact. This is the one that is used when writing:

dependencies {
    compile project(':libraries:lib2')
}复制代码

To create a dependency on another published artifact, you need to specify which one to use:

dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}复制代码

Important: Note that the published configuration is a full variant, including the build type, and needs to be referenced as such.
Important: When enabling publishing of non default, the Maven publishing plugin will publish these additional variants as extra packages (with classifier). This means that this is not really compatible with publishing to a maven repository. You should either publish a single variant to a repository OR enable all config publishing for inter-project dependencies.

默认Library只发布Release版本,这个是Gradle官方的限制,Google官方正在试图解决这个问题。可以使用defaultPublishConfig去设置发布的版本,通过设置publishNonDefault true可以让Library发布多个产品版本。
下面的这种平常书写的方式是依赖的默认发布版本:

dependencies {
    compile project(':libraries:lib2')
}复制代码

如果想要分渠道编译多种形态的Library,需要修改如下的方式:

dependencies {
    flavor1Compile project(path: ':lib1', configuration: 'flavor1Release')
    flavor2Compile project(path: ':lib1', configuration: 'flavor2Release')
}复制代码

注意事项:

  1. 编译的配置是产品名称的全称加编译类型,如:flavor1Release
  2. 如果Library是Maven的公开库最好不要使用这种特性
  3. 产品名称首字母要小写,否则会有语法问题
  4. 如果Gradle里面有使用MultiDex选项会生成多个aar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值