Meta has been at work to port their Android codebase from Java to Kotlin. Within the course of, they’ve discovered a variety of classes of normal curiosity and developed just a few helpful approaches, explains Meta engineer Omer Strulovich.
Meta’s determination to undertake Kotlin for his or her Android apps was motivated by Kotlin benefits over Java, together with nullability and useful programming assist, shorter code, and the potential for creating area particular languages. It was additionally clear to Kotlin engineers that they needed to port to Kotlin as a lot of their Java codebase as potential, principally to stop Java null pointers from creeping into the Kotlin codebase and to scale back the remaining Java code requiring upkeep. This was no simple process and required fairly some investigation at begin.
A primary impediment Meta engineers needed to overcome got here from a number of inner optimization instruments in use at Meta that didn’t work correctly with Kotlin. For instance, Meta needed to replace the ReDex Android bytecode optimizer and the lexer part of syntax highlighter Pygments, and constructed a Kotlin image processing (KSP) API, used to create Kotlin compiler plugins.
On the entrance of code conversion correct, Meta engineers opted to make use of Kotlin official converter J2K, obtainable as a compiler plugin. This labored fairly nicely aside from a variety of particular frameworks, together with JUnit, for which the software lacks enough data to have the ability to produce appropriate conversions.
Now we have discovered many cases of those small fixes. Some are simple to do (akin to changing isEmpty), some require analysis to determine the primary time (as within the case of JUnit guidelines), and some are workarounds for precise J2K bugs that can lead to something from a construct error to completely different runtime conduct.
The proper method to deal with this instances consisted in a three-step pipeline to first put together Java code, then mechanically run J2K in a headless Android Studio occasion, and eventually postprocess the generated information to use all required refactoring and fixes. Meta has open sourced a variety of these refactorings to assist different builders to perform the identical duties.
These automations don’t resolve all the issues, however we’re capable of prioritize the commonest ones. We run our conversion script (aptly named Kotlinator) on modules, prioritizing lively and easier modules first. We then observe the ensuing commit: Does it compile? Does it go our steady integration easily? If it does, we commit it. And if not, we take a look at the problems and devise new computerized refactors to repair them.
Because of this method, Meta has been capable of port over 10 million traces of Kotlin code, permitting thus the vast majority of Meta Android engineers to modify to Kotlin for his or her every day duties. The method additionally confirmed a variety of anticipated outcomes, together with shorter generated code and unchanged execution pace. On the adverse facet, although, Kotlin compiler proved considerably slower than Java’s. This opened up a brand new entrance for optimization through the use of KSP for annotation processing and enhancing Java stub era and compile occasions, which remains to be an ongoing effort.
Don’t miss the unique article about Meta’s journey to undertake Kotlin in case you are within the full element.