androidkotlinarchitecturenestjskmp

Shared code with Kotlin Multiplatform between Android/iOS & Server


Let me start off by giving some context: I've built an offline Android application that contains a bunch of business logic calculations for tax deductions. They are all hardcoded in the app. I talked with a friend of mine that volunteered to make a back-end for the app so we can add more concepts like shared lists, concept of a household, etc. All good so far.

However, we hit a roadblock: how do we handle these calculations? Do we duplicate them both in the app and the back-end? Do we just accept that the app no longer properly functions in offline-mode and rely only on the back end to do these calculations for us? Or..I thought of KMP.

I already had plans to migrate the Domain Layer & Data Layer to KMP so I could port the application to iOS. He already started the back-end using NestJS w/ Typescript.

My question is: Would it be possible to use KMP for our shared calculations, that being: all the business logic calculations be shared across server & apps? I've seen the Kotlin/JS IR Compiler, but since I've never used KMP in the real world, yet, I'm not sure if I can compile something for him to use in the NestJS back-end.

In the middle of this, I'm trying to wrap my head around how a KMP architecture would work in theory, here. I've got this rough sketch of what I'm brainstorming:

enter image description here

So, here I'm assuming each KMP layer is separate from the actual Android, iOS and Server projects. Meaning, a completely separate git repository that the apps consume. But maybe this is not feasible or the way to do it and that's my wrong assumption from the beggining? But, if not, given that the domain layer defines the interfaces for repositories and the data layer implements them, how does the presentation layer (Android & iOS) know about the actual implementation? Struggling here with this.

Thank you for your time. Eager to know your thoughts! Sorry if it is confusing, struggling to identify both the concrete problem and the solution, here.


Solution

  • In the absence of a proper answer by someone who actually knows Kotlin and KMP...

    Regarding:

    I'm not sure if I can compile something for him to use in the NestJS back-end.

    Do a simple and completely standalone prototype, where you essentially build a steel-thread of the technical architecture, and see if you can answer that specific question.

    When using a new technology it's a good idea to do this anyway before you start on a "real" project, otherwise you'll be implementing technical debt and sub-optimal architecture all over the place.

    Options Analysis

    After the prototype (or sooner if you want) get together with your friend and do an options analysis exercise:

    An analysis process like this, even just an informal whiteboard discussion, is usually super helpful.

    Actual Options

    I barely know how to spell koltin, but from a generic sense you probably have at least these options - and you might be able to find variants on them that work for you:

    If in doubt, and pending better advice from someone else, I strongly recommend making a throwaway prototype first. You'll learn a lot.