前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Android 组件化】从模块化到组件化

【Android 组件化】从模块化到组件化

作者头像
韩曙亮
发布2023-03-29 12:28:40
9240
发布2023-03-29 12:28:40
举报
文章被收录于专栏:韩曙亮的移动开发专栏

文章目录

一、从模块化到组件化


Android 应用项目 , 都存在一个应用模块 ( Application Module ) , 在 build.gradle 构建脚本中 , 第一个插件配置 com.android.application , 表明 该 Module 编译打包后的输出是 APK 安装包 ; 该项目可以直接运行 ;

代码语言:javascript
复制
plugins {
    id 'com.android.application'
    id 'kotlin-android'
}

如果在 build.gradle 配置的是 com.android.library 插件 , 那么 编译 Module 打包后输出的是 aar 依赖库 ; 该项目不能直接运行 ;

代码语言:javascript
复制
plugins {
    id 'com.android.library'
    id 'kotlin-android'
}

模块化 :

随着应用业务增加 , 功能变得越来越复杂 , 不能将所有的功能放在一个 Application 模块中 ;

大型项目的开发不能只有一个 Module , 大多数情况下 , Android 工程中 , 除了有一个 Application 模块外 , 还有若干 Library 模块提供给应用模块引用 ;

应用中还可能存在一个基础的 SDK 依赖库 , 提供给 Library 模块引用 , Application 再引用这些 Library 模块 ;

模块化的缺点 :

Library 模块中实现了一个功能 , 如果要运行的话 , 需要借助 Application 模块 , 这就需要将整个项目全部编译一遍 , 如果项目有几百个模块 , 调试运行就很困难 ;

单个开发者可能只负责几个模块 , 还涉及了与其它模块开发人者进行协作 ;

组件化 :

组件化是在模块化的基础上 , 可以 动态切换其模块类型 , 将 Library 模块切换成 Application 模块 , 这样独立的模块可以直接运行 ;

在进行 组件模式 开发时 , 将其变成 Application 模块 , 在 集成模式 开发时 , 将其变成 Library 模块 ;

组件开发时 , 单个 Library 模块变成 Application 模块 , 可以生成独立运行的 APK 安装包 ;

二、build.gradle 构建脚本分析


组件化实现需要依赖 Gradle ;

build.gradle 脚本都是使用 Groovy 语言编写的代码 , Groovy 也是 JVM 上语言 , 与 Java 语言完全兼容 , 其调用的 api 都是 Java 语言的 ;

Android Studio 中的 Android 工程 , 在 Project 层级下有一个 build.gradle 构建脚本 , 在 Application 模块 和 Library 模块 中 , 也都各自存在一个 Module 级别的 build.gradle 构建脚本 ;

Project 下的 build.gradle 编译时会被翻译成 Project.java 类对象 , 该类路径是 gradle-6.5\src\core-api\org\gradle\api\Project.java ;

代码语言:javascript
复制
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.4.31"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.1.2"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

其中的 buildscript , allprojects 等都是 Project.java 中的函数 ;

代码语言:javascript
复制
@HasInternalProtocol
public interface Project extends Comparable<Project>, ExtensionAware, PluginAware {

    /**
     * <p>Configures the build script classpath for this project.
     *
     * <p>The given closure is executed against this project's {@link ScriptHandler}. The {@link ScriptHandler} is
     * passed to the closure as the closure's delegate.
     *
     * @param configureClosure the closure to use to configure the build script classpath.
     */
    void buildscript(Closure configureClosure);

    /**
     * <p>Configures this project and each of its sub-projects.</p>
     *
     * <p>This method executes the given closure against this project and its sub-projects. The target {@link Project}
     * is passed to the closure as the closure's delegate.</p>
     *
     * @param configureClosure The closure to execute.
     */
    void allprojects(Closure configureClosure);
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-05-14,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 一、从模块化到组件化
  • 二、build.gradle 构建脚本分析
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档