preface

Telegram is a powerful end-to-end encryption IM, focus on the security and speed of support Android/IOS/Windows/macOS platform.

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

1 2

The source code to compile

The compilation environment is Android Studio4.2.1, and the JDK uses OpenJDK 11 of Android Studio

  • From github.com/DrKLO/Teleg… Download Telegram source code
  • Copy oneselfrelease.keystoretoTMessagesProj/configdirectory
  • ingradle.propertiesFill in the fileRELEASE_KEY_PASSWORD.RELEASE_KEY_ALIAS.RELEASE_STORE_PASSWORD, such as:
RELEASE_KEY_PASSWORD=123456
RELEASE_KEY_ALIAS=jack
RELEASE_STORE_PASSWORD=123456
Copy the code
  • accessconsole.firebase.google.com, create two Android applications with the application IDS respectivelyorg.telegram.messengerandorg.telegram.messenger.betaThen download Google-services. json and copy it to the TMessagesProj directory

  • The loginmy.telegram.orgTo apply forapi_id and api_hash, and then replaceTMessagesProj/src/main/java/org/telegram/messenger/BuildVars.javaIn the fileAPP_IDandAPP_HASHfield

  • Compile run program

Source code analysis

Telegram source code is extremely large and complex, extensive and profound, only a little bit of its sticker use library Rlottie, as follows:

  • The sticker is 512 by 512 pixels, lasts no longer than 3s and runs 60 frames per second
  • The file name suffix is. TGS, and the local cache path isAndroid/data/org.telegram.messenger/cache
  • The TGS file can be dragged and dropped directly to lottiefiles.com/preview to preview the effect
1 2 3

Rlottie code analysis

Native code

  • TMessagesProj/jni/lottie.cppRlottie wraps classes that define native functions for the Java layer to call
  • TMessagesProj/rlottieRlottie core code
  • TMessagesProj/lz4Compress library code

Java code

  • RLottieImageViewInheritance inImageView, internal callRLottieDrawableShow and play TGS animation
  • RLottieDrawableInheritance inDrawableCall native code to create render animation
// Create an animation using the file path
public static native long create(String src, String json, int w, int h, int[] params, boolean precache, int[] colorReplacement, boolean limitFps);

// Create animations using json strings
protected static native long createWithJson(String json, String name, int[] params, int[] colorReplacement);

// Destroy the animation
public static native void destroy(long ptr);

// Sets the color of the specified layer
private static native void setLayerColor(long ptr, String layer, int color);

// Replace the color
private static native void replaceColors(long ptr, int[] colorReplacement);

// Get a frame animation
public static native int getFrame(long ptr, int frame, Bitmap bitmap, int w, int h, int stride, boolean clear);

// Create a cache
private static native void createCache(long ptr, int w, int h);
Copy the code

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