Open source IM Telegram source code compilation and partial analysis

foreword

Telegram is a powerful end-to-end encrypted IM, focusing on security and speed, supporting platforms such as Android/IOS/Windows/macOS, rich in functions, smooth running, free and open source, and the code is meaningful for learning and research .

Telegram is a cloud-based mobile and desktop messaging app with a focus on security and speed.

1 2 3
ezgif-1-8f6a1c06cd9e.webp ezgif-3-89bc771499a8.webp ezgif-4-7f878651ff47.webp

source code compilation

The compilation environment is version Android Studio4.2.1 , JDKusing Android Studiothe built-inOpenJDK 11

image.png

  • Download Telegram source code from github.com/DrKLO/Teleg…
  • Copy your own release.keystoreto the TMessagesProj/configdirectory
  • gradle.propertiesFill RELEASE_KEY_PASSWORDin , RELEASE_KEY_ALIAS, in the file RELEASE_STORE_PASSWORD, such as:
RELEASE_KEY_PASSWORD=123456
RELEASE_KEY_ALIAS=jack
RELEASE_STORE_PASSWORD=123456
复制代码
  • Visit console.firebase.google.com , create two android applications, the application ids are org.telegram.messengerand respectively org.telegram.messenger.beta, then download google-services.json and copy it to the TMessagesProj directory

image.png

  • Log in to my.telegram.org , apply for api_id  and  api_hash , and replace the and fields TMessagesProj/src/main/java/org/telegram/messenger/BuildVars.javain the fileAPP_IDAPP_HASH

image.png

image.png

  • Compile and run the program

Source code analysis

Telegram服务端源码不开源,那研究Telegram客户端代码有何意义呢?个人认为Telegram作为世界级IM,研究其源码还是相当有意义的,如下:

  • 学习Telegram优秀的设计&编程思想,如单模块,单Activity架构,动态生成布局等
  • 将Telegram中相对独立的功能如rlottie(贴纸),MTProto(通信协议),webrtc(音视频通话)剔除其业务逻辑然后抽出来,集成到自己的项目中

Telegram源码极其庞大和复杂,博大精深(晦涩难懂),有以下特点:

  • 项目只有一个模块TMessagesProj,没有模块化,没有组件化,没有Kotlin,没有MVVM...
  • 项目极少直接使用第三方类库,没有okhttp,没有glide,包含大量c/c++代码(tgnet,ffmpeg,webrtc,rlottie...)
  • 项目中极少使用xml文件进行布局,基本都是使用代码动态生成界面,采用单Activity+多Fragment(这里的FragmentAndroid SDK里的Fragment没有任何关系)
  • 一个java类文件常常多达几千至几万行而没有任何注释说明,阅读起来感觉太爽了_^_

目前仅仅研究了一点点其贴纸使用的类库rlottie,如下:

  • 贴纸尺寸为512*512像素,时长不超过3s,每秒运行60帧
  • 贴纸文件名后缀为.tgs,本地缓存路径为Android/data/org.telegram.messenger/cache
  • tgs文件可以直接拖放到lottiefiles.com/preview上预览效果
1 2 3
22.gif 33.gif 44.gif

rlottie代码分析

native代码

  • TMessagesProj/jni/lottie.cpp rlottie包装类,定义了一些native函数,供java层调用
  • TMessagesProj/rlottie rlottie核心代码
  • TMessagesProj/lz4 压缩类库代码

java代码

  • RLottieImageView 继承于ImageView,内部调用RLottieDrawable展示播放tgs动画
  • RLottieDrawableInherited from Drawable, calling native code to create rendering animation
//使用文件路径创建动画
public static native long create(String src, String json, int w, int h, int[] params, boolean precache, int[] colorReplacement, boolean limitFps);

//使用json字符串创建动画
protected static native long createWithJson(String json, String name, int[] params, int[] colorReplacement);

//销毁动画
public static native void destroy(long ptr);

//设置指定层次的颜色
private static native void setLayerColor(long ptr, String layer, int color);

//替换颜色
private static native void replaceColors(long ptr, int[] colorReplacement);

//获取一帧动画
public static native int getFrame(long ptr, int frame, Bitmap bitmap, int w, int h, int stride, boolean clear);

//创建缓存
private static native void createCache(long ptr, int w, int h);
复制代码

I have extracted the rlottie related code from Telegram and uploaded it to github.com/kongpf8848/…

Other open source IM

resource

Guess you like

Origin juejin.im/post/7003978718662098952