DEV Community

Cover image for Open Source Astronomy on Android and iOS With One Patch Script
Nesim Razon
Nesim Razon

Posted on Originally published at nesimapps.com

Open Source Astronomy on Android and iOS With One Patch Script

Our horoscope and moon app needs real astronomy: where the Moon is, when the phase changes, when a planet looks like it moves backwards. We use an open source engine for that, and it worked on Android. When we moved the app to Kotlin Multiplatform for iPhone, the engine did not come along. This is how we made it run on both platforms without changing a single calculation.

The problem: a JVM-only artifact

The engine is published as a Kotlin library, and the ready-made version on the central Maven repository works only on the Java virtual machine. That is fine on Android and useless in shared code that also has to compile for iPhone. We did not want to rewrite thousands of lines of orbital maths, nor to call a different library on each platform, because two engines can give two slightly different moon phase times.

Use the source, not the package

The engine is open source under the MIT licence and the whole thing is one Kotlin source file. So instead of the package we took the source file itself and put it into the common source set of the shared module. The question became what in that file is Java-specific. It turned out to be very little.

What had to change

  • A file-level annotation that sets the Java class name, which common code does not know.
  • A few annotations that make functions static or generate Java overloads, again Java-only. Removing them changes nothing for Kotlin callers.
  • One use of a Java synchronization block, guarding the cache of an object we do not use, replaced with a plain block.
  • One string formatting call in the time-to-text function, replaced by manual zero padding with the same output.

A patch script that fails loudly

We did not edit the file by hand. A small script applies each change with a regular expression and asserts how many matches it expects. If a future version of the engine moves a line, the script stops with an error instead of silently producing a half-patched file. The script also writes a header comment into the result, saying which engine version it is, that only these changes were made, and that the calculation code is untouched, which keeps the licence notice honest.

Updating the engine becomes a repeatable routine: download the new source, run the script, run the tests.

Proving nothing changed: golden tests

Nobody should trust a port because the compiler is happy. Before the migration we recorded the output of the old Android code for a large set of cases: a reading summary for every sign over more than a year of days, full text samples, moon sign changes, phases and retrograde dates, the ISO week and compatibility results, plus a hash of the content bank. After the migration the shared code has to reproduce those files exactly.

It does, and that is the real safety net. When the content bank text changes on purpose, the golden files are regenerated with one switch and the diff is reviewed like any other change.

The rest of the move

Around the engine, the usual replacements applied: Java time classes became the Kotlin date and time library, a JSON library became Kotlin serialization, and locale-based formatting became small helpers. None of that needed a clever idea, only a list and patience. The astronomy engine was the one dependency that needed a plan.

FAQ

Why not call a native astronomy library on each platform?

Two engines can disagree slightly on moon phase times. One shared source gives identical results everywhere.

Is it allowed to modify an MIT licensed library?

Yes, as long as the licence notice stays. Our patched file keeps it and adds a header listing the changes.

What happens when the engine publishes a new version?

Download the new source, run the patch script and the golden tests. The script stops if a patch no longer applies.

How do golden tests help here?

They compare the new shared code with recorded output of the old code, so any difference in a result is caught immediately.


If you want to see this in action, Astrolings is free on Google Play and needs no account. More notes like this one live on my site.

Top comments (0)