Meta has been at work to port their Android codebase from Java to Kotlin. Within the course of, they’ve discovered quite a lot of classes of normal curiosity and developed a couple of 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 help, shorter code, and the opportunity of 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 doable, principally to stop Java null pointers from creeping into the Kotlin codebase and to cut back the remaining Java code requiring upkeep. This was no straightforward activity and required fairly some investigation at begin.
A primary impediment Meta engineers needed to overcome got here from a number of inside 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 component of syntax highlighter Pygments, and constructed a Kotlin symbol 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, out there as a compiler plugin. This labored fairly nicely apart from quite a lot of particular frameworks, together with JUnit, for which the instrument lacks ample data to have the ability to produce appropriate conversions.
We’ve got discovered many situations of those small fixes. Some are straightforward to do (comparable 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 appropriate method to deal with this instances consisted in a three-step pipeline to first put together Java code, then robotically run J2K in a headless Android Studio occasion, and at last postprocess the generated information to use all required refactoring and fixes. Meta has open sourced quite a lot of these refactorings to assist different builders to perform the identical duties.
These automations don’t resolve all the issues, however we’re in a position to prioritize the most typical ones. We run our conversion script (aptly named Kotlinator) on modules, prioritizing energetic and less complicated modules first. We then observe the ensuing commit: Does it compile? Does it cross 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 in a position to port over 10 million strains of Kotlin code, permitting thus the vast majority of Meta Android engineers to change to Kotlin for his or her each day duties. The method additionally confirmed quite a lot of anticipated outcomes, together with shorter generated code and unchanged execution pace. On the destructive facet, although, Kotlin compiler proved considerably slower than Java’s. This opened up a brand new entrance for optimization by utilizing KSP for annotation processing and bettering 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 if you’re within the full element.