Android模塊化之MicroModule(微信Pins工程)

微信Pins工程

相信你看過微信關於模塊化的分享《微信Android模塊化架構重構實踐》,也注意到裏面提到的pins工程結構。html

pins工程

做者是這樣描述的 ------「pins工程能在module以內再次構建完整的多子工程結構,經過project.properties來指定編譯依賴關係。經過依賴關係在編譯時找到全部的資源和源碼路徑。」java

仔細推敲這句話的意思,應該能知道它實現的基本原理------經過設置sourceSets指定多個java、res等路徑.android

有關sourceSets的介紹:
https://developer.android.com/studio/build/index.html#sourcesets
https://google.github.io/android-gradle-dsl/current/com.android.build.gradle.api.AndroidSourceSet.htmlgit

可是,有一個問題須要要知道的是,一個module只能指定一個AndroidManifest文件,pins工程中包含了多個AndroidManifest,它是怎麼作到的?github

研究過com.android.tools.build:gradle,會留意到它使用到一個子庫com.android.tools.build:manifest-merger,官方經過這個庫來合併多個AndroidManifest文件,或許pins工程也是用了這方式。api

接下來,再它的基礎上,我作的一些改動,取了另外一個名字叫 MicroModule,先來看一下工程結構:微信

MicroModule工程結構

與pins工程的結構大體不變,增長了androidTesttest,以及將project.properties替換爲build.gradle架構

MicroModule 介紹

基本原理是不變的,與微信pins工程同樣配置sourceSets。AndroidManifest合併用了com.android.tools.build:manifest-mergerapp

Usage

在根項目的build.gradle中添加插件依賴:ide

buildscript {
    repositories {
        jcenter()
        ...
    }
    dependencies {
        classpath 'com.eastwood.tools.plugins:micro-module:1.0.1'
        ...
    }
}

在模塊的build.gradle中引用插件並配置 MicroModule:

// 'micro-module'要置於'com.android.application'或'com.android.library'前。
apply plugin: 'micro-module'
apply plugin: 'com.android.application'

// 爲了防止兩個沒有依賴關係的MicroModule產生引用,能夠開啓下面這個代碼邊界檢查插件。
// apply plugin: 'micro-module-code-check'

...

microModule {
    // 這裏的include相似於settings.gradle中include。
    include ':p_home'
    include ':p_common'
    include ':p_base'
}

MicroModule中的build.gradle:

dependencies {
    implementation microModule(':MicroModule名稱') // 定義依賴關係,引用其餘MicroModule
    
    // 你也能夠在這裏依賴其餘第三方庫
    // implementation '***'
    // api '***'
    // ...
}

爲了使用上的更加方便,專門寫了Android Studio的插件,能快速的建立一個MicroMoudle.

插件安裝步驟:

  1. 打開 [File] -> [Settings...] -> [plugins] -> [Browse repositories...]
  2. 搜索插件名稱 MicroModule

插件詳解:
https://plugins.jetbrains.com/plugin/10785-micromodule

插件項目地址:
https://github.com/EastWoodYang/micro-module-idea-plugin

最後

MicroModule已經上傳至Github,歡迎star交流。
https://github.com/EastWoodYang/MicroModule

相關文章
相關標籤/搜索