跪拜 Guibai
← All articles
HarmonyOS · Android · AI Coding

AI Did the Grunt Work: Porting an RTF Parser from Android to HarmonyOS

By aqi00 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Cross-platform code migration—historically a multi-day slog of language translation, build fixing, and debugging—now fits in an afternoon, provided the output is checked. The AI faithfully carried over the original Android bugs, so human verification remains the unavoidable final step.

Summary

HarmonyOS natively supports PDF, TXT, EPUB, MOBI, and AZW3, but not RTF, DjVu, or DOCX. Porting an existing parser from another platform normally means translating C/Java/JavaScript code, fixing build issues, and debugging the result—days of tedious work. This walkthrough shows how AI compressed that into a few hours.

The developer chose RTF because it is a simple, cross-platform ASCII-based format. After asking Doubao for open-source RTF parsers, they landed on LibreraReader, a pure-Java reader using the BSD-licensed RtfParserKit. They then used Trae to strip the RTF-related code out of LibreraReader into a new Android project, fix a bug where images came out as 16-byte files, then convert that entire Android project to a HarmonyOS app in ArkTS. Both conversions took about an hour of AI computation, plus debugging.

Two bugs surfaced: odd alphanumeric strings mixed into text and missing images. Trae traced both to an off-by-one error in readCommand() that had been copied from the Java version. A second test found missing bold/italic and dead hyperlinks, which turned out to be a pre-existing flaw in the original Android code that AI had faithfully ported. AI fixed that too, and the final app renders rich RTF correctly.

Takeaways
— RTF is a cross-platform ASCII-based format that supports basic formatting like fonts, colors, styles, and images, which makes it a good test case for porting.
— HarmonyOS natively supports PDF, TXT, EPUB, MOBI, and AZW3 but not RTF, DjVu, or DOCX.
— LibreraReader was chosen because it is pure Java, avoiding NDK compilation, and its underlying RtfParserKit uses the permissive BSD license.
— Trae stripped the RTF parsing code from LibreraReader into a new Android project in 30 minutes, and then fixed a bug that produced 16-byte PNG files in 15 minutes.
— Converting the Android project to HarmonyOS ArkTS took nearly an hour of AI computation.
— Two separate display bugs (garbled strings and missing images) shared one root cause: an off-by-one error in readCommand() when handling control-word delimiters.
— Missing bold, heavy text, and hyperlinks were traced to a bug that already existed in the original Android code, which the AI carried over into the HarmonyOS version.
— The final HarmonyOS app correctly renders both plain-text RTF with bold and links, and rich-text RTF with images.
Conclusions

AI can handle the heavy lifting of code extraction, language translation, and bug fixing, but it requires the developer to supply domain direction, choose the right library, and verify the output.

The AI faithfully copied a pre-existing Android bug into the HarmonyOS port, showing that automated migration preserves defects as readily as it preserves functionality.

Choosing a pure-Java library with a permissive license simplified the AI-driven port significantly; avoiding NDK and C++ complexity directly reduced integration friction.

Source: juejin.cn ↗ Google Translate ↗ Backup ↗