主要观点总结
本文介绍了淘天集团直播用户终端技术团队在直播业务中实现“多端一码”的技术方案,重点使用Kotlin Multiplatform(KMP)解决多端代码逻辑差异、提升开发效率和维护性的实践过程。文章从背景问题出发,详细阐述了KMP的核心技术特性,并结合直播业务的实际场景,分享了从技术预研到复杂业务落地、再到架构迁移的完整经验。通过这一系列探索与实践,团队验证了KMP在直播核心业务中的可行性与优势。
关键观点总结
关键观点1: 背景问题
随着直播技术业务的不断迭代,直播各端业务代码以及架构差异变得愈发明显,同时双端不平衡的开发资源加剧了这种差异。为了解决这些问题,团队开始寻找一种能够实现多端一码的技术方案。
关键观点2: KMP简介
Kotlin Multiplatform(KMP)是一种跨平台开发框架,旨在通过共享代码库来简化在多个平台(如Android、iOS、Web、桌面等)上的应用开发。KMP利用Kotlin语言的多平台特性,使开发者能够编写一次核心业务逻辑代码,并在不同平台上复用,从而提高开发效率,减少维护成本。
关键观点3: KMP在直播业务中的应用
团队通过实践将KMP应用于直播业务,从小规模试验到大规模部署,逐步验证KMP在直播核心业务中的可行性和优势。主要包括技术预研、复杂业务实践和架构迁移三个阶段。
关键观点4: 性能与稳定性数据
相对于Native代码,KMP开放架构在直播间首帧性能和稳定性方面有明显提升。约有XX%的代码逻辑迁移到KMP后,crash率也有所下降。
关键观点5: 团队介绍
淘天集团直播用户终端技术团队专注于通过技术创新为用户打造流畅、互动性强且高效的直播间观看与消费体验。团队积极探索新技术,如KMP、前端alive互动组件等,并关注性能优化和稳定性提升。
正文
-
共享一部分业务逻辑,其它业务逻辑和UI依然保持各端独立。
-
共享全部业务逻辑,仅UI保持各端独立。
-
共享全部业务逻辑和UI(基于ComposeUI)。
基于以上的开发路线,让业务能够实现渐进式迁移。
媲美原生的性能
Kotlin语言
Kotlin做为现代化语言,主要优势如下:
-
简洁安全:减少代码量,避免常见错误(如空指针)。
-
多范式融合:灵活选择面向对象或函数式风格。
-
高效异步:协程简化并发编程。
学习曲线相较C++等传统语言来说更为平缓,尤其对于有 Java 背景的开发者来说,Kotlin 提供了更简洁和现代化的语法,能够快速上手。
直播实践
(笔者为iOS开发工程师,以下落地过程均以iOS开发视角输出)
尽管根据官方介绍,该方案与直播业务所需的多端方案具有较高的匹配度,但本着谨慎细致的原则,实施过程仍将遵循从小规模试验到大规模部署的路径进行。
在Q1阶段,我们的主要工作集中在以下几个方面:
-
Kotlin语言
:
投入时间深入学习Kotlin编程语言,了解其核心概念和高级特性。
-
Demo搭建运行
:
搭建KMP的样例工程,对开发环境做到初步了解。
-
基础工具链
:
与合作团队确定基础工具链功能,提供三端可用的开发环境以及打包流程。
Q1阶段结束时得到了以下结果:
1. 开发环境统一:
-
Kotlin开发IDE:IntelliJ IDEA (及其衍生 Android Studio)
-
iOS运行IDE: Xcode
-
鸿蒙运行IDE: DevEco-Studio
2. 基础工具链
-
KMP环境安装插件:mtl-cli-kmp
-
跑通mtl三端打包 & 本地编译调试
3. 一些学习心得
-
官方参考文档:
https://kotlinlang.org/docs/home.html
-
Kotlin中文文档:
https://book.kotlincn.net/
-
通过LLM(Claude、GPT等等)帮助学习
-
参考提示词
Create a coding style guide in Kotlin for professionals using best practices and conventions.
Refer to the official Kotlin [style guide](https://developer.android.com/kotlin/style-guide?hl=en) and [coding conventions](https://kotlinlang.org/docs/coding-conventions.html) as your primary resources.
# Steps
1. **Introduction**
- Start with a brief explanation of the importance of following a consistent coding style.
- Mention the intended audience and the scope of this guide.
2. **General Format**
- Guidelines for structuring code files, including package organization.
- Rules on indentation, line length, and whitespace usage.
3. **Naming Conventions**
- Specification for naming variables, functions, classes, interfaces, and types.
- Explanation of camelCase and PascalCase usage.
4. **Code Structure**
- Best practices for organizing functions, classes, and modules.
- Instructions on using access modifiers effectively.
5. **Commenting and Documentation**
- Guidelines for using comments to explain complex or non-obvious code.
- How to use Kotlin’s KDoc for generating API documentation.
6. **Coding Practices**
- Recommendations for using expressions, statements, and control flow constructs.
- Advice on exception handling, collection operations, and language idioms.
7. **Advanced Topics**
- Tips on writing efficient code and leveraging Kotlin-specific features like coroutines and inline functions.
8. **Sample Code and Examples**
- Provide sample code snippets that illustrate the core concepts and rules.
- Use placeholders like `[ClassNameCapitalized]`, `[intVariableName]`, `[functionName]` to emphasize naming conventions.
# Output Format
Compose the guide in a structured document format with clearly defined sections.
Include code examples and comments where appropriate to illustrate key points.
# Notes
- Ensure clarity and precision in language.
- Include references to the official style guides where applicable.
- Make sure the style guide is professional and adheres to industry standards.