<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Attila Szabó on Medium]]></title>
        <description><![CDATA[Stories by Attila Szabó on Medium]]></description>
        <link>https://medium.com/@atoka93?source=rss-34e7d6a06696------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Eky8A9HGukPcIVfvIqFCAQ.jpeg</url>
            <title>Stories by Attila Szabó on Medium</title>
            <link>https://medium.com/@atoka93?source=rss-34e7d6a06696------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 05:43:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@atoka93/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Combining Redux and Clean Architecture  for Android in Kotlin]]></title>
            <link>https://medium.com/@atoka93/clean-redux-android-architecture-in-kotlin-eecbb9ba8cbe?source=rss-34e7d6a06696------2</link>
            <guid isPermaLink="false">https://medium.com/p/eecbb9ba8cbe</guid>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[redux]]></category>
            <category><![CDATA[clean]]></category>
            <category><![CDATA[android]]></category>
            <dc:creator><![CDATA[Attila Szabó]]></dc:creator>
            <pubDate>Sat, 09 Jun 2018 08:13:41 GMT</pubDate>
            <atom:updated>2018-06-13T21:00:53.416Z</atom:updated>
            <content:encoded><![CDATA[<h3>Combining Redux and Clean Architecture for Android in Kotlin</h3><p>Last year when we decided to rewrite our mobile applications at trivago we read up on different architectures and best practices.</p><p>Hannes Dorfmann’s article series about “<a href="http://hannesdorfmann.com/android/mosby3-mvi-1">Reactive Apps with Model-View-Intent</a>” caught my attention. The idea of having unidirectional data flow, an application state with immutable models was very appealing. Although the idea seemed simple, using it proved to be difficult for our use case.</p><p>For Android we based our architecture on the <a href="https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html">Clean Architecture</a>. You can read about some of our reasoning <a href="https://tech.trivago.com/2018/02/06/rewriting-the-trivago-android-app-challenges-and-lessons-learnt/">here</a>. <br>In March I started working on the iOS application that uses a <a href="https://redux.js.org/">Redux</a> port to Swift as the base of its architecture. At this point I knew very little about Redux. But while studying it I became really excited about it. Redux provides everything that I liked about the Model-View-Intent architecture but with a bit of simplicity on top.</p><p>This lead me to the following two ideas:</p><ul><li>Porting the original Redux implementation to Kotlin as a <strong>library</strong>.</li><li>Come up with an architecture for my personal projects that combines the Clean Architecture principles with Redux for Android in Kotlin.</li></ul><p>There are plenty of articles and implementations about similar libraries and architectures. But the ones I found were either not modular enough, not scalable enough or they were missing functionality that I wanted to have, in short: they did not fulfil my expectations. Also, to really understand the idea behind Redux, its advantages and limitations and to be able to make it as flexible as I wanted it to be, it made sense to implement it by myself.</p><h3><strong>Pros and Cons</strong></h3><p>There are plenty of advantages when it comes to Redux:</p><ul><li>The <strong>structure </strong>doubles as a protocol, “enforcing” the correct usage of the architecture.</li><li>Having <strong>one application state</strong> gets rid of the pain of handling screen orientation changes or sharing data between components and gives you many options when it comes to debugging: like thorough bug reporting and time-travel debugging.</li><li><strong>Unidirectional data flow</strong> and <strong>immutable models</strong> ensures one source of truth while decreasing bugs.</li><li>Thanks to how middleware work it is easy to implement a feature once and provide it to many components (e.g. logging or tracking).</li><li>Having <strong>a predictable system</strong>: since the reducer functions are pure functions it is easier to understand and to unit test.</li></ul><p>These coupled with the advantages of Clean Architecture’s principles, namely: independence of UI, database, framework or any external agency which result in a more easily testable code; make this a really powerful architecture.</p><p>This doesn’t mean that there aren’t any downsides, there is the bad:</p><ul><li>Since the middleware, the store and the whole application state is kept in memory it is not hard to see how this could potentially become too much.<br>It could be solved by dynamically injecting some of the middleware, making this the only reason why you would need a dependency injection framework.</li><li>Having immutable models means that it has to be copied and/or recreated all the time.</li></ul><p>and the ugly:</p><ul><li>Handling process death becomes tricky. Even though you have an application state that you can save, it will probably not fit into the Bundle provided by the <em>onSaveInstanceState()</em> method. There are a couple of ways to deal with this: The easy way out is to not handle process death. The other way is to save a potentially stripped down version of the state (stripped from transient state) to internal storage. But this solution comes with its own issues: The state needs to be saved on every Activity’s <em>onSaveInstanceState()</em> (because you do not know which activity is the last one). Also, it should be saved asynchronously without the task being terminated during the process.</li><li>There is a steep learning curve to Redux.</li></ul><h3><strong>The library</strong></h3><p>I chose to implement the library in an <strong>object-oriented</strong> way, even though the original Redux implementation is not. I find it easier to understand this way.</p><p>In order to make the library more modular I split it into four modules:</p><ul><li><strong><em>redux-kotlin</em></strong> is the base module, it contains the basic functionality of Redux. This is a <strong>pure Kotlin module</strong> to make it usable not only in Android but any other project.</li><li><strong><em>redux-kotlin-base-implementation-java</em></strong> contains basic implementations of a store and an asynchronous version of the store. At this time the <a href="https://kotlinlang.org/docs/reference/coroutines.html">Kotlin Coroutines</a> are still experimental so I used a couple of Java classes in the implementation, hence the name. It already includes the base module as a dependency.</li><li><strong><em>redux-kotlin-base-implementation-android</em></strong> is for Android extensions. Currently it only contains one extension for ActivityLifecycleCallbacks which provides a nice interface for you to handle state persistence in case of process death. This module includes the previous module.</li><li><strong><em>redux-kotlin-rx-extensions</em></strong> contains extensions to enable RxJava2 usage. This module does not include but requires the base module.</li></ul><p>You can take a look at the code <a href="https://github.com/atoka93/redux-kotlin/tree/18e6baccde1cd7eeb392c1161d178c5bab8c441d"><strong>here</strong></a>.</p><h3><strong>The Architecture</strong></h3><p>It is showcased in a <a href="https://github.com/atoka93/clean-redux-demo"><strong>sample application</strong></a>. Based on Clean Architecture’s principles the application is split into five layers:</p><ul><li><strong><em>state</em></strong>: contains the whole state of the application. It is its own layer to minimize the dependencies between the layers.</li><li><strong><em>domain</em></strong>: is the business logic: the store, actions, reducers and middleware live here.</li><li><strong><em>data</em></strong>: provides the data for domain.</li><li><strong><em>data-sources</em></strong>: contains the implementation of the data sources such as databases or API calls.</li><li><strong><em>app</em></strong>: this is the presentation layer, it contains presenters/viewmodels (however you would like to call them), the views and the application class which holds the instance of the store.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/884/1*kX0dRjCsYxdUEtuh-iJ16A.png" /><figcaption>the dependency graph</figcaption></figure><p>The sample application includes only one mapper in the<em> data-sources</em> layer, this will map directly to the state which will ultimately be displayed. This is why the <em>data</em> layer needs to contain the state and <em>data-sources </em>contains the <em>data </em>layer. I am aware that one could and in some cases should map between every layer, but since it results in a lot of boilerplate I only map when and where it makes sense. The <em>app</em> layer relies on <em>data-sources</em> to instantiate the sources and inject them into the <em>domain </em>layer.</p><h3><strong>Conclusion</strong></h3><p>At I/O 2018 Google reinforced <a href="https://developer.android.com/topic/libraries/architecture/">Android Architecture Components</a>. That makes coming up with different architectures an uphill battle, so there might come a time when I will embrace the Architecture Components, specifically: Lifecycles, LiveData and ViewModel. But for now whenever it is possible and it makes sense I am sticking to this architecture because I like the advantages it provides.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=eecbb9ba8cbe" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>