ReduxKotlin.org

Earlier this year I began work on ReduxKotlin.org – a port of the Javascript library Redux. This series of posts will describe the what, why, how, and the vision of Redux in the Kotlin ecosystem. Also, I gave a talk title “ReduxKotlin.org: Redux for All Kotlin Platrms” at Droidcon NYC 2019. Here are the video and slides.

What is ReduxKotlin?

Redux is a state management library that started around 2015 for frontend web applications. Dan Abramov and Andrew Clark , facebook engineers, created Redux and to my understanding they wanted a better version of Flux.

The ‘state’ in ‘State management’ is global application state – all the data needed for the application. If your new to Redux I would recommend starting at ReduxKotlin.org and Redux.js.org.

ReduxKotlin is a port of Redux to Kotlin that supports multiplatform – all platforms that Kotlin supports. This includes JVM, Native (iOS, MacOS, Linux, Win, & WASM). One of the goals of the project is to spur development of an ecosystem of middleware, store enhancers, and developer tools. A couple, Thunk & Presenter-middleware, are available in the Github organization.

Why port Redux to Kotlin?

Being a simple and basic building block for frontend architectures, Redux can be used on any platform. With multiplatform Kotlin, frontends can be written for Android, iOS, web, and desktop. By porting the JS Redux library and keeping the same api, the same patterns can be used, and code can be shared thanks between these frontends.

Why use Redux on mobile?

Android & iOS already have recommended and established architectures. Why not use MVVM, MVP, MVC, or some other MVx architecture?

3 reasons:

  1. We can do better
  2. Multiplatform
  3. Jetpack Compose & SwiftUI

We can do better

Android development in particular can be difficult when state is placed into components that are tied to the Activity/Fragment/View lifecycle. Saving/restoring state when every view components begin/end their lifecycle is a chore and source of bugs. Android development in general has become bloated, slow, and cumbersome. Overly complex architectures combined with slow compilation due to abundance of annotation processors make development slow and painful. Don’t get me started on the madness that is Dagger and RxJava…

Additionally, most MVx architectures do not address where to store state. As a result, often there is some in the Model, some in the Presenter, and some in the view. Typically this ends up with a ‘state soup’ that can be difficult to debug and reason about.

Redux provides a pipeline of all actions in the app. These actions can trigger side effects and/or change the state. Middleware provides a clean separation of concern, and can be triggered on any action. Dependency injection is also simplified because UI components only need 2 functions to complete any task: dispatch & subscribe from the store.

Redux alone is not a silver bullet. And it is not a complete solution. It must be used with proper patterns for the given app. I believe over time some very nice patterns, tooling, and ecosystem can grow around multiplatform Redux.

Multiplatform

Kotlin Multiplatform projects are here and allow sharing code between iOS, Android, web, and backend. Redux can be used for an architecture that shares a lot of code between platforms. In my opinion all mobile developers should be thinking about multiplatform. Especially if you’re starting a greenfield project. Native mobile development is exspensive and writing the same app 2 or 3 times is extremely expensive. Web based solutions, Flutter, Xamarin, and others are getting a lot of attention, however they have some serious drawbacks (not having native look/feel, performance, memory/binary size, completely new language & tooling).

Kotlin Multiplatform on the other hand allows fully native UIs for each platform while sharing logic, networking, state management, & models. It is a sweet spot for multiplatform development. It is going to be a game changer for mobile development.

Also consider the best practice for Android architecture currently. If you use Android architecture components (ViewModels, Lifecycle aware components, etc) you will be tied to platform specific dependencies. Yes, there are probably a way to write an adapter layer, however I think there will always be an additional amount of Android specific code you will have to write if you adopt those libraries.

Jetpack Compose & SwiftUI

These new declarative UI frameworks will radically change the way UI code is written on Android and iOS. MVx architecture may not make much sense in this paradigm. Jetpack Compose & SwiftUI are similar in philosophy to React for the web. Redux can be used effectively in a declarative style, as proven by Redux + React on the web. This opens some new and interesting patterns for sharing code between platforms using Redux. An early look at how ReduxKotlin + SwiftUI & Jetpack Compose can be seen in the app MovieSwiftUI-Kotlin.

MovieSwiftUI-Kotlin on iOS – networking, reducers, store, & models in pure kotlin

Why write another redux library in Kotlin?

There is currently not a Redux library that puts multiplatform support as a top priority. In addition many of the other redux libraries have other functionality built into the library, adding opinion to the library. ReduxKotlin aims to be a minimal implementation on which other libraries and middleware can be built upon. The core ReduxKotlin library will remain small and nearly identical to the JS redux library.

Ready for use now

ReduxKotlin is ready for use right now. It has documentation available at ReduxKotlin.org and a few samples. Note that you will not find a detailed prescription on how to structure your apps. As such, if you’re new to development (mobile development in particular) then ReduxKotlin may not be for you. I plan to include more advanced, real world examples in the future.

Questions/Comments? Feel free to post here or Twitter, or follow the community links at ReduxKotlin.org.

Software Engineer, Husband, and father of 2 amazing kids. Android, iOS, Kotlin multiplatform!! Maintainer of ReduxKotlin.org