Android模組化與元件化–多模組區分編譯

墨鏡貓發表於2019-03-04

模組化與元件化–多模組區分編譯

示例地址: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

相關文章