1. 程式人生 > 其它 >Android 元件化架構

Android 元件化架構

Android 元件化架構

1、元件化優勢

  • 編譯塊
  • 模組之間耦合度降低
  • 提高程式碼複用率
  • 提高開發效率

2、元件結構

(該結構基於我 demo 僅供參考)

2.1、基本元件

基礎模組,封裝基本View,第三方SDK,網路訪問框架等

2.2、功能模組

利用基礎模組實現業務功能

2.3、App入口

配置全域性 Application 和 主Activity,不包含業務程式碼

2.4、專案結構

3、配置資訊

首先需要在 gradle.properties 中新增一個變數,標識當前產物型別

//當isModule 為 true 時,當前產物為整合產物
isModule = true

隨後在每個module 中配置 build.gradle 檔案

  • 配置殼子 build.gradle

    plugins {
        id 'com.android.application'
        id 'androidx.navigation.safeargs'
        id 'kotlin-android'
        id 'kotlin-kapt'
    }
    android {
        ...
    }
    dependencies {
        //當isModule 為 true 時,當前產物為整合產物
        if(isModule.toBoolean()){
            //匯入業務功能模組
            api project(":module_main")
        }
    }
    
  • 配置殼子 AndroidManifest.xml 檔案

    保留基本配置資訊

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.gwm.app.mykotlin">
    
        <application
            android:allowBackup="true"
            android:label="@string/app_name"
            android:supportsRtl="true">
        </application>
    
    </manifest>
    
  • 配置 公共庫 module 的 build.gradle

    plugins{
    	//由於公共庫不會作為獨立應用出現
        //所以這裡可以寫死 為 library
        id 'com.android.library'
        id 'kotlin-android'
    }
    
    • 配置公共庫的 AndroidManifest.xml

      <?xml version="1.0" encoding="utf-8"?>
      <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.gwm.app.lib.common">
      
          <application
              android:allowBackup="true"
              android:supportsRtl="true" />
      
      </manifest>
      
  • 配置 業務功能模組的 build.gradle

if(isModule.toBoolean()){
    //構建後輸出一個 APK 安裝包
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-kapt'
}else{
    //構建後輸出 ARR 包
    apply plugin: 'com.android.library'
    apply plugin: 'kotlin-android'
    apply plugin: 'kotlin-kapt'
}
android {
    sourceSets {
        main {
            if (isModule.toBoolean()) {
                //獨立除錯
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            } else {
                //整合除錯
                manifest.srcFile 'src/main/AndroidManifest.xml'
            }
        }
    }
}
dependencies {
    //匯入基本元件module
    api project(':lib_common')
}
  • 配置業務模組 AndroidManifest.xml

    配置應用入口Activity

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.gwm.app.module.main">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.MyKotlin" >
            <activity android:name=".MainActivity" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    
    </manifest>
    

4、使用

  • 直接跳轉

    目前跳轉只限於跳轉到Activity,Fragment 之間的跳轉暫未實現

    //目的Activity
    @Route(path = "/main/SecondActivity")
    class SecondActivity: BaseActivity() {}
    
    //跳轉方式
    ARouter.getInstance().build("/main/SecondActivity").navigation()
    
  • 攜帶引數

    //跳轉方式
    ARouter.getInstance().build("/main/SecondActivity")
                        .withString("key", "value")
                        .withBundle("key1", bundle)
                        .navigation()
    

    接收方

    //目的Activity
    @Route(path = "/main/SecondActivity")
    class SecondActivity: BaseActivity() {
    	@Autowried
        var key: String? = null
        @Autowried
       var key1: String? = null
        @Autowried
        var bundle: Bundle? = null
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            ARouter.getInstance().inject(this)
            Log.e("TAG", "key = $key, key1 = $key1")
        }
    }
    

引用:Android 手把手帶你搭建一個元件化專案架構 - 掘金 (juejin.cn)

Android 元件化架構設計從原理到實戰 - 掘金 (juejin.cn)

Android路由框架ARouter的基本使用 - 掘金 (juejin.cn)