美文网首页Android复习
Gradle 自我学习八-项目实战

Gradle 自我学习八-项目实战

作者: 程序员阿兵 | 来源:发表于2021-04-01 09:27 被阅读0次

    由前面的基础篇集合项目需求,开始着手项目实战。

    需求

    • 目前项目使用的是组件化,各组件虽然已经划分开各司其职独立运行。但是随着业务需求的递增,组件会越来越多。每个组件都在一个project 中,不仅打包编译时间越来越久,团队协作也随之变得很麻烦。

    目前的项目结构

    image.png

    结果导向我拉取项目所有工程组件也会都拉下来,可这并不是想要的,我只想运行自己开发的组件,那怎么办?

    组件管理拆分 ,每一个组件是唯一的git 仓库 只留下一个空壳子 app 主工程
    • 说干就干!这是优化后的结构
    image.png
    • 上面每个 lib 都是一个git 仓库 各自维护

    但是又有一个问题 ,假如一个新人入职 第一次拉取项目 对项目又不熟悉 ,岂不是让他一个个去拉取配置成一个完整的项目。一个好的项目架构应该让开发者用起来既简单又稳定高扩展!

    接下来就是本文的实战重点了,我要通过一个配置文件,让使用者在一个空壳子工程中执行脚本就能构建一个完整的项目

    • 下面演示效果


      image.png
    • 上面只是一个空壳子工程,和工程构建的脚本。重新rebuild 一下 执行脚本


      image.png

    结果各自项目会拉取到对应目录。那具体怎么实现呢,看一下script 目录下的的脚本


    image.png
    • module_config 这是个人维护的一份配置文件
      项目构建的配置文件 构建项目的组件名 拉取对应的分支 style代表依赖方式 1源码,2 aar
    //style代表依赖方式 1源码,2aar, 21aar依赖并且删除源文件【需要谨慎使用】, 0不处理
    gradle.ext.moduleConfigs = [
            //业务模块
            [
                    name      : "iot_ring",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "iot_band",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "service_oss",
                    git_branch: "master",
                    style     : 1
            ],
    
            [
                    name      : "service_log",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "kit_router",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "kit_http",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "kit_activity",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "kit_ui",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "kit_audio",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "kit_util",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "kit_share",
                    git_branch: "master",
                    style     : 1
            ],
    
            [
                    name      : "business_uc",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "business_web",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "business_search",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "business_inquery",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "business_message",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "business_personal",
                    git_branch: "master",
                    style     : 1
            ],
            [
                    name      : "business_home",
                    git_branch: "master",
                    style     : 1
            ],
            //注意interface是用一个git仓库
            [
                    name      : "interface",
                    git_branch: "master",
                    style     : 1
            ],
    ]
    //0 无插件,1本地插件 2远程插件
    gradle.ext.pluginStyle = 2
    
    • module_infos.gradle 这是 公共module信息 包括了 所有组件 git 地址 组件名 组件下载目录 以及远程仓库地址
    //公共module信息
    gradle.ext.moduleInfos = [
            //业务模块
    
            [
                    name      : "iot_ring",
                    source_dir: "iot/iot_ring",
                    remote_aar: "com.tal.tiku:service_push:1.0.5",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "iot_band",
                    source_dir: "iot/iot_band",
                    remote_aar: "com.tal.tiku:service_update:1.0.6",
                    git       : "https://gitlab.aaaa.com/health/android/lib_script.git"
            ],
    
            [
                    name      : "service_oss",
                    source_dir: "service/service_oss",
                    remote_aar: "com.tal.tiku:service_oss:1.0.9",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "service_log",
                    source_dir: "service/service_log",
                    remote_aar: "com.tal.tiku:service_log:1.0.11",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "kit_router",
                    source_dir: "framework/kit_router",
                    remote_aar: "com.tal.tiku:kit_router:1.0.0",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "kit_http",
                    source_dir: "framework/kit_http",
                    remote_aar: "com.tal.tiku:kit_http:1.0.9",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "kit_activity",
                    source_dir: "framework/kit_activity",
                    remote_aar: "com.tal.tiku:kit_activity:1.0.9",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "kit_ui",
                    source_dir: "framework/kit_ui",
                    remote_aar: "com.tal.tiku:kit_widget:1.0.13",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "kit_share",
                    source_dir: "framework/kit_share",
                    remote_aar: "com.tal:social:1.4.4",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "kit_audio",
                    source_dir: "framework/kit_audio",
                    remote_aar: "com.tal.tiku:kit_audio:1.0.0",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "kit_util",
                    source_dir: "framework/kit_util",
                    remote_aar: "com.tal.tiku:kit_util:1.1.5",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
    
    
            [
                    name      : "business_uc",
                    source_dir: "business/business_user",
                    remote_aar: "com.tal.tiku.business:user:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "business_sroom",
                    source_dir: "business/business_sroom",
                    remote_aar: "com.tal.tiku.business:sroom:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "business_web",
                    source_dir: "business/business_web",
                    remote_aar: "com.tal.tiku.business:web:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "business_search",
                    source_dir: "business/business_search",
                    remote_aar: "com.tal.tiku.business:psearch:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "business_inquery",
                    source_dir: "business/business_inquery",
                    remote_aar: "com.tal.tiku.business:subject:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "business_message",
                    source_dir: "business/business_message",
                    remote_aar: "com.tal.tiku.business:message:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "business_personal",
                    source_dir: "business/business_personal",
                    remote_aar: "com.tal.tiku.business:hall:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
            [
                    name      : "business_home",
                    source_dir: "business/business_home",
                    remote_aar: "com.tal.tiku.business:kousuan:2.0.1",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git"
            ],
    
    
    
    
    
    
    
          //  ---------------------注意interface是同一个git仓库
            [
                    name      : "interface",
                    source_dir: "interface",
                    git       : "https://gitlab.aaa.com/health/android/lib_script.git",
                    sub_list  : [
                            [
                                    name      : "interface_hall",
                                    source_dir: "interface/interface_hall",
                                    remote_aar: "com.tal.tiku.interface:hall:2.0.1"
                            ],
                            [
                                    name      : "interface_uc",
                                    source_dir: "interface/interface_uc",
                                    remote_aar: "com.tal.tiku.interface:uc:2.0.1"
                            ],
                            [
                                    name      : "interface_message",
                                    source_dir: "interface/interface_message",
                                    remote_aar: "com.tal.tiku.interface:message:2.0.1"
                            ],
                            [
                                    name      : "interface_search",
                                    source_dir: "interface/interface_search",
                                    remote_aar: "com.tal.tiku.interface:psearch:2.0.1"
                            ],
                            [
                                    name      : "interface_subject",
                                    source_dir: "interface/interface_subject",
                                    remote_aar: "com.tal.tiku.interface:subject:2.0.1"
                            ],
                            [
                                    name      : "interface_web",
                                    source_dir: "interface/interface_web",
                                    remote_aar: "com.tal.tiku.interface:web:2.0.1"
                            ],
                            [
                                    name      : "interface_kousuan",
                                    source_dir: "interface/interface_kousuan",
                                    remote_aar: "com.tal.tiku.interface:web:2.0.1"
                            ]
                    ]
            ],
    
    
    
    
    ]
    

    重点:

    • 具体的实现细节在 lib_config.gradle
    gradle.ext.moduleInfos.each { item ->
        def result = moduleConfigs.find { module -> module.name == item.name }
        println "=========================模块配置${item.name}"
        item.style = (result != null ? result.style : 0)
        item.git_branch = (result != null ? result.git_branch : "master")
        def filePath = "${rootProject.projectDir}/${item.source_dir}"
        def file = new File(filePath)
        if (item.style == 1) {
            //打包平台特殊需求
            if (is_build_platform) {
                if (file.exists()) {
                    file.deleteDir()
                }
                item.git_branch = build_platform_branch
            }
            //拉去代码
            if (!file.exists()) {
                def gitClone = "git clone -b ${item.git_branch} ${item.git} $filePath"
                execCommand(gitClone)
            }
            //设置每个版本的version
            item.version = "git -C ${filePath} rev-list HEAD --count".execute().text.toInteger()
            if (item.sub_list != null) {
                item.sub_list.each { sub ->
                    def projectInfo = assemblyProject(item, sub)
                    println "include${projectInfo.libName}"
                    include projectInfo.libName
                    projectInfoList.add(projectInfo)
                    projectName.add(projectInfo.name)
                }
            } else {
                item.libName = ":" + item.source_dir.replace("/", ":")
                println "include${item.libName}"
                include item.libName
                projectInfoList.add(item)
                projectName.add(item.name)
            }
        } else if (item.style == 2) {
            if (item.sub_list != null) {
                item.sub_list.each { sub ->
                    def projectInfo = assemblyProject(item, sub)
                    projectInfoList.add(projectInfo)
                    projectName.add(projectInfo.name)
                }
            } else {
                projectInfoList.add(item)
                projectName.add(item.name)
            }
        }
    }
    

    通过遍历配置文件 获取我当前需要拉取的工程。通过execCommand 去执行我当前需要clone 的指令 把当前的信息添加到 projectInfoList集合中

    • root_config.gradle
      根据上面添加入集合projectInfoList 的相关配置数据 去选择使用源码还是 aar

    implementation rootProject.depPath('interface_hall') 使用方式

    /**
     * 动态切换aar与source依赖
     * @param name
     * @return
     */
    def depPath(def name) {
        def orderModule = gradle.ext.projectInfoList.find { module -> module.name == name }
        int type = orderModule != null ? orderModule.style : 0
        if (type == 1) {
            println "${name}->${orderModule.libName}"
            return project("${orderModule.libName}")
        } else if (type == 2) {
            println "${name}->${orderModule.remote_aar}"
            return orderModule.remote_aar
        }
        throw new IllegalStateException("${name}")
    }
    

    相关文章

      网友评论

        本文标题:Gradle 自我学习八-项目实战

        本文链接:https://www.haomeiwen.com/subject/mgywhltx.html