Android组件化构建

256 阅读4分钟

Android组件化构建

环境Android studio 3.1patch1

build:gradle:7.3.1

1. 在项目当中首先要创建一个config.gradle 来管理各版本 以及modle的依赖

image.png

定义一个项目全局变量 isRelease ,用于动态切换:组件化模式/集成化模式

// false :组件化模式(子模块可以独立运行) true:集成化模式(打包整个项目apk,子模块不可以独立运行) (下面是config.gradle的代码,当然,依赖第三方控件等等,请自行)

定义 androidId 用来管理Android版本号以及SDK版本号
androidId = [
            compileSdk : 32,
            minSdk     : 23,
            targetSdk  : 32,
            versionCode: 1,
            versionName: "1.0"
    ]
定义appId用来管理   
applicationId 以及最新版本的 namespace
 
 appId = [
            app  : "com.example.practicemodularization",
            order: "com.example.order",
            mine : "com.example.mine",
            util : "com.example.util"
    ]
config.gradle代码

ext {

   // 定义一个项目全局变量 isRelease ,用于动态切换:组件化模式/集成化模式
   // false :组件化模式(子模块可以独立运行)   true:集成化模式(打包整个项目apk,子模块不可以独立运行)
   //正式环境  测试环境
   isRelease = false

   androidId = [
           compileSdk : 32,
           minSdk     : 23,
           targetSdk  : 32,
           versionCode: 1,
           versionName: "1.0"
   ]

   appId = [
           app  : "com.example.practicemodularization",
           order: "com.example.order",
           mine : "com.example.mine",
           util : "com.example.util"
   ]


   //正式/测试环境的URL
   url = [
           debug  : "https://www.baidu.com",
           release: "https://www.wangyiyun163.com"
   ]

   appcompat = "1.4.1"
   material = "1.5.0"
   junit = "4.13.2"
   constraintlayout = "2.1.3"
   ext_junit = "1.1.3"
   espresso = "3.4.0"

   dependencies = [
           "appcompat"       : "androidx.appcompat:appcompat:${appcompat}",
           "material"        : "com.google.android.material:material:${material}",
           "junit"           : "junit:junit:${junit}",
           "constraintlayout": "androidx.constraintlayout:constraintlayout:${constraintlayout}",
           "ext_junit"       : "androidx.test.ext:junit:${ext_junit}",
           "espresso"        : "androidx.test.espresso:espresso-core:${espresso}",
   ]

}

2. 在项目工程当中再来配置gradle

// 注意!!!根目录下的build.gradle头部加入自定义的config.gradle  相当于layout布局当中加入include
apply   from:"config.gradle"

image.png 3. 在App壳工程当中再来配置gradle

plugins {
    id 'com.android.application'
}

//赋值与引用 config.gradle
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def androidx = rootProject.ext.dependencies
def url = rootProject.ext.url

android {
    namespace "com.example.practicemodularization"
    compileSdk androidId.compileSdk

    defaultConfig {
        applicationId appId.applicationId
        minSdk androidId.minSdk
        targetSdk androidId.targetSdk
        versionCode androidId.versionCode
        versionName androidId.versionName

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"


        //开启分包
        //multiDexEnabled true

        ndk {
            abiFilters('x86', "x86_64")
        }



        buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
    }

    buildTypes {
        debug {

        }
        release {
            buildConfigField("boolean", "isRelease", String.valueOf(isRelease))
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    androidx.each { k, v -> implementation v }

    //公共基础库  (网络请求,图片加载,数据库...)
    implementation project(':common')

    //如果isRelease = false 就是组件化,各个单独的模块可以运行   如果isRelease=true 就是集成化,只能一个app模块运行
    if (isRelease) {
        implementation project(':order')
        implementation project(':mine')
        implementation project(':util')
    }
}

当然 common order mine home 是我们创建的模块化包。

image.png

image.png

4.这边以其中一个modle来举例 配置home中的build.gradle

上面在config当中就有说明 isRelease变量的作用,
那么由于新版本会多出一个 plugins  在modle当中这个不用管,直接删除即可
 
plugins {
    id 'com.android.application'
}
 
 
 
if (isRelease){
    apply plugin:'com.android.library'
}else {
    apply plugin:'com.android.application'
}
 

5.下面是home当中gradle当中的代码

namespace appId.util 这里直接可以在上面来的config当中去看

 
//配置资源路径,方便测试环境,打包不集成到正式环境    
 
sourceSets {
    main {
        if (!isRelease) {
            //如果是组件化模式,需要单独运行,
            manifest.srcFile 'src/main/debug/AndroidManifest.xml'
        } else {
            //集成化模式,整个项目打包apk
            manifest.srcFile 'src/main/AndroidManifest.xml'
            java {
                //release 时 debug目录下的文件不需要合并到主工程
                exclude '**/debug/**'
            }
        }
    }
}

那么我们就需要在这里去创建debug 模式下的AndroidManifest

这是在debug包下的清单文件:kotlin工程也是可以的 image.png 这是原来的清单文件

image.png image.png 在home工程当中的main方法 去创建debug包,在debug包去创建测试模式下当中的activity以及程序

这句代码在我们切换正式环境打包时,注意!!!是打包,debug目录下的文件不会合并。
java {
   
    exclude '**/debug/**'
}

androidx.each { k, v -> implementation v } 这句代码可以试一下哈哈哈!


if (isRelease){
    apply plugin:'com.android.library'
}else {
    apply plugin:'com.android.application'
}

//赋值与引用 config.gradle
def androidId = rootProject.ext.androidId
def appId = rootProject.ext.appId
def androidx = rootProject.ext.dependencies

android {
    namespace  appId.util
    compileSdk androidId.compileSdk

    defaultConfig {
        if (!isRelease){//如果是集成化模式,不能有applicationId
            applicationId appId.util   //组件化才能有application   可以独立运行的
        }
        minSdk androidId.minSdk
        targetSdk androidId.targetSdk
        versionCode androidId.versionCode
        versionName androidId.versionName
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    //配置资源路径,方便测试环境,打包不集成到正式环境
    sourceSets {
        main {
            if (!isRelease) {
                //如果是组件化模式,需要单独运行,
                manifest.srcFile 'src/main/debug/AndroidManifest.xml'
            } else {
                //集成化模式,整个项目打包apk
                manifest.srcFile 'src/main/AndroidManifest.xml'
                java {
                    //release 时 debug目录下的文件不需要合并到主工程
                    exclude '**/debug/**'
                }
            }
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {

    androidx.each { k, v -> implementation v }
    //公共基础库  (网络请求,图片加载,数据库...)
    implementation project(':common')
}

之后我们去切换config当中的isRelease变量就可以去控制组件化和集成化了。当然如果不是独立开发,一定要注意命名 `##

那么组件化的通信问题,:1类加载模式,2公共类当中去做Map,以及APT路由,还是推荐去使用阿里路由毕竟前面2种麻烦哈哈哈,c v`