Move your Network Layer to Kotlin Multiplatform; here is why

Arslan Rafique
Daresay
Published in
3 min readMar 23, 2023

DRY principle among cross platforms is not fictional anymore.

Imagine you want to send your birthday party invitation to your friends, would you choose to send it one by one or in a group chat? (off-course you only create a group of people you want to invite, doh! 🤓)

Approach 1; send message/invitation one by one:
14 friends, 14 messages, no brainer here. And if you friends have some questions, they ask you individually as well, and its important to take a note of that.

Approach 2; send message/invitation in a group:
14 friends, 3 messages, 12 friends are in group, 2 are not part of that group, so you send them individual invitations, do you see the point here? even with exceptions like your 2 friends, you are down to 3 messages from 14.

Now, if your friends have some questions, they ask in the group, and everyone seems the questions and their answers, one single source of truth here peeps.

I think by now you should have the idea. (if not, please ask me and I’ll try to come up with a different analogy.)

So, in above analogy, your Group is Kotlin Multiplatform, and your Friends are iOS and Android(at least, web could be there as well but not invited this time.)

Let’s see how the creators explain Kotlin Multiplatform:

Kotlin Multiplatform Mobile — Creator’s Perspective

—Why?

A world before KMP

Do you see the problem in above image? The Network Layer and Cache are redundant. A good bye to DRY!

When we say redundant, please do not forget that they are different platforms, and require different implementations of Network and Cache, hence twice the time is needed, which we can easily avoid now.

A world with KMP

Do you feel the beauty in the architecture above? not only it looks nice, but it is way more efficient by eliminating redundant Network Layer and Cache, eventually following DRY principle.

By now you should have deep understanding of the power of Kotlin Multiplatform.

Since this article’s focus is to move our Network Layer, we need a Kotlin Multiplatform Framework/Library, that we can share between iOS and Android. And at this point I believe I need different article for this with a complete how-to, so stay tuned for that.

— Create your first Cross-Platform Mobile App?

Since I at least mentioned DRY twice in this article, let me redirect you to the KMP video explaining how to create your first Cross Platfom Application:

I hope you enjoyed this throughout, I’ll write part 2 of this article soon, which will be covering “how” part.

Ciao!

--

--