Skip to content

chore(android): replace math engine with RaTeX#355

Open
eszlamczyk wants to merge 11 commits into
mainfrom
chore/android/replace-android-math-with-ratex
Open

chore(android): replace math engine with RaTeX#355
eszlamczyk wants to merge 11 commits into
mainfrom
chore/android/replace-android-math-with-ratex

Conversation

@eszlamczyk
Copy link
Copy Markdown
Collaborator

What/Why?

Introduces new math engine - RaTeX - replacing Android math.

AndroidMath was unmaintained, required JitPack, and shipped libc++_shared.so which caused duplicate native library conflicts in instrumentation builds (Detox). It also used reflection-heavy metric extraction for baseline alignment of inline math.

What changed:
  • build.gradle - swaps com.github.gregcockroft:AndroidMath for io.github.erweixin:ratex-android:0.1.9, removes the JitPack repository, and removes the packaging { pickFirsts } workarounds that existed solely for AndroidMath
  • consumer-rules.pro - updates ProGuard keep rules from com.agog.mathdisplay.** / com.pvporbit.freetype.** to io.ratex.**
  • MathInlineSpan - replaces MTMathView + reflection-based ascent/descent extraction with RaTeXEngine.parseBlocking + RaTeXRenderer drawn to a Bitmap; removes the reflection companion entirely
  • MathMeasureHelper - drops the CountDownLatch/main-thread dispatch (no longer needed since RaTeX is allegedly thread-safe); renamed from measureOnMainThread to measure
  • MathContainerView - replaces MTMathView with a lightweight inline View that renders synchronously via RaTeXEngine.parseBlocking

Testing

Start example app and try different markdown combinations via set markdown button (in playground - can be done one by one or via a combination):

$$E = mc^2$$

$$\psi(x, t) = A e^{i(kx - \omega t)}$$

$$\begin{aligned}
\nabla \cdot \mathbf{E} &= \frac{\rho}{\varepsilon_0} \\
\nabla \cdot \mathbf{B} &= 0 \\
\nabla \times \mathbf{E} &= -\frac{\partial \mathbf{B}}{\partial t} \\
\nabla \times \mathbf{B} &= \mu_0\mathbf{J} + \mu_0\varepsilon_0\frac{\partial \mathbf{E}}{\partial t}
\end{aligned}$$

$$\mathbf{R}_z(\theta) = \begin{bmatrix} 
\cos\theta & -\sin\theta & 0 \\ 
\sin\theta & \cos\theta & 0 \\ 
0 & 0 & 1 
\end{bmatrix}$$

$$f'(x) = \lim_{h \to 0} \frac{f(x+h) - f(x)}{h}$$

$$\int_{-\infty}^{\infty} e^{-x^2} \, dx = \sqrt{\pi}$$

$$f(n) = \begin{cases} 
\frac{n}{2} & \text{if } n \text{ is even} \\ 
3n + 1 & \text{if } n \text{ is odd} 
\end{cases}$$

$$aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa$$

$a^2$

Random text with random $inline$ and $$a^2$$ 

Additionaly change apps/example/src/screens/playground/PlaygroundScreeen.tsx's MARKDOWN_STYLE fields (line 50) to include math and inlineMath stylings, for example:

math: {
  color: '#1e40aa',
  backgroundColor: '#eff6ff',
  padding: 12,
  marginTop: 8,
  marginBottom: 8,
  textAlign: 'center' as const,
},
inlineMath: {
  color: '#7c3aed',
},

PR Checklist

  • Code compiles and runs on iOS
  • Code compiles and runs on Android
  • Updated documentation/README if applicable
  • Ran example app to verify changes
  • E2E tests are passing
  • Required E2E tests have been added (if applicable)

@eszlamczyk eszlamczyk requested a review from hryhoriiK97 May 26, 2026 16:12
Copy link
Copy Markdown
Collaborator

@hryhoriiK97 hryhoriiK97 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! This is a great change 🙌 I've left a few comments - please take a look at them 🙏

Comment thread android/src/math/java/com/swmansion/enriched/markdown/views/MathContainerView.kt Outdated
Comment thread android/src/math/java/com/swmansion/enriched/markdown/views/MathContainerView.kt Outdated
Comment thread android/src/math/java/com/swmansion/enriched/markdown/views/MathContainerView.kt Outdated
Comment thread android/src/math/java/com/swmansion/enriched/markdown/spans/MathMeasureHelper.kt Outdated
@eszlamczyk eszlamczyk requested a review from hryhoriiK97 May 27, 2026 14:39
@eszlamczyk
Copy link
Copy Markdown
Collaborator Author

Note, bumping RaTeX in this pr as new version has important fix

@eszlamczyk
Copy link
Copy Markdown
Collaborator Author

eszlamczyk commented May 29, 2026

Configuration Size (MB) Size (KB)
ratex + math=true 34MB 35,326.06 KB
ratex + math=false 31MB 31,807.24 KB
android math + math=true 33MB 34,014.68 KB
android math + math=false 31MB 31,807.24 KB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants