<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Padmakar Garg</title>
    <description>The latest articles on DEV Community by Padmakar Garg (@gargpadmakar).</description>
    <link>https://dev.to/gargpadmakar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4151500%2F22892962-94dc-474f-bac6-9bd9d530aa8a.jpg</url>
      <title>DEV Community: Padmakar Garg</title>
      <link>https://dev.to/gargpadmakar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gargpadmakar"/>
    <language>en</language>
    <item>
      <title>Senior Android Engineer Guide: 6 Topics, Diagrams and Interview Questions</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Tue, 01 Dec 2026 06:15:19 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/senior-android-engineer-guide-6-topics-diagrams-and-interview-questions-4bfk</link>
      <guid>https://dev.to/gargpadmakar/senior-android-engineer-guide-6-topics-diagrams-and-interview-questions-4bfk</guid>
      <description>&lt;p&gt;This guide covers six areas that senior Android interviews can probe. Each has a diagram, short explanations and questions with model answers.&lt;/p&gt;

&lt;p&gt;To answer well, use four steps: state the mechanism, name how it fails, give your choice and its trade-off, then say how you would measure it. Code samples are short sketches, not production code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Android fundamentals&lt;/li&gt;
&lt;li&gt;Kotlin concurrency&lt;/li&gt;
&lt;li&gt;Architecture and data&lt;/li&gt;
&lt;li&gt;Performance&lt;/li&gt;
&lt;li&gt;Security&lt;/li&gt;
&lt;li&gt;Production engineering&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  1. Android fundamentals
&lt;/h2&gt;

&lt;p&gt;Android can destroy your screens, or your whole process, at any time. Good apps treat memory as temporary and can rebuild the UI from saved state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; onCreate ---&amp;gt; onStart ---&amp;gt; onResume   (interactive)
    ^             ^           |  ^
    |             |           v  |
    |         onRestart      onPause   (focus lost)
    |             |              |
    |             |              v
    |             +---------- onStop   (not visible)
    |                          :   \
    |                          :    +--&amp;gt; onDestroy
    |                          v         (finish or recreation)
    |                   [process killed]
    |                          |  no further callbacks
    |                          v
    +------------ user returns: new process + saved Bundle
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  1.1 Activity lifecycle
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Callback&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;th&gt;Typical work&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onCreate&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Instance created&lt;/td&gt;
&lt;td&gt;Set content, restore state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onStart&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Visible&lt;/td&gt;
&lt;td&gt;Start UI observation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onResume&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Foreground and interactive&lt;/td&gt;
&lt;td&gt;Resume interaction-only work&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onPause&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Losing focus, may still be visible&lt;/td&gt;
&lt;td&gt;Keep it very short&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onStop&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;No longer visible&lt;/td&gt;
&lt;td&gt;Release UI resources, persist drafts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onDestroy&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Instance going away&lt;/td&gt;
&lt;td&gt;Clean up; not guaranteed to run&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;When A opens B: &lt;code&gt;A.onPause&lt;/code&gt; → &lt;code&gt;B.onCreate&lt;/code&gt; → &lt;code&gt;B.onStart&lt;/code&gt; → &lt;code&gt;B.onResume&lt;/code&gt; → &lt;code&gt;A.onStop&lt;/code&gt;. A translucent B leaves A without &lt;code&gt;onStop&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;A configuration change recreates the Activity by default. A &lt;code&gt;ViewModel&lt;/code&gt; survives it.&lt;/li&gt;
&lt;li&gt;In multi-window, a paused Activity can still be visible. Tie UI collection to &lt;code&gt;STARTED&lt;/code&gt;, not &lt;code&gt;RESUMED&lt;/code&gt;. Use &lt;code&gt;onTopResumedActivityChanged&lt;/code&gt; for exclusive resources.&lt;/li&gt;
&lt;li&gt;On large screens, apps targeting Android 17 &lt;a href="https://developer.android.com/about/versions/17/summary" rel="noopener noreferrer"&gt;can no longer opt out&lt;/a&gt; of ignored orientation and resizability limits. Design for resizing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1.2 Process death
&lt;/h3&gt;

&lt;p&gt;The system can kill a background process to reclaim memory, without calling &lt;code&gt;onDestroy&lt;/code&gt;. If the user returns to the task, Android creates a new process and restores the back stack.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;State holder&lt;/th&gt;
&lt;th&gt;Rotation&lt;/th&gt;
&lt;th&gt;System process death&lt;/th&gt;
&lt;th&gt;Use for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Plain fields, singletons&lt;/td&gt;
&lt;td&gt;Singletons only&lt;/td&gt;
&lt;td&gt;Lost&lt;/td&gt;
&lt;td&gt;Caches you can rebuild&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ViewModel&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Survives&lt;/td&gt;
&lt;td&gt;Lost&lt;/td&gt;
&lt;td&gt;Screen UI state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;SavedStateHandle&lt;/code&gt;, &lt;code&gt;rememberSaveable&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Survives&lt;/td&gt;
&lt;td&gt;Survives&lt;/td&gt;
&lt;td&gt;IDs, query text, small selections&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Room, DataStore, files&lt;/td&gt;
&lt;td&gt;Survives&lt;/td&gt;
&lt;td&gt;Survives&lt;/td&gt;
&lt;td&gt;Durable user data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Saved state is not durable storage: it is discarded when the user dismisses the task. It travels through Binder, whose buffer of about 1 MB is shared by the process, so save IDs rather than lists. To test, background the app, run &lt;code&gt;adb shell am kill &amp;lt;package&amp;gt;&lt;/code&gt;, then reopen it from Recents.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.3 Services and background work
&lt;/h3&gt;

&lt;p&gt;A service is a component, &lt;strong&gt;not a thread&lt;/strong&gt;. Its callbacks run on the main thread.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Need&lt;/th&gt;
&lt;th&gt;Choose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Work only while the screen is relevant&lt;/td&gt;
&lt;td&gt;Coroutine in &lt;code&gt;viewModelScope&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deferrable work that must survive restarts&lt;/td&gt;
&lt;td&gt;WorkManager (periodic minimum is 15 minutes)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ongoing work the user can perceive&lt;/td&gt;
&lt;td&gt;Foreground service with a declared type and notification&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Large transfer the user asked for&lt;/td&gt;
&lt;td&gt;User-initiated data transfer job (Android 14+)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API offered to another component or app&lt;/td&gt;
&lt;td&gt;Bound service&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Alarm at an exact user-chosen time&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;AlarmManager&lt;/code&gt; exact alarm, where permitted&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;START_NOT_STICKY&lt;/code&gt; does not recreate the service. &lt;code&gt;START_STICKY&lt;/code&gt; may recreate it with a null intent. &lt;code&gt;START_REDELIVER_INTENT&lt;/code&gt; redelivers the last intent.&lt;/li&gt;
&lt;li&gt;Android 12+ restricts starting foreground services from the background. Android 14+ requires a service type.&lt;/li&gt;
&lt;li&gt;For apps targeting Android 15+, &lt;code&gt;dataSync&lt;/code&gt; and &lt;code&gt;mediaProcessing&lt;/code&gt; services each get &lt;a href="https://developer.android.com/develop/background-work/services/fgs/timeout" rel="noopener noreferrer"&gt;6 hours in any 24&lt;/a&gt;; bringing the app to the foreground resets the timer. In &lt;code&gt;onTimeout&lt;/code&gt;, call &lt;code&gt;stopSelf()&lt;/code&gt; within seconds.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1.4 Threading
&lt;/h3&gt;

&lt;p&gt;The main thread runs a &lt;code&gt;Looper&lt;/code&gt; that takes messages from a &lt;code&gt;MessageQueue&lt;/code&gt; one at a time. Input, lifecycle callbacks and frame work all share it. One slow task delays everything behind it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At 60 Hz a frame has about 16.7 ms; at 120 Hz about 8.3 ms.&lt;/li&gt;
&lt;li&gt;Incoming Binder calls run on a thread pool, so a &lt;code&gt;ContentProvider&lt;/code&gt; or AIDL service must be thread-safe.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;StrictMode&lt;/code&gt; in debug builds to catch disk and network access on the main thread.&lt;/li&gt;
&lt;li&gt;Apps targeting Android 17 get a &lt;a href="https://developer.android.com/about/versions/17/summary" rel="noopener noreferrer"&gt;lock-free MessageQueue&lt;/a&gt;. Check libraries that reflect on its private internals.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1.5 Memory management
&lt;/h3&gt;

&lt;p&gt;ART garbage collection frees objects that are unreachable from GC roots. A &lt;strong&gt;leak&lt;/strong&gt; is an object you no longer need that is still reachable.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Common leaks: an Activity held by a singleton, unremoved listeners, delayed &lt;code&gt;Handler&lt;/code&gt; messages, a Fragment keeping its view binding after &lt;code&gt;onDestroyView&lt;/code&gt;, a &lt;code&gt;WebView&lt;/code&gt; not destroyed.&lt;/li&gt;
&lt;li&gt;Churn (many short-lived allocations) causes GC pressure. Bitmaps are often the largest cost: decode at display size and use an image loader.&lt;/li&gt;
&lt;li&gt;Respond to &lt;code&gt;onTrimMemory&lt;/code&gt;. From Android 14, rely on &lt;code&gt;TRIM_MEMORY_UI_HIDDEN&lt;/code&gt; and &lt;code&gt;TRIM_MEMORY_BACKGROUND&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The low-memory killer removes less important processes first: cached before service, visible and foreground.&lt;/li&gt;
&lt;li&gt;Android 17 adds &lt;a href="https://developer.android.com/about/versions/17/summary" rel="noopener noreferrer"&gt;RAM-based app memory limits&lt;/a&gt;. The &lt;a href="https://developer.android.com/about/versions/17/release-notes" rel="noopener noreferrer"&gt;release notes&lt;/a&gt; say such kills show &lt;code&gt;MemoryLimiter&lt;/code&gt; in &lt;code&gt;ApplicationExitInfo.getDescription&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Tools: LeakCanary, Memory Profiler, heap dumps and &lt;code&gt;ApplicationExitInfo&lt;/code&gt; (Android 11+).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Interview questions: fundamentals
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Q1. A user fills a long form, takes a call, returns, and it is empty. Rotation works. Why?&lt;/strong&gt;&lt;br&gt;
The state lives only in a &lt;code&gt;ViewModel&lt;/code&gt;, so it survives rotation but not process death. Put small keys in &lt;code&gt;SavedStateHandle&lt;/code&gt; and autosave the draft to Room. Reproduce it with &lt;code&gt;am kill&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q2. Is &lt;code&gt;onDestroy&lt;/code&gt; a safe place to save data?&lt;/strong&gt;&lt;br&gt;
No. A killed process gets no callback. Save as the user edits, or by &lt;code&gt;onStop&lt;/code&gt; at the latest.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q3. You must upload a 500 MB video that the user just selected. What do you use?&lt;/strong&gt;&lt;br&gt;
On Android 14+, a user-initiated data transfer job. On older versions, WorkManager with foreground execution. Make the upload chunked and resumable with a server upload ID, so a restart does not begin again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q4. Heap grows after each visit to one screen. How do you debug it?&lt;/strong&gt;&lt;br&gt;
Open and close it several times, force GC and take a heap dump. Find destroyed instances still retained, then follow the shortest path to a GC root. Fix ownership; a &lt;code&gt;WeakReference&lt;/code&gt; often only hides the bug.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q5. What is the difference between a leak, churn and an OOM?&lt;/strong&gt;&lt;br&gt;
A leak retains unneeded objects. Churn allocates and frees too fast, causing GC work. An OOM is an allocation that cannot be satisfied; a leak is one cause, a single huge bitmap is another.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q6. Why can a &lt;code&gt;BroadcastReceiver&lt;/code&gt; lose work?&lt;/strong&gt;&lt;br&gt;
The process can lose importance once &lt;code&gt;onReceive&lt;/code&gt; returns. &lt;code&gt;goAsync&lt;/code&gt; buys a little time but stays within the broadcast timeout. Enqueue WorkManager for anything longer.&lt;/p&gt;


&lt;h2&gt;
  
  
  2. Kotlin concurrency
&lt;/h2&gt;

&lt;p&gt;Every coroutine should have an owner that can cancel it, wait for it and see its failure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;              viewModelScope (SupervisorJob)
               |                        |
     launch: load screen        launch: refresh badge
     (coroutineScope)           (unaffected if load fails)
        |            |
 async: profile   async: feed
 (throws)         (gets cancelled)

 Clearing the ViewModel cancels everything.
 Handle the error in load: an uncaught one can still crash the app.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.1 Coroutines and dispatchers
&lt;/h3&gt;

&lt;p&gt;A coroutine is a computation that can suspend without blocking its thread. &lt;code&gt;suspend&lt;/code&gt; does not move work to another thread. A suspend function should be &lt;strong&gt;main-safe&lt;/strong&gt;: the function that does blocking work switches dispatcher itself.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dispatcher&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;th&gt;Note&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Main&lt;/code&gt; / &lt;code&gt;Main.immediate&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;UI work&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;immediate&lt;/code&gt; skips a re-dispatch if already on main&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Default&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;CPU work&lt;/td&gt;
&lt;td&gt;Sized around CPU cores&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;IO&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Blocking I/O&lt;/td&gt;
&lt;td&gt;Larger elastic pool; limit with &lt;code&gt;limitedParallelism&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  2.2 Structured concurrency
&lt;/h3&gt;

&lt;p&gt;A child inherits its parent's context. The parent completes only after its children. Cancelling a parent cancels them all.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scope&lt;/th&gt;
&lt;th&gt;Failure of one child&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;coroutineScope&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Cancels siblings and fails the scope&lt;/td&gt;
&lt;td&gt;All results are required&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;supervisorScope&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Siblings continue&lt;/td&gt;
&lt;td&gt;Independent results&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;launch&lt;/code&gt; propagates an uncaught exception at once. &lt;code&gt;async&lt;/code&gt; exposes it at &lt;code&gt;await&lt;/code&gt;, but a failing child &lt;code&gt;async&lt;/code&gt; still cancels a regular parent.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CoroutineExceptionHandler&lt;/code&gt; works for uncaught exceptions in root &lt;code&gt;launch&lt;/code&gt; coroutines, including direct children of a supervisor scope. It is not a recovery tool.&lt;/li&gt;
&lt;li&gt;Avoid &lt;code&gt;GlobalScope&lt;/code&gt;. Inject an application scope for work that must outlive a screen. It still dies with the process.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;loadHome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Home&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;supervisorScope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;profile&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;        &lt;span class="c1"&gt;// required&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;ads&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;                             &lt;span class="c1"&gt;// optional&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ads&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nc"&gt;Home&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;profile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ads&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.3 Cancellation
&lt;/h3&gt;

&lt;p&gt;Cancellation is &lt;strong&gt;cooperative&lt;/strong&gt;. Library suspend functions such as &lt;code&gt;delay&lt;/code&gt; check for it. CPU loops must call &lt;code&gt;ensureActive()&lt;/code&gt; or &lt;code&gt;yield()&lt;/code&gt;. Blocking calls need &lt;code&gt;runInterruptible&lt;/code&gt; or an API with its own cancel hook.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rethrow &lt;code&gt;CancellationException&lt;/code&gt;. Both &lt;code&gt;catch (e: Exception)&lt;/code&gt; and &lt;code&gt;runCatching&lt;/code&gt; can swallow it.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;withContext(NonCancellable)&lt;/code&gt; only for short suspending cleanup in &lt;code&gt;finally&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;withTimeout&lt;/code&gt; cancels cooperatively. It cannot undo a server request that already succeeded.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.4 Flow
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Behaviour&lt;/th&gt;
&lt;th&gt;Use&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;Flow&lt;/code&gt; (cold)&lt;/td&gt;
&lt;td&gt;Producer runs for each collector&lt;/td&gt;
&lt;td&gt;Queries, request pipelines&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;StateFlow&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Hot, has a current value, drops equal values, conflates&lt;/td&gt;
&lt;td&gt;UI state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;SharedFlow&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Hot, configurable replay and buffer&lt;/td&gt;
&lt;td&gt;Broadcasts to several collectors&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Channel&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Each element goes to one receiver&lt;/td&gt;
&lt;td&gt;Work queues&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;flowOn&lt;/code&gt; changes the upstream context only. &lt;code&gt;catch&lt;/code&gt; handles upstream errors only.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;buffer&lt;/code&gt; decouples producer and consumer. &lt;code&gt;conflate&lt;/code&gt; skips intermediate values. &lt;code&gt;collectLatest&lt;/code&gt; cancels the old block.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;flatMapLatest&lt;/code&gt; suits search. &lt;code&gt;flatMapConcat&lt;/code&gt; keeps order. &lt;code&gt;flatMapMerge&lt;/code&gt; runs concurrently.&lt;/li&gt;
&lt;li&gt;Wrap callback APIs with &lt;code&gt;callbackFlow&lt;/code&gt; and unregister in &lt;code&gt;awaitClose&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Collect with &lt;code&gt;collectAsStateWithLifecycle&lt;/code&gt; in Compose, or &lt;code&gt;repeatOnLifecycle&lt;/code&gt; in Views.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;SharedFlow&lt;/code&gt; or &lt;code&gt;Channel&lt;/code&gt; does not guarantee that a one-off UI event is handled. Model important outcomes as state that the UI acknowledges.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctUntilChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// unnecessary if query is already a StateFlow&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flatMapLatest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;q&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;repo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;q&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stateIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SharingStarted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WhileSubscribed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In production, return an error state instead of an empty list. If &lt;code&gt;query&lt;/code&gt; is a &lt;code&gt;StateFlow&lt;/code&gt;, remove the &lt;code&gt;distinctUntilChanged&lt;/code&gt; line.&lt;/p&gt;

&lt;h3&gt;
  
  
  2.5 Race conditions
&lt;/h3&gt;

&lt;p&gt;Races happen even on one thread, because other coroutines run while one is suspended. Typical cases are check-then-act, stale responses overwriting new ones, and two token refreshes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;th&gt;Fits&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;MutableStateFlow.update&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Atomic state change; the lambda may rerun, so no side effects&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;Mutex.withLock&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;A critical section that suspends; not reentrant&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;limitedParallelism(1)&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Confines non-suspending sections; others interleave at suspension points&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single consumer of a &lt;code&gt;Channel&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Strict serial processing&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database transaction or unique constraint&lt;/td&gt;
&lt;td&gt;Invariants on persisted data&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="k"&gt;lock&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mutex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;

&lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;lock&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withLock&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fetchConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;also&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;cached&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Interview questions: concurrency
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Q1. Does &lt;code&gt;viewModelScope.launch { api.pay() }&lt;/code&gt; guarantee payment completes?&lt;/strong&gt;&lt;br&gt;
No. Clearing the ViewModel cancels it, and the process can die. Cancellation also does not prove the server did nothing. Persist the intent, send an idempotency key and reconcile with the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q2. What is wrong with &lt;code&gt;runCatching&lt;/code&gt; around a suspend call?&lt;/strong&gt;&lt;br&gt;
It catches &lt;code&gt;CancellationException&lt;/code&gt;, so cancelled work may continue and look like an error. Catch expected exception types, or rethrow cancellation first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q3. &lt;code&gt;StateFlow&lt;/code&gt; or &lt;code&gt;SharedFlow&lt;/code&gt; for navigation after login?&lt;/strong&gt;&lt;br&gt;
Prefer state such as &lt;code&gt;loggedIn&lt;/code&gt;, which the UI reads and acts on. It survives collector restarts. A replay-0 &lt;code&gt;SharedFlow&lt;/code&gt; drops the event with no subscriber.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q4. How do you run 1,000 uploads with at most 4 in flight?&lt;/strong&gt;&lt;br&gt;
Use 4 workers reading a channel, or &lt;code&gt;flatMapMerge(concurrency = 4)&lt;/code&gt;. A &lt;code&gt;Semaphore(4)&lt;/code&gt; also limits calls, but still creates 1,000 coroutines. Persist the queue if it must survive process death.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q5. How do you test time-based Flow logic?&lt;/strong&gt;&lt;br&gt;
Use &lt;code&gt;runTest&lt;/code&gt;, inject dispatchers and advance virtual time. Collect hot flows in &lt;code&gt;backgroundScope&lt;/code&gt;. Avoid real sleeps.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q6. Does &lt;code&gt;Dispatchers.Main&lt;/code&gt; make code race-free?&lt;/strong&gt;&lt;br&gt;
It removes parallel access between main-thread blocks. It does not make a multi-step suspend operation atomic.&lt;/p&gt;


&lt;h2&gt;
  
  
  3. Architecture and data
&lt;/h2&gt;

&lt;p&gt;Give each piece of data one source of truth. State flows down to the UI and events flow back up.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; UI (Compose) &amp;lt;--&amp;gt; ViewModel &amp;lt;--&amp;gt; Repository
                                      ^
                                      |
                                      v
                          Room: SOURCE OF TRUTH
                          (entities, outbox, cursor)
                                      ^
                                      |
                                      v
                          Sync worker (WorkManager)
                                      ^
                  push: ops + opId    |    pull: changes after cursor
                                      v
                          Server API (versions, deduplication)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.1 MVVM
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;View&lt;/strong&gt; renders state and sends events. The &lt;strong&gt;ViewModel&lt;/strong&gt; turns data into an immutable UI state and handles those events. The &lt;strong&gt;Model&lt;/strong&gt; is the data and business logic. A ViewModel must not hold an Activity, a View or a UI &lt;code&gt;Context&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Model loading, content and errors so that impossible combinations cannot occur. A sealed type suits exclusive states. A data class suits screens that show cached content while refreshing. When strict event ordering matters, MVI adds a single reducer.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 Clean Architecture
&lt;/h3&gt;

&lt;p&gt;Source-code dependencies point toward business rules. The domain defines repository interfaces, and the data layer implements them.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Contains&lt;/th&gt;
&lt;th&gt;Depends on&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Presentation&lt;/td&gt;
&lt;td&gt;Composables, ViewModels, UI state&lt;/td&gt;
&lt;td&gt;Domain&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Domain&lt;/td&gt;
&lt;td&gt;Entities, use cases, repository interfaces&lt;/td&gt;
&lt;td&gt;Pure Kotlin only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Data&lt;/td&gt;
&lt;td&gt;Repository implementations, Room, network, mappers&lt;/td&gt;
&lt;td&gt;Domain&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This differs from Google's recommended layering, where the domain layer is optional and sits above the data layer. Add use cases when they hold real rules or are reused. One-line pass-through classes only add cost. In large apps, split by feature module and keep each public API small.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.3 Room
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;DAOs that return &lt;code&gt;Flow&lt;/code&gt; emit again when observed tables change. Unrelated row changes can trigger this, so use &lt;code&gt;distinctUntilChanged&lt;/code&gt; where useful.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;@Transaction&lt;/code&gt; or &lt;code&gt;withTransaction&lt;/code&gt; for writes that must succeed together. Keep network calls outside transactions.&lt;/li&gt;
&lt;li&gt;Add indexes for real filters and joins, then check with &lt;code&gt;EXPLAIN QUERY PLAN&lt;/code&gt;. Page large lists.&lt;/li&gt;
&lt;li&gt;Export schemas and test every supported upgrade path with &lt;code&gt;MigrationTestHelper&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Destructive migration deletes user data, including unsynced edits.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.4 Offline-first design
&lt;/h3&gt;

&lt;p&gt;The UI reads &lt;strong&gt;only&lt;/strong&gt; from the local database. The network updates the database. A user edit is saved locally first and shown with a pending status.&lt;/p&gt;

&lt;p&gt;Use the &lt;strong&gt;outbox pattern&lt;/strong&gt;: in one transaction, update the entity and insert an operation row. A worker sends the rows later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;rename&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withTransaction&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;notes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateTitle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SyncState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;outbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Op&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;opId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;noteId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;RENAME&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Then enqueue unique sync work. Also reconcile on app start,&lt;/span&gt;
&lt;span class="c1"&gt;// in case the process died between the commit and the enqueue.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.5 Synchronization
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Push:&lt;/strong&gt; send operations in order for each entity. Use the same &lt;code&gt;opId&lt;/code&gt; on every retry, and have the server deduplicate it (idempotency).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Pull:&lt;/strong&gt; request changes after a server-issued cursor. Apply the changes and the new cursor in one transaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deletes:&lt;/strong&gt; send tombstones, or other devices cannot learn that a row was removed. Define a full resync for expired cursors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Retries:&lt;/strong&gt; use exponential backoff with jitter for temporary errors. Do not retry permanent errors forever; surface them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Triggers:&lt;/strong&gt; WorkManager unique work with a network constraint, app foreground, and push messages as hints. Push delivery is not guaranteed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Race:&lt;/strong&gt; if new rows arrive while a worker is finishing, &lt;code&gt;KEEP&lt;/code&gt; can miss them. &lt;code&gt;APPEND_OR_REPLACE&lt;/code&gt; or a re-check closes the gap.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3.6 Conflict resolution
&lt;/h3&gt;

&lt;p&gt;A conflict occurs when two writers change the same base version. Detect it with a server version or ETag: the server rejects a stale write, for example with HTTP 409 or 412.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Strategy&lt;/th&gt;
&lt;th&gt;Good for&lt;/th&gt;
&lt;th&gt;Cost&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Last write wins&lt;/td&gt;
&lt;td&gt;Low-value settings&lt;/td&gt;
&lt;td&gt;Silently loses data; device clocks are unreliable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server wins&lt;/td&gt;
&lt;td&gt;Prices, stock, permissions&lt;/td&gt;
&lt;td&gt;Discards local intent unless you tell the user&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Field-level three-way merge&lt;/td&gt;
&lt;td&gt;Forms with separate fields&lt;/td&gt;
&lt;td&gt;Needs the base version; same-field clashes remain&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ask the user&lt;/td&gt;
&lt;td&gt;Valuable documents&lt;/td&gt;
&lt;td&gt;More UI and delay&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Domain operations&lt;/td&gt;
&lt;td&gt;Counters: send +1, not the total&lt;/td&gt;
&lt;td&gt;Server must apply and deduplicate operations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;CRDT&lt;/td&gt;
&lt;td&gt;Collaborative editing&lt;/td&gt;
&lt;td&gt;Complex, with metadata overhead&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Do not overwrite a row that has pending local edits with a pulled copy. Rebase the pending operations on the new server version.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interview questions: architecture and data
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Q1. Design an offline notes app.&lt;/strong&gt;&lt;br&gt;
Cover client-generated IDs, Room as source of truth, an outbox, cursor-based pull, tombstones and versions. Then state the conflict rule per field, the sync status the user sees, and what happens on logout with unsent edits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q2. The request succeeded but the response was lost. What happens?&lt;/strong&gt;&lt;br&gt;
The client retries with the same operation ID. The server returns the stored result instead of applying it again. WorkManager alone cannot give exactly-once effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q3. When is Clean Architecture too much?&lt;/strong&gt;&lt;br&gt;
For a small app with simple CRUD and one team. Keep UI and data separation, and add a domain layer when rules grow or are shared.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q4. Where do you map DTOs?&lt;/strong&gt;&lt;br&gt;
At the data boundary. Separate DTO, entity and domain models stop API or schema changes leaking into UI. Merge them only when the cost clearly outweighs the isolation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q5. How do you ship a risky schema migration?&lt;/strong&gt;&lt;br&gt;
Test from every old version with realistic data. Prefer additive changes. Roll out in stages and watch crashes. Plan to fix forward, because an installed schema cannot simply be rolled back.&lt;/p&gt;


&lt;h2&gt;
  
  
  4. Performance
&lt;/h2&gt;

&lt;p&gt;Measure on a real, lower-end device with a release-like build, change one thing, then measure again. Debug builds give misleading timings.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; MAIN THREAD (a Compose frame)
 input/animation -&amp;gt; composition -&amp;gt; layout   -&amp;gt; drawing
                    (what)         (where)     (record commands)
                                                    |
                                                    v
 SurfaceFlinger + display &amp;lt;------------ RenderThread + GPU

 Frame interval: ~16.7 ms at 60 Hz, ~8.3 ms at 120 Hz
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4.1 Profilers and tools
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Question&lt;/th&gt;
&lt;th&gt;Tool&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Where is CPU time going?&lt;/td&gt;
&lt;td&gt;Android Studio CPU profiler&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;What retains memory?&lt;/td&gt;
&lt;td&gt;Memory Profiler, heap dump, LeakCanary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why was this frame or startup slow?&lt;/td&gt;
&lt;td&gt;Perfetto system trace&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Did my change help, repeatably?&lt;/td&gt;
&lt;td&gt;Macrobenchmark (user journeys), Microbenchmark (hot code)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Is it happening to real users?&lt;/td&gt;
&lt;td&gt;Android vitals, JankStats, your own telemetry&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Why does Compose recompose?&lt;/td&gt;
&lt;td&gt;Layout Inspector recomposition counts, compiler reports&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  4.2 Perfetto
&lt;/h3&gt;

&lt;p&gt;Perfetto shows threads, CPU scheduling, frames, Binder calls and custom sections on one timeline. Add sections with &lt;code&gt;androidx.tracing&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Find the slow frame or startup window.&lt;/li&gt;
&lt;li&gt;Inspect the main thread and RenderThread.&lt;/li&gt;
&lt;li&gt;Check each thread state. &lt;strong&gt;Running&lt;/strong&gt; means executing code. &lt;strong&gt;Runnable&lt;/strong&gt; means waiting for a CPU. &lt;strong&gt;Sleeping&lt;/strong&gt; often means waiting on a lock, Binder or another thread. &lt;strong&gt;Uninterruptible sleep&lt;/strong&gt; often points to I/O.&lt;/li&gt;
&lt;li&gt;Follow the wake-up or Binder reply to the real blocker.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In PerfettoSQL, query the trace to compare many slices or runs.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.3 ANRs
&lt;/h3&gt;

&lt;p&gt;Default timeouts below are for AOSP and Pixel devices and &lt;a href="https://developer.android.com/topic/performance/anrs/diagnose-and-fix-anrs" rel="noopener noreferrer"&gt;can vary by manufacturer&lt;/a&gt;. Broadcast figures are for Android 14+; the longer end applies to CPU-starved processes.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;ANR type&lt;/th&gt;
&lt;th&gt;Timeout&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Input dispatch, including no focused window&lt;/td&gt;
&lt;td&gt;5 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Broadcast, foreground priority&lt;/td&gt;
&lt;td&gt;10 to 20 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Broadcast, background priority&lt;/td&gt;
&lt;td&gt;60 to 120 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Executing service, foreground / background&lt;/td&gt;
&lt;td&gt;20 s / 200 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Common causes are main-thread I/O, lock contention, deadlock, slow synchronous Binder calls, heavy startup work and CPU starvation. Read &lt;strong&gt;all&lt;/strong&gt; thread stacks. If the main thread waits on a lock, find its holder. An idle &lt;code&gt;nativePollOnce&lt;/code&gt; stack may mean the dump missed the real problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.4 Startup
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;th&gt;Starting point&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Cold&lt;/td&gt;
&lt;td&gt;No process: create process, &lt;code&gt;Application&lt;/code&gt;, Activity, first frame&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Warm&lt;/td&gt;
&lt;td&gt;Part of the work is reused, for example the process lives but the Activity is recreated&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hot&lt;/td&gt;
&lt;td&gt;Existing Activity is brought to the foreground&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TTID&lt;/strong&gt; is time to the first frame. &lt;strong&gt;TTFD&lt;/strong&gt; is time until content is usable; report it with &lt;code&gt;reportFullyDrawn&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Check &lt;code&gt;ContentProvider&lt;/code&gt; initializers from libraries, &lt;code&gt;Application.onCreate&lt;/code&gt;, dependency graph creation and disk reads.&lt;/li&gt;
&lt;li&gt;Defer or lazy-load work. Moving it to a background thread does not help if the first frame waits for it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Baseline Profiles&lt;/strong&gt; compile important paths ahead of time. &lt;strong&gt;Startup Profiles&lt;/strong&gt; improve DEX layout. Use R8 and verify with Macrobenchmark.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4.5 Rendering
&lt;/h3&gt;

&lt;p&gt;A frame goes through input, animation, measure, layout and draw on the main thread. RenderThread then issues GPU work and SurfaceFlinger composes the screen. &lt;strong&gt;Jank&lt;/strong&gt; is a frame that misses its deadline. Android vitals also treats frames over 700 ms as frozen. Look for deep or repeated layout, large bitmaps, overdraw and allocations during drawing.&lt;/p&gt;

&lt;h3&gt;
  
  
  4.6 Compose performance
&lt;/h3&gt;

&lt;p&gt;Compose works in three phases: &lt;strong&gt;composition&lt;/strong&gt; (what to show), &lt;strong&gt;layout&lt;/strong&gt; (where) and &lt;strong&gt;drawing&lt;/strong&gt; (how). A state read invalidates from the phase where it happens, so read fast-changing state as late as possible.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Reads in composition: recomposes on every scroll change&lt;/span&gt;
&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;scroll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;// Reads in layout (placement): skips recomposition&lt;/span&gt;
&lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;offset&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;IntOffset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scroll&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snippet shows read timing only; the two use different units.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Give lazy list items stable, unique &lt;code&gt;key&lt;/code&gt;s, and a &lt;code&gt;contentType&lt;/code&gt; for mixed lists.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;remember&lt;/code&gt; for expensive calculations. Use &lt;code&gt;derivedStateOf&lt;/code&gt; when inputs change more often than the result.&lt;/li&gt;
&lt;li&gt;Strong skipping is on by default since Kotlin 2.0.20. Unstable parameters are compared by instance, stable ones by &lt;code&gt;equals&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Do not mutate a list in place. Use immutable data and produce a new value.&lt;/li&gt;
&lt;li&gt;Avoid backwards writes: changing state after it was read in the same composition.&lt;/li&gt;
&lt;li&gt;Profile before adding &lt;code&gt;@Stable&lt;/code&gt; or &lt;code&gt;@Immutable&lt;/code&gt;. They are promises the compiler trusts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Interview questions: performance
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Q1. Cold start regressed after a release. How do you investigate?&lt;/strong&gt;&lt;br&gt;
Confirm it in field data by version and device. Reproduce with Macrobenchmark, compare Perfetto traces and diff startup work, including new SDK initializers. Fix the largest blocker and add a regression check.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q2. ANRs occur only on low-end devices, with no clear app frame.&lt;/strong&gt;&lt;br&gt;
Group by ANR type, device and OS. Look for CPU starvation, memory pressure, I/O and Binder waits. Add trace sections and breadcrumbs, then reproduce under load.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q3. A list stutters while scrolling.&lt;/strong&gt;&lt;br&gt;
Measure a release build. Separate composition, image decoding and main-thread work in a trace. Check keys, unstable data, sorting inside composables and oversized images.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q4. Why can an average hide a problem?&lt;/strong&gt;&lt;br&gt;
A few slow devices or sessions disappear in it. Track P50, P90 and P99, split by device class and app version.&lt;/p&gt;


&lt;h2&gt;
  
  
  5. Security
&lt;/h2&gt;

&lt;p&gt;Treat the app and device as an environment an attacker may control. The server must enforce authorization, and no secret shipped in the app stays secret.&lt;/p&gt;

&lt;p&gt;Authorization code flow with PKCE:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; App             Browser tab         Auth server      Resource API
  |--1. login + challenge--&amp;gt;|                |                |
  |                |--2. authorize----------&amp;gt;|                |
  |                |&amp;lt;-3. sign-in, code-------|                |
  |&amp;lt;-4. redirect---|                         |                |
  |--5. code + code_verifier----------------&amp;gt;|                |
  |&amp;lt;-6. verifier matches: tokens-------------|                |
  |--7. API call with access token---------------------------&amp;gt;|
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5.1 Android Keystore
&lt;/h3&gt;

&lt;p&gt;Keystore performs operations with keys while keeping key material out of your app process. Keys can be backed by a TEE or, on some devices, StrongBox. Check the actual security level instead of assuming it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keystore protects &lt;strong&gt;keys&lt;/strong&gt;, not data. Encrypt data with AES-GCM and store the ciphertext and IV. Never reuse an IV with the same key; let the cipher generate it.&lt;/li&gt;
&lt;li&gt;A compromised app process may be unable to extract a key but can still &lt;strong&gt;use&lt;/strong&gt; it.&lt;/li&gt;
&lt;li&gt;For sensitive actions, require user authentication and bind &lt;code&gt;BiometricPrompt&lt;/code&gt; to a &lt;code&gt;CryptoObject&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Handle lost or invalidated keys. A new biometric enrollment can invalidate some keys. Keystore keys do not move with backups, so restored ciphertext may be unreadable. Re-authenticate.&lt;/li&gt;
&lt;li&gt;All APIs in Jetpack &lt;code&gt;security-crypto&lt;/code&gt;, including &lt;code&gt;EncryptedSharedPreferences&lt;/code&gt;, are &lt;a href="https://developer.android.com/privacy-and-security/cryptography" rel="noopener noreferrer"&gt;deprecated&lt;/a&gt;. For new code, one option is Keystore directly, or a maintained library such as Tink, with DataStore.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5.2 OAuth 2.0 and OpenID Connect
&lt;/h3&gt;

&lt;p&gt;OAuth 2.0 grants &lt;strong&gt;access&lt;/strong&gt; to APIs. OIDC adds &lt;strong&gt;authentication&lt;/strong&gt; through an ID token. A mobile app is a public client.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use the authorization code flow with PKCE in the system browser or a Custom Tab, not an embedded WebView. Do not rely on a client secret.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://datatracker.ietf.org/doc/html/rfc9700" rel="noopener noreferrer"&gt;RFC 9700&lt;/a&gt; requires PKCE for public clients. &lt;code&gt;S256&lt;/code&gt; is the method to use. The password grant must not be used, and the implicit grant should not be.&lt;/li&gt;
&lt;li&gt;Prefer verified App Links for the redirect. Validate &lt;code&gt;state&lt;/code&gt;, and for OIDC the &lt;code&gt;nonce&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Validate the ID token's signature, issuer, audience and expiry. A backend that creates a session must validate it itself.&lt;/li&gt;
&lt;li&gt;For native sign-in with passkeys, passwords or federated identity, look at Credential Manager.&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Token&lt;/th&gt;
&lt;th&gt;Audience&lt;/th&gt;
&lt;th&gt;Rule&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Access token&lt;/td&gt;
&lt;td&gt;Resource API&lt;/td&gt;
&lt;td&gt;Short-lived; the client should not depend on its contents&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Refresh token&lt;/td&gt;
&lt;td&gt;Authorization server&lt;/td&gt;
&lt;td&gt;Protect strongly; never send to resource APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ID token&lt;/td&gt;
&lt;td&gt;Client app&lt;/td&gt;
&lt;td&gt;Proof of sign-in; do not use it to call APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  5.3 Token lifecycle
&lt;/h3&gt;

&lt;p&gt;The lifecycle is issue, store, use, refresh, revoke and delete. For public clients, RFC 9700 requires refresh tokens to be &lt;a href="https://datatracker.ietf.org/doc/html/rfc9700" rel="noopener noreferrer"&gt;sender-constrained or rotated&lt;/a&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Allow &lt;strong&gt;one refresh at a time&lt;/strong&gt;. Parallel refreshes with rotation can look like token theft and end the session.&lt;/li&gt;
&lt;li&gt;Inside the lock, check whether another caller already refreshed.&lt;/li&gt;
&lt;li&gt;Persist the new refresh token before using the new access token.&lt;/li&gt;
&lt;li&gt;Retry a failed request once. Log out on &lt;code&gt;invalid_grant&lt;/code&gt;, not on a network timeout or a server 5xx.&lt;/li&gt;
&lt;li&gt;On logout, cancel work, revoke tokens if supported, and clear local user data. Decide first what happens to unsent edits.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refreshIfStale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mutex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withLock&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;access&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;used&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt;@&lt;span class="n"&gt;withLock&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;access&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;fresh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;authApi&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;current&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;store&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fresh&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fresh&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;access&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With OkHttp, an &lt;code&gt;Authenticator&lt;/code&gt; handles 401 responses. It is a blocking API, so bridge to this function with care, use a separate client for refresh calls and cap retries.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.4 Secure networking
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use TLS everywhere. Cleartext is disabled by default for apps targeting API 28+. Control exceptions in Network Security Config.&lt;/li&gt;
&lt;li&gt;Never install a trust-all &lt;code&gt;TrustManager&lt;/code&gt; or an allow-all hostname verifier.&lt;/li&gt;
&lt;li&gt;Pinning limits which keys are trusted, but a bad rotation can cut off installed apps. If your threat model needs it, add backup pins, an expiry and a rotation runbook.&lt;/li&gt;
&lt;li&gt;Keep tokens and personal data out of logs, URLs and crash reports.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  5.5 Threat modeling
&lt;/h3&gt;

&lt;p&gt;Ask four questions: What are we building? What can go wrong? What will we do? Did it work? &lt;strong&gt;STRIDE&lt;/strong&gt; helps with the second.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Threat&lt;/th&gt;
&lt;th&gt;Android example&lt;/th&gt;
&lt;th&gt;Mitigation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Spoofing&lt;/td&gt;
&lt;td&gt;Another app claims the redirect scheme&lt;/td&gt;
&lt;td&gt;PKCE, verified App Links&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tampering&lt;/td&gt;
&lt;td&gt;Modified client sends a lower price&lt;/td&gt;
&lt;td&gt;Server calculates and validates&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repudiation&lt;/td&gt;
&lt;td&gt;User disputes a payment&lt;/td&gt;
&lt;td&gt;Server audit trail&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Information disclosure&lt;/td&gt;
&lt;td&gt;Token written to logs&lt;/td&gt;
&lt;td&gt;Redaction, safe storage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Denial of service&lt;/td&gt;
&lt;td&gt;Malformed deep link crashes the app&lt;/td&gt;
&lt;td&gt;Validate all input&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Elevation of privilege&lt;/td&gt;
&lt;td&gt;Exported component runs an internal action&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;exported=false&lt;/code&gt;, permissions, caller checks&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Also review &lt;code&gt;PendingIntent&lt;/code&gt; mutability (immutable unless mutation is required), WebView JavaScript bridges, &lt;code&gt;FileProvider&lt;/code&gt; paths and third-party SDKs. Play Integrity gives a risk signal that your server verifies; it does not replace authorization.&lt;/p&gt;

&lt;h3&gt;
  
  
  Interview questions: security
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Q1. How do you hide an API secret in an APK?&lt;/strong&gt;&lt;br&gt;
You cannot. Obfuscation and NDK code only slow extraction. Keep privileged secrets on a backend that authenticates, authorizes and rate-limits.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q2. What attack does PKCE stop?&lt;/strong&gt;&lt;br&gt;
Someone who intercepts the authorization code cannot exchange it without the &lt;code&gt;code_verifier&lt;/code&gt;. It does not protect tokens already stolen from a compromised device.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q3. Ten requests get 401 together. What happens?&lt;/strong&gt;&lt;br&gt;
One refreshes under a lock; the others see the new token and reuse it. Each retries once. Only a confirmed invalid refresh token ends the session. Account for a lost rotation response, which depends on server policy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q4. Does root detection solve security?&lt;/strong&gt;&lt;br&gt;
No. It is a bypassable signal that can block legitimate users. Combine risk signals with server-side checks.&lt;/p&gt;


&lt;h2&gt;
  
  
  6. Production engineering
&lt;/h2&gt;

&lt;p&gt;Assume every release can fail. Limit how many users it reaches, detect problems quickly and keep a way to stop the damage.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; CI checks -&amp;gt; signed App Bundle -&amp;gt; testing tracks -&amp;gt; small rollout
     ^        (+ mapping file)                           |
     |                                                   v
     |                       expand in steps &amp;lt;--yes-- healthy?
     |                       (re-check each)             | no
     |                                                   v
     +---- fix forward (higher version code) &amp;lt;---- halt + mitigate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6.1 Crash reporting
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use a tool such as Firebase Crashlytics or Sentry next to Android vitals.&lt;/li&gt;
&lt;li&gt;Upload the R8 mapping file and native symbols for every build.&lt;/li&gt;
&lt;li&gt;Record app version, device, OS, flag states and breadcrumbs. Never record tokens or unnecessary personal data.&lt;/li&gt;
&lt;li&gt;Report meaningful non-fatal errors. A custom uncaught-exception handler must delegate to the previous one.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;ApplicationExitInfo&lt;/code&gt; to learn why earlier processes ended.&lt;/li&gt;
&lt;li&gt;Rank issues by affected users and journeys, not only event counts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.2 Android vitals
&lt;/h3&gt;

&lt;p&gt;Exceeding a bad behavior threshold can reduce your visibility on Google Play. These are the &lt;a href="https://developer.android.com/google/play/vitals" rel="noopener noreferrer"&gt;published thresholds&lt;/a&gt; for stability and wake locks, checked 1 December 2026.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Core vital&lt;/th&gt;
&lt;th&gt;Overall&lt;/th&gt;
&lt;th&gt;Per phone model&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;User-perceived crash rate&lt;/td&gt;
&lt;td&gt;1.09%&lt;/td&gt;
&lt;td&gt;8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User-perceived ANR rate&lt;/td&gt;
&lt;td&gt;0.47%&lt;/td&gt;
&lt;td&gt;8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Excessive partial wake locks&lt;/td&gt;
&lt;td&gt;5%&lt;/td&gt;
&lt;td&gt;No threshold listed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The same page lists &lt;strong&gt;memory usage&lt;/strong&gt; (anonymous RSS + swap) and &lt;strong&gt;bitmap memory&lt;/strong&gt; as core vitals for mobile apps. Memory limits depend on device RAM and app state. For apps, foreground memory on a 4 GB device is limited to 2.00 GB. Bitmap limits are 200 MB for background and user-perceived services, and 400 MB for cached. Store visibility effects for memory may start in &lt;strong&gt;February 2027&lt;/strong&gt;. The page also sets watch-specific thresholds and a code optimization check. Treat all thresholds as limits, not targets.&lt;/p&gt;

&lt;h3&gt;
  
  
  6.3 Staged rollouts
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Go from internal and closed testing to a small percentage, then increase in steps.&lt;/li&gt;
&lt;li&gt;Before starting, define the metrics, the halt limits, the observation time and the owner.&lt;/li&gt;
&lt;li&gt;Halting stops further distribution. It does &lt;strong&gt;not&lt;/strong&gt; downgrade users who already updated. Ship a fix with a higher version code.&lt;/li&gt;
&lt;li&gt;Percentage rollout applies to updates, not an app's first release.&lt;/li&gt;
&lt;li&gt;Compare the new version with the old one. A small sample may not show rare crashes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.4 Feature flags
&lt;/h3&gt;

&lt;p&gt;Flags separate &lt;strong&gt;deploying&lt;/strong&gt; code from &lt;strong&gt;releasing&lt;/strong&gt; a feature.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Ship safe defaults that work offline, and cache the last good values.&lt;/li&gt;
&lt;li&gt;Keep values stable during a user journey. Log which variant ran.&lt;/li&gt;
&lt;li&gt;A kill switch only works if the app fetches it and the flag is checked before the failing code.&lt;/li&gt;
&lt;li&gt;Flags are not authorization. Every flag needs an owner and a removal date.&lt;/li&gt;
&lt;li&gt;Keep database and API changes backward compatible while old versions remain installed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.5 CI/CD
&lt;/h3&gt;

&lt;p&gt;Each pull request should compile, run lint and static analysis, and run unit tests. Add instrumented, screenshot, migration and performance tests where risk justifies the time.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Build one signed App Bundle and promote that same artifact through tracks.&lt;/li&gt;
&lt;li&gt;Keep signing credentials in a secret manager, never in the repository or in jobs for untrusted pull requests. Use Play App Signing.&lt;/li&gt;
&lt;li&gt;Pin dependencies and scan them. Generate unique version codes.&lt;/li&gt;
&lt;li&gt;Test the R8 release build, because shrinking can break reflection or serialization.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  6.6 Incident response
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Detect&lt;/strong&gt; through an alert, vitals or support.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Triage&lt;/strong&gt; impact and severity. Name an incident lead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mitigate&lt;/strong&gt; first: kill switch, server change or halted rollout.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Communicate&lt;/strong&gt; at agreed intervals.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix and verify&lt;/strong&gt; with a staged release.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learn&lt;/strong&gt; through a blameless review with owners and due dates.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Interview questions: production
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Q1. Crashes double at a 5% rollout. What do you do?&lt;/strong&gt;&lt;br&gt;
Halt the rollout and confirm the signal by version and device. Disable the suspect feature if a flag covers it. Ship a higher-version hotfix, stage it and add the missing test or alert.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q2. The app crashes on launch before flags load. What now?&lt;/strong&gt;&lt;br&gt;
The remote switch may be unreachable. Halt the rollout, apply any compatible server-side mitigation and ship a fix. To prevent repeats, use safe cached defaults, delay risky initialization and consider crash-loop detection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q3. A backend change breaks old app versions.&lt;/strong&gt;&lt;br&gt;
Restore compatibility on the server first. Then use contract tests, API versioning and tolerant parsing. A forced update is a last resort.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q4. How do you decide to ship?&lt;/strong&gt;&lt;br&gt;
Use agreed gates: stability, ANRs, startup, key journey success and business guardrails. Name who can override a gate and how that risk is recorded.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Q5. Behavioural: describe an incident you handled.&lt;/strong&gt;&lt;br&gt;
Use your own example: impact, your decisions, mitigation, root cause and what changed afterwards. Do not invent one.&lt;/p&gt;




&lt;h2&gt;
  
  
  Final thought
&lt;/h2&gt;

&lt;p&gt;The same habits run through all six topics: assume the process can die, give every task an owner, keep one source of truth, measure before optimizing, trust the server for security decisions, and keep a way to stop a bad release.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Android platform rules and Google Play thresholds change. Check the linked official pages before relying on a specific number.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; Padmakar Garg&lt;br&gt;&lt;br&gt;
Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture, mobile security, and high-performance app development.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>interview</category>
      <category>mobile</category>
    </item>
    <item>
      <title>How to Build a Custom Android ROM and Kernel: A Practical Guide</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Tue, 01 Dec 2026 05:38:46 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/how-to-build-a-custom-android-rom-and-kernel-a-practical-guide-35o4</link>
      <guid>https://dev.to/gargpadmakar/how-to-build-a-custom-android-rom-and-kernel-a-practical-guide-35o4</guid>
      <description>&lt;h1&gt;
  
  
  How to Build a Custom Android ROM and Kernel
&lt;/h1&gt;

&lt;p&gt;Building your own Android is one of the best ways to learn how the platform works below the app layer. This guide covers the full path: the build machine, AOSP, a device ROM based on LineageOS, and a custom kernel.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning:&lt;/strong&gt; Flashing unofficial software can erase your data, stop the phone from booting, break features such as banking apps or contactless payments, and may affect your warranty. Use a spare device that you own. Commands and partition layouts differ between devices, so your exact model's instructions take priority over this guide.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  1. ROM vs kernel
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Part&lt;/th&gt;
&lt;th&gt;What it contains&lt;/th&gt;
&lt;th&gt;Typical changes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;ROM&lt;/td&gt;
&lt;td&gt;Android framework, system apps, services, device configuration&lt;/td&gt;
&lt;td&gt;Features, UI, bundled apps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Kernel&lt;/td&gt;
&lt;td&gt;Linux kernel plus Android changes&lt;/td&gt;
&lt;td&gt;Drivers, kernel configuration, debugging options&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vendor components&lt;/td&gt;
&lt;td&gt;Hardware-specific binaries and modules&lt;/td&gt;
&lt;td&gt;Usually taken from the device maker&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can build a ROM without modifying the kernel. Change one at a time, so that you know what caused a failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Check your device first
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Bootloader:&lt;/strong&gt; Can it be unlocked? Some manufacturers and carriers do not allow it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exact model and codename:&lt;/strong&gt; Similar-looking variants can need different builds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Device support:&lt;/strong&gt; Is there a maintained device tree, kernel source and set of vendor files? The LineageOS Wiki is a good place to check.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Recovery path:&lt;/strong&gt; Download the official factory or recovery firmware and learn how to restore it &lt;em&gt;before&lt;/em&gt; flashing anything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup:&lt;/strong&gt; Copy your data off the phone.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without device support, you are porting Android to new hardware. That is a much larger project than this guide covers.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Prepare the build machine
&lt;/h2&gt;

&lt;p&gt;For current AOSP, plan for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A 64-bit x86 Linux system (Ubuntu is a common choice)&lt;/li&gt;
&lt;li&gt;At least 400 GB of free disk space (about 250 GB for source, 150 GB for the build)&lt;/li&gt;
&lt;li&gt;64 GB of RAM&lt;/li&gt;
&lt;li&gt;A fast SSD and a reliable internet connection&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Install the packages listed by AOSP:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;sudo apt-get update
sudo apt-get install git-core gnupg flex bison build-essential zip curl \
  zlib1g-dev libc6-dev-i386 x11proto-core-dev libx11-dev lib32z1-dev \
  libgl1-mesa-dev libxml2-utils xsltproc unzip fontconfig repo
repo version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your distribution's &lt;code&gt;repo&lt;/code&gt; package is missing or outdated, use the manual install steps in the AOSP documentation. Set your Git identity:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git config --global user.name &lt;span class="s2"&gt;"Your Name"&lt;/span&gt;
git config --global user.email &lt;span class="s2"&gt;"you@example.com"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  4. Learn the flow with AOSP
&lt;/h2&gt;

&lt;p&gt;Start with a virtual device. It lets you practise without risking a phone.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkdir -p ~/aosp &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; ~/aosp
repo init --partial-clone --no-use-superproject \
  -b android-latest-release \
  -u https://android.googlesource.com/platform/manifest
repo sync -c -j8

&lt;span class="nb"&gt;source&lt;/span&gt; build/envsetup.sh
lunch aosp_cf_x86_64_only_phone-aosp_current-userdebug
m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;lunch&lt;/code&gt; target has three parts: product, release configuration and build variant. &lt;code&gt;userdebug&lt;/code&gt; is convenient for development. &lt;code&gt;user&lt;/code&gt; is the production-style variant.&lt;/p&gt;

&lt;p&gt;The target above is for &lt;strong&gt;Cuttlefish&lt;/strong&gt;, Android's virtual device. After installing the Cuttlefish host packages (see its documentation), launch it with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;launch_cvd --daemon
&lt;span class="c"&gt;# Open https://localhost:8443 in a browser&lt;/span&gt;
stop_cvd
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As of 2026, Google publishes AOSP source in Q2 and Q4. The &lt;code&gt;android-latest-release&lt;/code&gt; manifest points to the most recent release.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Build a ROM for a real phone (LineageOS)
&lt;/h2&gt;

&lt;p&gt;A generic AOSP checkout does not contain everything your phone needs. For a supported device, LineageOS is a practical starting point.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Open the build page for your device on the LineageOS Wiki.&lt;/strong&gt; Use its package list, branch and special steps. Below, replace &lt;code&gt;&amp;lt;BRANCH&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;CODENAME&amp;gt;&lt;/code&gt; with your values.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.1 Download the source
&lt;/h3&gt;

&lt;p&gt;Install the packages from your device's build page, including &lt;code&gt;git-lfs&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;git lfs install
mkdir -p ~/android/lineage &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; ~/android/lineage
repo init -u https://github.com/LineageOS/android.git -b &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;BRANCH&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; \
  --git-lfs --no-clone-bundle
repo sync
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5.2 Optional: enable ccache
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ccache&lt;/code&gt; speeds up repeat builds.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;USE_CCACHE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;1
&lt;span class="kd"&gt;export&lt;/span&gt; &lt;span class="nv"&gt;CCACHE_EXEC&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;/usr/bin/ccache
ccache -M 50G
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5.3 Prepare device code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;source&lt;/span&gt; build/envsetup.sh
breakfast &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;CODENAME&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This fetches the device configuration and kernel.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.4 Get proprietary vendor files
&lt;/h3&gt;

&lt;p&gt;Many devices need proprietary files ("blobs") for hardware such as the camera or modem. The usual method extracts them from a phone that already runs the matching LineageOS branch, connected with ADB and root enabled. From the device directory, run whichever script exists:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;./extract-files.py    &lt;span class="c"&gt;# or ./extract-files.sh&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Wiki also describes extraction from an installable zip. If &lt;code&gt;breakfast&lt;/code&gt; failed because vendor files were missing, run it again afterwards.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.5 Build
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;croot
brunch &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;CODENAME&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;cd&lt;/span&gt; &lt;span class="nv"&gt;$OUT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On success, look for a package named like &lt;code&gt;lineage-&amp;lt;version&amp;gt;-&amp;lt;date&amp;gt;-UNOFFICIAL-&amp;lt;CODENAME&amp;gt;.zip&lt;/code&gt;, along with device-specific images such as a recovery or boot image.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Customize the ROM
&lt;/h2&gt;

&lt;p&gt;Make one small change, build, and test.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Add or remove apps:&lt;/strong&gt; edit the product makefiles (&lt;code&gt;PRODUCT_PACKAGES&lt;/code&gt;). Removing an app may also require changing the makefile that includes it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Change default settings or resources:&lt;/strong&gt; use resource overlays.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Change framework behavior:&lt;/strong&gt; start in &lt;code&gt;frameworks/base&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep your work:&lt;/strong&gt; use a Git branch in each repository you edit. Use a local manifest for your own forks so that &lt;code&gt;repo sync&lt;/code&gt; knows about them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep SELinux &lt;strong&gt;enforcing&lt;/strong&gt;. If something is denied, read the denial in the logs and write a narrow policy instead of disabling SELinux.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Build a custom kernel
&lt;/h2&gt;

&lt;h3&gt;
  
  
  7.1 Understand GKI
&lt;/h3&gt;

&lt;p&gt;Android's &lt;strong&gt;Generic Kernel Image (GKI)&lt;/strong&gt;, introduced with Android 11, separates the Google-maintained core kernel from vendor modules. Vendor modules depend on the &lt;strong&gt;Kernel Module Interface (KMI)&lt;/strong&gt;. A change that breaks it can stop modules from loading, which can break display, touch, storage or boot.&lt;/p&gt;

&lt;p&gt;Not every phone uses GKI. Many devices need the manufacturer's kernel source and its own build instructions. On LineageOS, start with the kernel that &lt;code&gt;breakfast&lt;/code&gt; downloaded.&lt;/p&gt;

&lt;p&gt;First, record what your phone runs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb shell uname -r
adb shell getprop ro.product.device
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7.2 Download the Android Common Kernel
&lt;/h3&gt;

&lt;p&gt;Choose the branch that matches your device's Android release tag and kernel version. A matching version number alone does not guarantee compatibility. &lt;code&gt;common-android16-6.12&lt;/code&gt; is only an example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mkdir -p ~/android-kernel &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd&lt;/span&gt; ~/android-kernel
repo init -u https://android.googlesource.com/kernel/manifest \
  -b common-android16-6.12
repo sync -c -j8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7.3 Build with Kleaf
&lt;/h3&gt;

&lt;p&gt;Kleaf is the Bazel-based kernel build system, introduced with Android 13. The tools come with the kernel checkout.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;tools/bazel run //common:kernel_aarch64_dist -- --destdir=out/dist
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;build/build.sh&lt;/code&gt; is the legacy method for older branches. It is not supported on Android 14 and later.&lt;/p&gt;

&lt;h3&gt;
  
  
  7.4 Change the configuration
&lt;/h3&gt;

&lt;p&gt;Build the unmodified kernel first. Then put your changes in a &lt;strong&gt;defconfig fragment&lt;/strong&gt; instead of editing generated files. The exact Bazel setup differs between branches, so follow the Kleaf documentation in your checkout (&lt;code&gt;build/kernel/kleaf/docs/&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;A harmless first change is a custom version suffix or one debugging option. Leave overclocking and voltage changes alone: mistakes can cause instability or overheating.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Test and install
&lt;/h2&gt;

&lt;h3&gt;
  
  
  8.1 Unlock the bootloader
&lt;/h3&gt;

&lt;p&gt;Enable &lt;strong&gt;Developer options&lt;/strong&gt; and &lt;strong&gt;OEM unlocking&lt;/strong&gt;, then follow your manufacturer's procedure. On devices that support the standard command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb reboot bootloader
fastboot flashing unlock
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Unlocking factory-resets the device.&lt;/strong&gt; An unlocked bootloader also weakens the phone's security.&lt;/p&gt;

&lt;h3&gt;
  
  
  8.2 Try a kernel before flashing it
&lt;/h3&gt;

&lt;p&gt;Some devices can boot an image temporarily:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;adb reboot bootloader
fastboot boot &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;your-boot-image.img&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This does not permanently replace the kernel, and a normal reboot returns to the installed one. It is not a guarantee of safety: a bad kernel can still affect data. Not every bootloader supports the command. The image must match your device's boot format, and a raw kernel file may not be enough.&lt;/p&gt;

&lt;h3&gt;
  
  
  8.3 Install the ROM
&lt;/h3&gt;

&lt;p&gt;Follow the &lt;strong&gt;installation page for your exact codename&lt;/strong&gt;. Depending on the device, the steps may involve &lt;code&gt;boot&lt;/code&gt;, &lt;code&gt;init_boot&lt;/code&gt;, &lt;code&gt;vendor_boot&lt;/code&gt;, a recovery image or &lt;code&gt;adb sideload&lt;/code&gt;. Do not copy flash commands from another phone.&lt;/p&gt;

&lt;p&gt;Do &lt;strong&gt;not&lt;/strong&gt; relock the bootloader with custom software installed unless your device and signing setup explicitly support it. It can leave the phone unable to boot. Check anti-rollback rules before installing older firmware.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Debug failures
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Problem&lt;/th&gt;
&lt;th&gt;What to check&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;repo sync&lt;/code&gt; fails&lt;/td&gt;
&lt;td&gt;Network, disk space, the first error; retry with fewer jobs (&lt;code&gt;-j4&lt;/code&gt;)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Build runs out of memory&lt;/td&gt;
&lt;td&gt;Free RAM, swap, fewer parallel jobs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Build fails&lt;/td&gt;
&lt;td&gt;The &lt;strong&gt;first&lt;/strong&gt; error, not the final summary&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Missing vendor makefiles&lt;/td&gt;
&lt;td&gt;Blob extraction and device repositories&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Boot loop&lt;/td&gt;
&lt;td&gt;Restore the last working image; compare your latest change&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Wi-Fi, touch or camera broken&lt;/td&gt;
&lt;td&gt;Module loading, vendor files, kernel/ROM compatibility&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Useful commands:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;m 2&lt;span class="o"&gt;&amp;gt;&amp;amp;&lt;/span&gt;1 &lt;span class="o"&gt;|&lt;/span&gt; tee build.log          &lt;span class="c"&gt;# save an AOSP build log&lt;/span&gt;
adb logcat -b all &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; logcat.txt  &lt;span class="c"&gt;# when ADB is available&lt;/span&gt;
adb shell su -c dmesg           &lt;span class="c"&gt;# needs root; syntax varies&lt;/span&gt;
adb root &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; adb shell dmesg     &lt;span class="c"&gt;# userdebug builds that allow adb root&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Kernel crash logs may be available under &lt;code&gt;/sys/fs/pstore/&lt;/code&gt; after a reboot, if the device supports it.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Share builds responsibly
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Publish kernel source that matches your binaries, as required by the GPL.&lt;/li&gt;
&lt;li&gt;Check the licence of every component. Do not assume you may redistribute proprietary blobs or Google apps.&lt;/li&gt;
&lt;li&gt;Use your own private release keys for builds you distribute, and never publish them. Public test keys are not suitable.&lt;/li&gt;
&lt;li&gt;State the supported codename, known bugs, install steps and recovery steps.&lt;/li&gt;
&lt;li&gt;Keep up with security patches.&lt;/li&gt;
&lt;li&gt;Credit upstream developers.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Suggested learning path
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Build AOSP and run it on Cuttlefish.&lt;/li&gt;
&lt;li&gt;Make one visible framework or resource change.&lt;/li&gt;
&lt;li&gt;Build an unmodified ROM for a supported spare phone.&lt;/li&gt;
&lt;li&gt;Build the unmodified kernel.&lt;/li&gt;
&lt;li&gt;Make one small kernel change and test it.&lt;/li&gt;
&lt;li&gt;Learn signing, SELinux, updates and release maintenance.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Change one thing at a time, keep a way back, and read the first error.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;AOSP, Download the source: &lt;a href="https://source.android.com/docs/setup/download" rel="noopener noreferrer"&gt;https://source.android.com/docs/setup/download&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;AOSP, Build Android: &lt;a href="https://source.android.com/docs/setup/build/building" rel="noopener noreferrer"&gt;https://source.android.com/docs/setup/build/building&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;AOSP, Build kernels: &lt;a href="https://source.android.com/docs/setup/build/building-kernels" rel="noopener noreferrer"&gt;https://source.android.com/docs/setup/build/building-kernels&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;AOSP, Lock and unlock the bootloader: &lt;a href="https://source.android.com/docs/core/architecture/bootloader/locking_unlocking" rel="noopener noreferrer"&gt;https://source.android.com/docs/core/architecture/bootloader/locking_unlocking&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Kleaf documentation: &lt;a href="https://android.googlesource.com/kernel/build/+/refs/heads/main/kleaf/README.md" rel="noopener noreferrer"&gt;https://android.googlesource.com/kernel/build/+/refs/heads/main/kleaf/README.md&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;LineageOS Wiki: &lt;a href="https://wiki.lineageos.org/" rel="noopener noreferrer"&gt;https://wiki.lineageos.org/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; Padmakar Garg&lt;br&gt;&lt;br&gt;
Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture, mobile security, and high-performance app development.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kernel</category>
      <category>linux</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Beyond Coding: 15 Real-World Android Engineering Interview Questions with Answers</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Tue, 01 Dec 2026 04:55:52 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/beyond-coding-15-real-world-android-engineering-interview-questions-with-answers-19gc</link>
      <guid>https://dev.to/gargpadmakar/beyond-coding-15-real-world-android-engineering-interview-questions-with-answers-19gc</guid>
      <description>&lt;p&gt;Your app works perfectly in development. The test suite is green. The demo looks smooth.&lt;/p&gt;

&lt;p&gt;Then production tells a different story:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A small percentage of users experience crashes.&lt;/li&gt;
&lt;li&gt;The app becomes slower after staying open for a while.&lt;/li&gt;
&lt;li&gt;Two network responses race and show outdated information.&lt;/li&gt;
&lt;li&gt;Offline edits conflict with changes made on another device.&lt;/li&gt;
&lt;li&gt;A release introduces a critical bug.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These are not merely coding problems. They test how well you understand the Android platform, distributed systems, observability, lifecycle management, and release engineering.&lt;/p&gt;

&lt;p&gt;Below are practical scenarios, how I would investigate them, and the trade-offs I would explain in an interview.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Production debugging: The app crashes for 2% of users. How would you investigate?
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; The app works on your development device, but a small percentage of production users report crashes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would avoid guessing or immediately changing code. First, I would establish the scope and identify a reproducible pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Measure the impact&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Check crash-free users and crash-free sessions.&lt;/li&gt;
&lt;li&gt;Identify the affected app versions and release tracks.&lt;/li&gt;
&lt;li&gt;Group crashes by exception type, stack trace, device model, Android version, and app version.&lt;/li&gt;
&lt;li&gt;Check whether the issue affects a specific screen, user journey, account state, or network condition.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tools such as Firebase Crashlytics, Play Console Android vitals, and production telemetry can help. I would distinguish a crash rate from the percentage of users affected; they are not the same metric.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2: Find the first actionable stack trace&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Look for the root exception and the first relevant application frame. Check whether the crash is caused by:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A null or invalid state.&lt;/li&gt;
&lt;li&gt;An API response that differs from the expected schema.&lt;/li&gt;
&lt;li&gt;A lifecycle or threading issue.&lt;/li&gt;
&lt;li&gt;A device/API-level compatibility problem.&lt;/li&gt;
&lt;li&gt;A native library or SDK.&lt;/li&gt;
&lt;li&gt;Code shrinking, obfuscation, or missing keep rules in the release build.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For minified builds, verify that the correct mapping file is uploaded so obfuscated stack traces can be deobfuscated.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 3: Reproduce production conditions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use the affected Android versions and device classes where possible. Test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Fresh install versus upgrade.&lt;/li&gt;
&lt;li&gt;Slow, unavailable, or switching networks.&lt;/li&gt;
&lt;li&gt;Empty, malformed, and unexpectedly large API responses.&lt;/li&gt;
&lt;li&gt;Background/foreground transitions.&lt;/li&gt;
&lt;li&gt;Process death and state restoration.&lt;/li&gt;
&lt;li&gt;Different locales, font scales, and screen sizes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 4: Fix, verify, and monitor&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Create a regression test for the failure. Validate the fix against the affected configuration, then monitor the same crash signature after rollout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; A production bug is an investigation problem first and a coding problem second. Start with evidence, narrow the blast radius, reproduce, fix, and verify.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Memory leaks: Why can an Activity leak even after &lt;code&gt;onDestroy()&lt;/code&gt;?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;onDestroy()&lt;/code&gt; does not automatically free an Activity. Garbage collection can reclaim an object only when it is no longer strongly reachable from a GC root.&lt;/p&gt;

&lt;p&gt;An Activity can therefore remain in memory after its lifecycle has ended if another long-lived object still references it.&lt;/p&gt;

&lt;p&gt;Common causes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;code&gt;companion object&lt;/code&gt; or static field holding an Activity or View.&lt;/li&gt;
&lt;li&gt;A singleton retaining an Activity context.&lt;/li&gt;
&lt;li&gt;A listener or callback that was never unregistered.&lt;/li&gt;
&lt;li&gt;A long-running coroutine capturing an Activity.&lt;/li&gt;
&lt;li&gt;An inner class or anonymous callback retaining its outer Activity.&lt;/li&gt;
&lt;li&gt;A Handler message or delayed task retaining a View or Activity.&lt;/li&gt;
&lt;li&gt;A lifecycle observer registered with an owner that outlives the Activity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Bad example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;ScreenTracker&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;activity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Activity&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the Activity is assigned here and never cleared, the singleton can keep it reachable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better approach:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Do not store Activity references in singletons.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;applicationContext&lt;/code&gt; only when an application-level context is actually appropriate.&lt;/li&gt;
&lt;li&gt;Tie UI work to &lt;code&gt;lifecycleScope&lt;/code&gt; or &lt;code&gt;repeatOnLifecycle&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Unregister listeners from the same lifecycle scope in which they were registered.&lt;/li&gt;
&lt;li&gt;Prefer lifecycle-aware APIs and avoid retaining Views in ViewModels.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;WeakReference&lt;/code&gt; only for specific cases where its semantics are appropriate; it is not a substitute for correct ownership.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;How I investigate&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reproduce the navigation or screen-closing flow.&lt;/li&gt;
&lt;li&gt;Capture a heap dump with Android Studio Memory Profiler.&lt;/li&gt;
&lt;li&gt;Look for Activity instances that remain after they should be gone.&lt;/li&gt;
&lt;li&gt;Inspect the reference path to a GC root.&lt;/li&gt;
&lt;li&gt;Fix the owner/cleanup issue and repeat the capture to confirm the Activity is collectible.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; The key concept is reachability, not whether &lt;code&gt;onDestroy()&lt;/code&gt; was called.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Performance: The app becomes slow after running for 20 minutes. How would you investigate?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;A delayed slowdown suggests a problem that accumulates over time. I would compare performance shortly after launch with performance after reproducing the slowdown.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Investigate these areas:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Memory growth:&lt;/strong&gt; Look for retained Activities, Views, bitmaps, caches without limits, and continuously growing collections.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CPU usage:&lt;/strong&gt; Find repeated work, expensive sorting/filtering, parsing, compression, or calculations on the main thread.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Main-thread stalls:&lt;/strong&gt; Inspect slow frames, long-running callbacks, lock contention, and synchronous disk or network work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Background work:&lt;/strong&gt; Check whether polling, timers, observers, or retries are multiplying instead of being cancelled or deduplicated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database and storage:&lt;/strong&gt; Inspect slow queries, missing indexes, large transactions, and repeated disk access.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rendering:&lt;/strong&gt; Look for unnecessary recompositions, expensive layouts, large images, and excessive UI updates.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Tools&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Android Studio CPU Profiler.&lt;/li&gt;
&lt;li&gt;Android Studio Memory Profiler and heap dumps.&lt;/li&gt;
&lt;li&gt;Android Studio Network Inspector.&lt;/li&gt;
&lt;li&gt;Perfetto and system traces.&lt;/li&gt;
&lt;li&gt;Macrobenchmark for repeatable performance measurements.&lt;/li&gt;
&lt;li&gt;JankStats or frame timing telemetry for field visibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would profile the same user journey under the same conditions, identify the dominant bottleneck, make one targeted change, and measure again.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; Do not optimize based on intuition. Measure before and after, and distinguish memory growth from CPU saturation and main-thread blocking.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Concurrency: Two API requests update the same screen, but the older response arrives last. How do you prevent stale UI?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;This is a race condition. Network completion order is not guaranteed to match request order.&lt;/p&gt;

&lt;p&gt;If the latest user action should win, I would make that rule explicit and cancel or invalidate previous work.&lt;/p&gt;

&lt;p&gt;With Kotlin coroutines and Flow, &lt;code&gt;flatMapLatest&lt;/code&gt; is useful when a new input should replace the previous request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SearchViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SearchRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debounce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;distinctUntilChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;flatMapLatest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;map&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Item&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SearchUiState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="nc"&gt;SearchUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SearchUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;orEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stateIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;scope&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;started&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SharingStarted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WhileSubscribed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SearchUiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Loading&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onQueryChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;flatMapLatest&lt;/code&gt; cancels collection of the previous inner flow when a new query arrives. The repository and underlying client should cooperate with cancellation where possible.&lt;/p&gt;

&lt;p&gt;For independent requests that cannot be cancelled, use a monotonically increasing request ID or compare a server-provided version before applying a result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;latestRequestId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;requestId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="n"&gt;latestRequestId&lt;/span&gt;

    &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;requestId&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;latestRequestId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact strategy depends on product semantics. For example, two independent dashboard panels may both be valid, while a search box usually follows latest-input-wins behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; Cancellation is useful, but correctness should not depend only on assuming that every underlying operation stops immediately. Guard the state update when necessary.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Offline handling: A user edits data offline while another device updates the same record. What happens?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;This is a distributed data consistency problem. Both devices may have legitimate changes, so blindly overwriting the server record can cause data loss.&lt;/p&gt;

&lt;p&gt;I would design the app around an explicit synchronization and conflict policy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A practical offline-first design:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Store the user's change locally in a database such as Room.&lt;/li&gt;
&lt;li&gt;Update the local UI immediately from the local source of truth.&lt;/li&gt;
&lt;li&gt;Record a pending operation in an outbox table.&lt;/li&gt;
&lt;li&gt;When connectivity returns, send pending operations to the server.&lt;/li&gt;
&lt;li&gt;Let the server validate the base version and decide whether the update can be applied.&lt;/li&gt;
&lt;li&gt;If there is a conflict, apply the product's defined resolution policy and update local state.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A record can include a version number or ETag. The client sends the version it originally read. The server can reject an update based on an outdated version, commonly with a conflict response such as HTTP 409, or use conditional requests such as &lt;code&gt;If-Match&lt;/code&gt; with HTTP 412.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Possible conflict strategies&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server wins:&lt;/strong&gt; Use the server's latest value. Simple, but may discard the offline edit.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Client wins:&lt;/strong&gt; Apply the local edit. Useful only when overwriting is acceptable.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Field-level merge:&lt;/strong&gt; Merge non-conflicting fields and flag overlapping edits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Manual resolution:&lt;/strong&gt; Ask the user to choose when the data is important.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain-specific merge:&lt;/strong&gt; For example, merge independent additions to a list rather than replacing the whole list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Idempotency keys so retrying an operation does not create duplicate effects.&lt;/li&gt;
&lt;li&gt;Retry with backoff for transient failures.&lt;/li&gt;
&lt;li&gt;Ordering and dependencies between queued operations.&lt;/li&gt;
&lt;li&gt;Authentication expiry while offline.&lt;/li&gt;
&lt;li&gt;Tombstones or deletion handling.&lt;/li&gt;
&lt;li&gt;Conflict visibility and observability.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; WorkManager can help schedule persistent synchronization, but it does not solve conflict resolution. The server and client need a shared consistency contract.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Release management: A new version introduces a critical bug. How would you reduce the impact?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;First, I would determine whether the bug is limited to a particular version, feature, device group, or backend interaction. Then I would stop increasing exposure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Response plan:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pause or halt the staged rollout in Google Play Console if the release is still rolling out.&lt;/li&gt;
&lt;li&gt;Disable the affected functionality remotely if a safe, tested feature flag or server-side kill switch exists.&lt;/li&gt;
&lt;li&gt;Assess whether the backend or data format also changed. A client rollback alone may not reverse server-side effects.&lt;/li&gt;
&lt;li&gt;Prepare a hotfix with a regression test and validate it on affected configurations.&lt;/li&gt;
&lt;li&gt;Publish the corrected release and increase rollout gradually while monitoring crash-free users, ANRs, key flows, and support reports.&lt;/li&gt;
&lt;li&gt;Communicate clearly with support and internal stakeholders.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A Play Store update is not an instant rollback for users who already installed the faulty version. Some users may remain on it until they receive and install a newer release. A remote mitigation can reduce exposure sooner, but only if the app was designed to support it safely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; Release management is risk management. Staged rollout, observability, feature flags, backward-compatible APIs, and a tested hotfix process should exist before an incident.&lt;/p&gt;




&lt;h1&gt;
  
  
  More Real-World Android Interview Questions
&lt;/h1&gt;

&lt;p&gt;The first six scenarios cover common production challenges. These additional questions help assess architecture, platform knowledge, security, and engineering judgment.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Process death: The user returns to the app and the screen has lost its state. How would you prevent this?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would distinguish configuration changes from process death.&lt;/p&gt;

&lt;p&gt;A ViewModel survives configuration changes, but it does not survive process death. Small UI state can be saved with &lt;code&gt;SavedStateHandle&lt;/code&gt; or Compose's &lt;code&gt;rememberSaveable&lt;/code&gt;. Durable business data should be stored in a persistent source such as Room or a backend.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DetailsViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;savedStateHandle&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SavedStateHandle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DetailsRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;selectedTab&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;savedStateHandle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"selected_tab"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;initialValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;selectTab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;savedStateHandle&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"selected_tab"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not put large objects or entire database records into saved instance state. Save identifiers and lightweight UI choices, then reload durable data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; ViewModel handles configuration changes; saved state helps restore lightweight state after system-initiated process recreation; persistent storage preserves application data.&lt;/p&gt;




&lt;h2&gt;
  
  
  8. ANRs: Users report that the app freezes, but Crashlytics shows few crashes. What would you check?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;An Application Not Responding (ANR) occurs when the app fails to respond to input within the system's allowed time. It is not necessarily a crash.&lt;/p&gt;

&lt;p&gt;I would inspect Android vitals and ANR traces, then look for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Network or disk I/O on the main thread.&lt;/li&gt;
&lt;li&gt;Long database transactions.&lt;/li&gt;
&lt;li&gt;Expensive JSON parsing or bitmap processing.&lt;/li&gt;
&lt;li&gt;Deadlocks and lock contention.&lt;/li&gt;
&lt;li&gt;Synchronous Binder calls.&lt;/li&gt;
&lt;li&gt;Heavy work in lifecycle callbacks or broadcast receivers.&lt;/li&gt;
&lt;li&gt;Excessive work during app startup.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would use Perfetto, traces, StrictMode in debug builds, and Android vitals to identify the blocked thread and the work preventing it from responding.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; Moving work to a background dispatcher is not enough if the main thread still waits synchronously for it.&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Security: How would you protect access and refresh tokens in an Android app?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would begin with the server-side authentication design, not just local storage.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use short-lived access tokens and a carefully designed refresh-token lifecycle.&lt;/li&gt;
&lt;li&gt;Store sensitive token material using Android Keystore-backed encryption where appropriate.&lt;/li&gt;
&lt;li&gt;Avoid logging tokens, putting them in analytics, or including them in crash reports.&lt;/li&gt;
&lt;li&gt;Use TLS and validate the server endpoint correctly.&lt;/li&gt;
&lt;li&gt;Rotate refresh tokens when supported and detect reuse on the backend.&lt;/li&gt;
&lt;li&gt;Revoke sessions on logout and handle refresh failure consistently.&lt;/li&gt;
&lt;li&gt;Keep secrets out of the APK; values embedded in a client app cannot be treated as confidential.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Encrypted storage reduces exposure at rest, but it does not make a compromised or instrumented device fully trusted. The backend must still authorize every sensitive operation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; Client-side storage is one layer. Authorization and session validity must be enforced by the server.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Architecture: A ViewModel has become 1,500 lines long. How would you refactor it?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would first identify responsibilities and dependencies rather than splitting the class mechanically.&lt;/p&gt;

&lt;p&gt;A healthier structure might include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ViewModel: screen state and user actions.&lt;/li&gt;
&lt;li&gt;Use cases: application-specific operations and orchestration.&lt;/li&gt;
&lt;li&gt;Repository: data coordination and source-of-truth decisions.&lt;/li&gt;
&lt;li&gt;Data sources: API, database, files, or preferences.&lt;/li&gt;
&lt;li&gt;Mappers: convert network/database models into domain or UI models.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would extract one cohesive responsibility at a time, preserve observable behavior, and add tests around the existing behavior before moving code. Dependency injection can make boundaries explicit.&lt;/p&gt;

&lt;p&gt;I would not create a use case for every one-line function automatically. Abstractions should clarify ownership, enable reuse, or improve testability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; Clean Architecture is about dependency direction and clear boundaries, not the number of packages or classes.&lt;/p&gt;




&lt;h2&gt;
  
  
  11. Testing: A bug appears only on slow networks. How would you test it reliably?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would make network conditions part of the test strategy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unit-test retry, timeout, parsing, and error mapping.&lt;/li&gt;
&lt;li&gt;Use fake repositories to test ViewModel state transitions deterministically.&lt;/li&gt;
&lt;li&gt;Use MockWebServer or an equivalent HTTP test server to simulate delayed responses, disconnects, malformed payloads, and error codes.&lt;/li&gt;
&lt;li&gt;Test cancellation and out-of-order responses.&lt;/li&gt;
&lt;li&gt;Add UI tests for loading, empty, error, and retry states.&lt;/li&gt;
&lt;li&gt;Run selected end-to-end tests on constrained network profiles and real devices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tests should verify user-visible behavior, not merely that a request method was called.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; A successful response test does not validate the behavior of a real network-dependent feature.&lt;/p&gt;




&lt;h2&gt;
  
  
  12. Database migrations: The new app version crashes while opening an existing user's database. What would you do?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would reproduce the upgrade path using a database created by the previous production version. A fresh-install test is not enough.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Review the migration path from every supported schema version.&lt;/li&gt;
&lt;li&gt;Add explicit Room migrations rather than destructive fallback for user data.&lt;/li&gt;
&lt;li&gt;Test migrations with representative old database files.&lt;/li&gt;
&lt;li&gt;Verify indexes, constraints, default values, and data transformations.&lt;/li&gt;
&lt;li&gt;Make migrations safe for partially populated or legacy data.&lt;/li&gt;
&lt;li&gt;Keep a recovery plan for corrupted or incompatible data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Room's migration testing support can validate schema transitions. I would also monitor database-related exceptions after rollout.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; Database migration correctness is about upgrading real user data, not only creating the latest schema from scratch.&lt;/p&gt;




&lt;h2&gt;
  
  
  13. Battery drain: Users say your app drains battery in the background. How would you investigate?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would inspect background activity and wakeups before changing scheduling code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Identify frequent polling, repeated retries, location updates, and unnecessary sensors.&lt;/li&gt;
&lt;li&gt;Check whether jobs are duplicated or rescheduled too aggressively.&lt;/li&gt;
&lt;li&gt;Use WorkManager for deferrable, persistent work with appropriate constraints.&lt;/li&gt;
&lt;li&gt;Batch network requests where possible.&lt;/li&gt;
&lt;li&gt;Stop location and sensor updates when no longer needed.&lt;/li&gt;
&lt;li&gt;Use Battery Historian and Android Studio profiling to understand wakeups and background execution.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WorkManager is not intended for exact-time execution. If work can be deferred, let the system batch it efficiently.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; A background task that is reliable but runs too often can still be a poor mobile implementation.&lt;/p&gt;




&lt;h2&gt;
  
  
  14. App startup: Cold startup takes five seconds. How would you improve it?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;I would measure startup first, separating time to initial display from time to fully usable content.&lt;/p&gt;

&lt;p&gt;Then I would inspect:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Synchronous work in &lt;code&gt;Application.onCreate()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Eager initialization of SDKs and dependency graphs.&lt;/li&gt;
&lt;li&gt;Main-thread disk access.&lt;/li&gt;
&lt;li&gt;Expensive dependency injection modules.&lt;/li&gt;
&lt;li&gt;Blocking database or network calls.&lt;/li&gt;
&lt;li&gt;Heavy first-screen rendering and image decoding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I would defer nonessential initialization, use lazy or on-demand setup where appropriate, and render useful initial UI without waiting for unrelated work. Baseline Profiles and Macrobenchmark can help measure and improve startup and critical journeys.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; Do not move every initialization task to a background thread blindly. Identify which work is required before first interaction and which can safely happen later.&lt;/p&gt;




&lt;h2&gt;
  
  
  15. Jetpack Compose: A screen recomposes too frequently. How would you debug it?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Answer
&lt;/h3&gt;

&lt;p&gt;Recomposition is not inherently bad. I would investigate whether it causes measurable frame drops or unnecessary work.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use Layout Inspector recomposition counts as a clue, not as a performance verdict.&lt;/li&gt;
&lt;li&gt;Inspect state ownership and ensure state is scoped as narrowly as practical.&lt;/li&gt;
&lt;li&gt;Avoid creating expensive derived objects on every recomposition; use &lt;code&gt;remember&lt;/code&gt; when the value is stable for the relevant keys.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;derivedStateOf&lt;/code&gt; when it meaningfully reduces downstream updates from rapidly changing state.&lt;/li&gt;
&lt;li&gt;Use stable, unique keys in lazy lists.&lt;/li&gt;
&lt;li&gt;Avoid broad state reads high in the UI tree when only a small child needs the value.&lt;/li&gt;
&lt;li&gt;Measure actual frame performance with Macrobenchmark or tracing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key distinction:&lt;/strong&gt; The goal is not zero recompositions. The goal is correct state behavior and smooth rendering.&lt;/p&gt;




&lt;h1&gt;
  
  
  A Strong Interview Answer Framework
&lt;/h1&gt;

&lt;p&gt;For scenario-based questions, avoid jumping straight to a library or code snippet. Use this sequence:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clarify impact:&lt;/strong&gt; Who is affected, how often, and since which release?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Collect evidence:&lt;/strong&gt; Logs, metrics, traces, device/API details, and reproduction steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Form a hypothesis:&lt;/strong&gt; Explain the likely failure modes and how you will distinguish them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Mitigate risk:&lt;/strong&gt; Reduce user impact before attempting a large refactor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Implement a focused fix:&lt;/strong&gt; Preserve existing behavior and add regression coverage.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validate in production:&lt;/strong&gt; Monitor the same metric or failure signature after rollout.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Prevent recurrence:&lt;/strong&gt; Improve tests, dashboards, release checks, and engineering practices.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A strong answer also explains trade-offs. For example, cancelling an old request is useful, but a request ID guard may still be needed. A local database enables offline use, but conflict resolution requires a server contract. A hotfix helps, but a remote kill switch may reduce impact sooner.&lt;/p&gt;

&lt;h1&gt;
  
  
  Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Android engineering does not end when the app compiles or the UI looks correct on one device.&lt;/p&gt;

&lt;p&gt;Production-ready development means understanding lifecycle ownership, concurrency, data consistency, observability, performance, security, testing, and release strategy.&lt;/p&gt;

&lt;p&gt;The best interview answers demonstrate a repeatable way to investigate problems—not just familiarity with Android libraries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Learn the platform. Measure real behavior. Design for failure. Validate every fix.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Suggested learning path
&lt;/h2&gt;

&lt;p&gt;If you are preparing for senior Android interviews, study these topics in this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Android fundamentals:&lt;/strong&gt; Activity lifecycle, process death, services, threading, memory management.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Kotlin concurrency:&lt;/strong&gt; Coroutines, Flow, cancellation, structured concurrency, race conditions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Architecture and data:&lt;/strong&gt; MVVM, Clean Architecture, Room, offline-first design, synchronization, conflict resolution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; Profilers, Perfetto, ANRs, startup, rendering, Compose performance.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Security:&lt;/strong&gt; Android Keystore, OAuth 2.0/OIDC, token lifecycle, secure networking, threat modeling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Production engineering:&lt;/strong&gt; Crash reporting, Android vitals, staged rollouts, feature flags, CI/CD, incident response.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Build a small sample app that includes offline edits, retry handling, process-death restoration, and production-style telemetry. Then deliberately introduce failures and practice diagnosing them. That experience is more valuable than memorizing answers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; Padmakar Garg&lt;br&gt;&lt;br&gt;
Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture, mobile security, and high-performance app development.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>interview</category>
      <category>mobiledevelopment</category>
    </item>
    <item>
      <title>Gen AI vs Agentic AI: From Generating Content to Taking Action</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Mon, 30 Nov 2026 05:56:30 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/gen-ai-vs-agentic-ai-from-generating-content-to-taking-action-4n36</link>
      <guid>https://dev.to/gargpadmakar/gen-ai-vs-agentic-ai-from-generating-content-to-taking-action-4n36</guid>
      <description>&lt;p&gt;Artificial Intelligence is moving through an important transition.&lt;/p&gt;

&lt;p&gt;A few years ago, most people interacted with AI through prediction, classification, and recommendation systems. Then Generative AI made it possible to create text, code, images, summaries, and much more from natural-language instructions.&lt;/p&gt;

&lt;p&gt;Now, the conversation is expanding toward &lt;strong&gt;Agentic AI&lt;/strong&gt;: systems that can work toward a goal, decide which steps to take, use tools, observe results, and continue until they reach a defined outcome or need human help.&lt;/p&gt;

&lt;p&gt;These terms are related, but they are not interchangeable. Understanding the difference is useful whether you are an Android developer, backend engineer, architect, or someone starting with AI.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is Generative AI?
&lt;/h2&gt;

&lt;p&gt;Generative AI (Gen AI) refers to models that generate new content based on patterns learned from training data and the context provided at inference time.&lt;/p&gt;

&lt;p&gt;Instead of only classifying an input or retrieving an existing record, a generative model can produce a new response.&lt;/p&gt;

&lt;p&gt;Common examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Writing or rewriting an email.&lt;/li&gt;
&lt;li&gt;Summarizing a long document.&lt;/li&gt;
&lt;li&gt;Generating Kotlin, Java, Python, or SQL code.&lt;/li&gt;
&lt;li&gt;Creating an image from a text prompt.&lt;/li&gt;
&lt;li&gt;Translating content between languages.&lt;/li&gt;
&lt;li&gt;Explaining a technical concept with examples.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Large Language Models (LLMs) are a widely used form of generative AI for language-related tasks. Other generative models work with images, audio, video, and other modalities.&lt;/p&gt;

&lt;h3&gt;
  
  
  A simple example
&lt;/h3&gt;

&lt;p&gt;You ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Write a Kotlin extension function that validates an email address.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A Gen AI model can generate a possible implementation, explain it, and suggest test cases.&lt;/p&gt;

&lt;p&gt;The model is generating an answer. Unless it is connected to tools and explicitly given permission to use them, it is not automatically changing your repository, running the tests, or opening a pull request.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. What is Agentic AI?
&lt;/h2&gt;

&lt;p&gt;Agentic AI describes AI systems designed to pursue goals through a sequence of actions.&lt;/p&gt;

&lt;p&gt;An agent may use an LLM for reasoning, but the complete system usually includes more than a model:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Goal:&lt;/strong&gt; What outcome should be achieved?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Planning:&lt;/strong&gt; What steps may be needed?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State or memory:&lt;/strong&gt; What has happened so far?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tools:&lt;/strong&gt; Which external capabilities can it use?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Observation:&lt;/strong&gt; What happened after each action?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Control logic:&lt;/strong&gt; Should it continue, retry, stop, or ask a person?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An agent can select a tool, call it, inspect the result, and decide what to do next. This creates a loop rather than a single prompt-and-response interaction.&lt;/p&gt;

&lt;h3&gt;
  
  
  A simple example
&lt;/h3&gt;

&lt;p&gt;You ask:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Investigate why the Android unit tests are failing and prepare a fix.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A suitably configured development agent might:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Inspect the project structure.&lt;/li&gt;
&lt;li&gt;Read the relevant source and test files.&lt;/li&gt;
&lt;li&gt;Run the test command.&lt;/li&gt;
&lt;li&gt;Interpret the failure output.&lt;/li&gt;
&lt;li&gt;Propose or apply a focused code change.&lt;/li&gt;
&lt;li&gt;Run the tests again.&lt;/li&gt;
&lt;li&gt;Summarize the changes and remaining issues.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The important difference is that the system is working toward an outcome across multiple steps. The agent should still operate within permissions and review boundaries you define.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Gen AI vs Agentic AI
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Area&lt;/th&gt;
&lt;th&gt;Generative AI&lt;/th&gt;
&lt;th&gt;Agentic AI&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary purpose&lt;/td&gt;
&lt;td&gt;Generate content or responses&lt;/td&gt;
&lt;td&gt;Achieve a goal through actions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical interaction&lt;/td&gt;
&lt;td&gt;Prompt → response&lt;/td&gt;
&lt;td&gt;Goal → plan → action → observation → next action&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Autonomy&lt;/td&gt;
&lt;td&gt;Usually responds to each request&lt;/td&gt;
&lt;td&gt;Can make bounded decisions across steps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Tool usage&lt;/td&gt;
&lt;td&gt;May have no tools or use tools when requested&lt;/td&gt;
&lt;td&gt;Often coordinates tools as part of its workflow&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;State&lt;/td&gt;
&lt;td&gt;Often focused on the current context&lt;/td&gt;
&lt;td&gt;May maintain task state across steps&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Example&lt;/td&gt;
&lt;td&gt;Generate a Kotlin function&lt;/td&gt;
&lt;td&gt;Inspect code, edit a function, run tests, report results&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Human role&lt;/td&gt;
&lt;td&gt;Review the generated output&lt;/td&gt;
&lt;td&gt;Set goals, permissions, checkpoints, and review outcomes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; Agentic AI is not a completely separate kind of model. An agentic system can use a generative model as its reasoning or language component. The agent behavior comes from the surrounding orchestration, tools, state, and control flow.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. How does an AI agent work?
&lt;/h2&gt;

&lt;p&gt;A useful mental model is a repeating cycle:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;             User Goal
                 |
                 v
          Understand Goal
                 |
                 v
           Plan Next Step
                 |
                 v
          Select a Tool
                 |
                 v
          Execute Action
                 |
                 v
          Observe Result
                 |
                 v
       Is the Goal Complete?
            /          \
          No            Yes
          |              |
          +----&amp;gt; Plan    v
                 Next  Final Response
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example, an agent asked to prepare a weekly engineering report could retrieve approved project updates, summarize them, identify blockers, draft the report, and request approval before sending it.&lt;/p&gt;

&lt;p&gt;The loop needs boundaries. Without limits, an agent can repeat failed actions, spend excessive resources, or make changes that were never intended.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. The building blocks of an agentic system
&lt;/h2&gt;

&lt;h3&gt;
  
  
  5.1 LLM — reasoning and language
&lt;/h3&gt;

&lt;p&gt;The model interprets instructions, extracts relevant context, proposes plans, and helps choose the next action.&lt;/p&gt;

&lt;p&gt;An LLM does not automatically know the current state of your systems. It needs reliable context from tools, databases, or other approved sources.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.2 Tools — the ability to act
&lt;/h3&gt;

&lt;p&gt;Tools connect the agent to capabilities outside the model.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Search documentation.&lt;/li&gt;
&lt;li&gt;Read a file.&lt;/li&gt;
&lt;li&gt;Query a database.&lt;/li&gt;
&lt;li&gt;Create a ticket.&lt;/li&gt;
&lt;li&gt;Call an API.&lt;/li&gt;
&lt;li&gt;Run a test command.&lt;/li&gt;
&lt;li&gt;Send a notification.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tool access should follow least privilege. A reporting agent may only need read access, while a deployment agent may require a separate approval before production access.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.3 Memory and state
&lt;/h3&gt;

&lt;p&gt;These concepts are related but different:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Conversation context:&lt;/strong&gt; Information available in the current interaction.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Task state:&lt;/strong&gt; The current step, completed actions, and pending work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long-term memory:&lt;/strong&gt; Information intentionally retained for future tasks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Memory should be scoped, relevant, secure, and subject to retention rules. Sensitive data should not be stored just because it appeared in a prompt.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.4 Planning and orchestration
&lt;/h3&gt;

&lt;p&gt;The orchestrator manages the workflow: which step runs, which tool is available, how results are checked, and when the process should stop.&lt;/p&gt;

&lt;p&gt;Some tasks are best handled by one agent. Others may benefit from multiple specialized agents, but adding agents also adds coordination and debugging complexity.&lt;/p&gt;

&lt;h3&gt;
  
  
  5.5 Guardrails and human approval
&lt;/h3&gt;

&lt;p&gt;A production agent needs clear rules:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Which tools can it call?&lt;/li&gt;
&lt;li&gt;Which data can it access?&lt;/li&gt;
&lt;li&gt;Which actions are read-only?&lt;/li&gt;
&lt;li&gt;Which actions require approval?&lt;/li&gt;
&lt;li&gt;How many steps or retries are allowed?&lt;/li&gt;
&lt;li&gt;What happens when a tool fails?&lt;/li&gt;
&lt;li&gt;How are actions logged and audited?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For high-impact actions—such as deleting data, issuing refunds, changing access permissions, or deploying to production—use explicit approval and verification.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Real-world use cases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Software development
&lt;/h3&gt;

&lt;p&gt;A development agent can inspect a bug report, locate relevant code, propose a patch, run tests, and prepare a change summary. A human developer should review the diff and test results before merging.&lt;/p&gt;

&lt;h3&gt;
  
  
  Customer support
&lt;/h3&gt;

&lt;p&gt;An agent can retrieve an order status, check the applicable support policy, draft a response, and route exceptions to a human representative.&lt;/p&gt;

&lt;h3&gt;
  
  
  IT operations
&lt;/h3&gt;

&lt;p&gt;An operations agent can inspect monitoring alerts, gather logs, correlate known incidents, and recommend a remediation. Any action that affects production should follow an approved runbook and access policy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Research and reporting
&lt;/h3&gt;

&lt;p&gt;A research workflow can search approved sources, collect evidence, compare findings, and prepare a report with references. The output still needs source verification.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mobile application development
&lt;/h3&gt;

&lt;p&gt;Imagine an Android engineering assistant that can:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read a crash report.&lt;/li&gt;
&lt;li&gt;Identify the affected screen and related code.&lt;/li&gt;
&lt;li&gt;Inspect lifecycle and coroutine usage.&lt;/li&gt;
&lt;li&gt;Suggest a focused fix.&lt;/li&gt;
&lt;li&gt;Run relevant unit tests or static checks.&lt;/li&gt;
&lt;li&gt;Produce a review summary.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This can reduce repetitive investigation, but it does not replace engineering judgment, device testing, security review, or release validation.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. A practical architecture for an agentic application
&lt;/h2&gt;

&lt;p&gt;A simplified architecture might look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                User / Application
                        |
                        v
                  Agent Interface
                        |
                        v
                 Agent Orchestrator
                   /     |      \
                  /      |       \
                 v       v        v
              Planner  State   Policy &amp;amp; Limits
                 |
                 v
             Tool Router
          /      |       \
         v       v        v
       APIs   Database  Developer Tools
          \      |       /
           \     |      /
            v    v     v
              Results
                 |
                 v
          Validate Outcome
                 |
          +------+------+
          |             |
          v             v
      Continue     Human Approval
          |             |
          +------+------+
                 |
                 v
             Final Result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A production implementation should also include authentication, authorization, observability, audit logs, timeouts, retries, rate limits, and cost controls. Treat tool outputs as untrusted input and validate them before using them in subsequent actions.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. What are the risks of Agentic AI?
&lt;/h2&gt;

&lt;p&gt;More autonomy means more responsibility. Common risks include:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Hallucinations:&lt;/strong&gt; A model may produce incorrect facts or assumptions. Verify important claims against trusted sources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prompt injection:&lt;/strong&gt; Instructions hidden in documents, web pages, or tool results may try to manipulate the agent. Treat retrieved content as data, not as trusted system instructions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Excessive permissions:&lt;/strong&gt; An agent with broad access can cause more damage if it makes a mistake. Use least-privilege credentials and narrowly scoped tools.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Uncontrolled loops:&lt;/strong&gt; Repeated retries or poor planning can increase latency and cost. Set step limits, budgets, timeouts, and clear stop conditions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Data exposure:&lt;/strong&gt; Logs, prompts, memory, and tool calls may contain confidential information. Apply data minimization, access controls, and retention policies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Incorrect actions:&lt;/strong&gt; A plausible plan can still produce a harmful result. Add validation and human approval for consequential operations.&lt;/p&gt;

&lt;p&gt;Agentic systems should be evaluated not only on whether they produce a good final answer, but also on whether their intermediate actions are safe, authorized, efficient, and reproducible.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. How should developers start learning?
&lt;/h2&gt;

&lt;p&gt;You do not need to begin by building a multi-agent platform. Learn the fundamentals in layers.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Understand AI and LLM basics:&lt;/strong&gt; Tokens, context windows, embeddings, inference, model limitations, and hallucinations.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learn prompting and structured output:&lt;/strong&gt; Clear instructions, JSON schemas, constrained responses, and evaluation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build a simple model-powered feature:&lt;/strong&gt; For example, a document summarizer or an in-app help assistant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Learn tool/function calling:&lt;/strong&gt; Let the model request a narrowly defined operation, then validate and execute it in application code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add workflow state:&lt;/strong&gt; Track task progress, tool results, errors, and retry limits.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add retrieval when needed:&lt;/strong&gt; Learn embeddings and Retrieval-Augmented Generation (RAG) for grounding answers in your own documents.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Introduce agent loops carefully:&lt;/strong&gt; Add planning, observation, and bounded iteration to a task that genuinely needs multiple steps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Add security and evaluation:&lt;/strong&gt; Test tool permissions, prompt-injection resistance, failure handling, latency, and cost.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  A project idea for Android developers
&lt;/h3&gt;

&lt;p&gt;Build an &lt;strong&gt;AI-powered Android troubleshooting assistant&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Start with a read-only feature:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User enters an error message or pastes a crash log.&lt;/li&gt;
&lt;li&gt;The app sends a sanitized request to your backend.&lt;/li&gt;
&lt;li&gt;The backend asks an LLM to explain the likely cause.&lt;/li&gt;
&lt;li&gt;The response includes possible next debugging steps.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Then evolve it into a bounded agent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Retrieve relevant internal documentation.&lt;/li&gt;
&lt;li&gt;Search an approved knowledge base.&lt;/li&gt;
&lt;li&gt;Generate a proposed investigation checklist.&lt;/li&gt;
&lt;li&gt;Track which steps have been completed.&lt;/li&gt;
&lt;li&gt;Ask for confirmation before performing any action that changes a system.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep API keys on the backend, not inside the Android APK. Add authentication, rate limiting, privacy controls, and request logging with sensitive data redacted.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Final thoughts
&lt;/h2&gt;

&lt;p&gt;Generative AI helps machines create useful content. Agentic AI builds on those capabilities to pursue goals through planning, tool use, feedback, and controlled action.&lt;/p&gt;

&lt;p&gt;The shift is not simply from a chatbot to a more powerful chatbot. It is from generating a response to coordinating a workflow.&lt;/p&gt;

&lt;p&gt;For developers, the best way to learn is to start with one small, measurable task. Build a reliable Gen AI feature first, add a tool with limited permissions, then introduce state and a bounded action loop. Keep humans in control wherever mistakes can have real consequences.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The future is not about giving AI unlimited autonomy. It is about building useful systems that can act reliably within clearly defined boundaries.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; Padmakar Garg&lt;br&gt;&lt;br&gt;
Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture, mobile security, and high-performance app development.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>machinelearning</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>Android System Design: From Fundamentals to Production-Ready Architecture</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Mon, 30 Nov 2026 05:29:47 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/android-system-design-from-fundamentals-to-production-ready-architecture-2pp6</link>
      <guid>https://dev.to/gargpadmakar/android-system-design-from-fundamentals-to-production-ready-architecture-2pp6</guid>
      <description>&lt;p&gt;Building an Android application that works is one challenge. Building one that remains reliable as features grow, users increase, networks become unstable, devices run out of memory, and security requirements become stricter is a different challenge.&lt;/p&gt;

&lt;p&gt;That second challenge is where &lt;strong&gt;Android system design&lt;/strong&gt; becomes essential.&lt;/p&gt;

&lt;p&gt;System design is not just about choosing MVVM, Clean Architecture, or a dependency injection framework. It is the process of making engineering decisions across the entire mobile system: the Android client, local storage, network communication, backend services, security boundaries, observability, and release pipeline.&lt;/p&gt;

&lt;p&gt;A good design should answer questions such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How will the app behave when the device goes offline?&lt;/li&gt;
&lt;li&gt;What happens if two requests try to refresh an expired token at the same time?&lt;/li&gt;
&lt;li&gt;How will a large feature set be divided into maintainable modules?&lt;/li&gt;
&lt;li&gt;How can the app avoid duplicate writes and inconsistent local data?&lt;/li&gt;
&lt;li&gt;What happens when Android kills the process?&lt;/li&gt;
&lt;li&gt;How will the app recover from a failed upload?&lt;/li&gt;
&lt;li&gt;Which work belongs on the device, and which belongs on the server?&lt;/li&gt;
&lt;li&gt;How will we measure performance and diagnose production failures?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article builds a practical learning path for Android engineers who want to design production-ready applications and confidently discuss architecture in senior-level interviews.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What does Android system design include?
&lt;/h2&gt;

&lt;p&gt;Android system design has two connected perspectives.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mobile application design
&lt;/h3&gt;

&lt;p&gt;This focuses on the Android application itself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;UI and state management&lt;/li&gt;
&lt;li&gt;Lifecycle-aware components&lt;/li&gt;
&lt;li&gt;App architecture and modularization&lt;/li&gt;
&lt;li&gt;Local persistence and caching&lt;/li&gt;
&lt;li&gt;Coroutines and background work&lt;/li&gt;
&lt;li&gt;Performance, battery, and memory&lt;/li&gt;
&lt;li&gt;Permissions and platform security&lt;/li&gt;
&lt;li&gt;Testing and release management&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  End-to-end system design
&lt;/h3&gt;

&lt;p&gt;This considers the mobile app as one part of a larger distributed system:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API contracts and backend services&lt;/li&gt;
&lt;li&gt;Authentication and authorization&lt;/li&gt;
&lt;li&gt;Load balancing and caching&lt;/li&gt;
&lt;li&gt;Real-time communication&lt;/li&gt;
&lt;li&gt;Data synchronization&lt;/li&gt;
&lt;li&gt;Push notifications&lt;/li&gt;
&lt;li&gt;Observability and reliability&lt;/li&gt;
&lt;li&gt;Scalability and failure recovery&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A senior Android engineer should understand both. You do not need to implement every backend component, but you should understand its contract, failure modes, and impact on the mobile client.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Start with requirements before choosing architecture
&lt;/h2&gt;

&lt;p&gt;A common mistake is to begin a design discussion with a technology choice:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We will use MVVM, Retrofit, Room, and Hilt.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is not a design yet. First establish what the system must do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional requirements
&lt;/h3&gt;

&lt;p&gt;These describe user-visible capabilities. For a task management application, examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Users can sign in and view assigned tasks.&lt;/li&gt;
&lt;li&gt;Users can create, update, submit, and close tasks.&lt;/li&gt;
&lt;li&gt;Managers can review and approve submissions.&lt;/li&gt;
&lt;li&gt;Users can attach images or documents.&lt;/li&gt;
&lt;li&gt;Changes can be made while offline and synchronized later.&lt;/li&gt;
&lt;li&gt;Users receive notifications about important updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Non-functional requirements
&lt;/h3&gt;

&lt;p&gt;These describe system qualities and constraints:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Requirement&lt;/th&gt;
&lt;th&gt;Design question&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Reliability&lt;/td&gt;
&lt;td&gt;Can the user recover from a failed request?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance&lt;/td&gt;
&lt;td&gt;Does the main screen render quickly?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Scalability&lt;/td&gt;
&lt;td&gt;Can the backend handle increased traffic?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Security&lt;/td&gt;
&lt;td&gt;How are credentials and sensitive data protected?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Availability&lt;/td&gt;
&lt;td&gt;What features remain usable offline?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Maintainability&lt;/td&gt;
&lt;td&gt;Can teams change features independently?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Testability&lt;/td&gt;
&lt;td&gt;Can business rules be tested without Android UI?&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Accessibility&lt;/td&gt;
&lt;td&gt;Can users operate the app with assistive technology?&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Also clarify constraints: minimum Android version, supported devices, expected data volume, upload sizes, latency expectations, regulatory requirements, and whether multiple devices can edit the same record.&lt;/p&gt;

&lt;p&gt;These decisions shape the architecture far more than a framework preference.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. High-Level Design (HLD) and Low-Level Design (LLD)
&lt;/h2&gt;

&lt;p&gt;HLD and LLD answer different questions.&lt;/p&gt;

&lt;h3&gt;
  
  
  High-Level Design
&lt;/h3&gt;

&lt;p&gt;HLD describes the major components and how data moves between them.&lt;/p&gt;

&lt;p&gt;For a typical Android application:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 Android Application
                         |
              Presentation Layer
              Compose / ViewModel
                         |
                   Domain Layer
                Use Cases / Models
                         |
                    Data Layer
                    Repository
                    /        \
             Local Source   Remote Source
             Room / SQLDelight  Retrofit
                    \        /
                 Sync Coordinator
                         |
                    Backend API
                         |
            Services / Database / Cache
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The diagram is intentionally logical. A real application may have more layers, modules, or services depending on its requirements.&lt;/p&gt;

&lt;p&gt;HLD should explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Component responsibilities&lt;/li&gt;
&lt;li&gt;Communication boundaries&lt;/li&gt;
&lt;li&gt;Data ownership&lt;/li&gt;
&lt;li&gt;Trust boundaries&lt;/li&gt;
&lt;li&gt;Offline and failure behavior&lt;/li&gt;
&lt;li&gt;External dependencies&lt;/li&gt;
&lt;li&gt;Scalability constraints&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Low-Level Design
&lt;/h3&gt;

&lt;p&gt;LLD describes the implementation inside those components.&lt;/p&gt;

&lt;p&gt;For example, a task feature might contain:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;feature-task/
  presentation/
    TaskListScreen.kt
    TaskListViewModel.kt
    TaskUiState.kt
    TaskUiAction.kt
  domain/
    model/
      Task.kt
    repository/
      TaskRepository.kt
    usecase/
      GetTasksUseCase.kt
      UpdateTaskUseCase.kt
  data/
    local/
      TaskDao.kt
      TaskEntity.kt
    remote/
      TaskApi.kt
      TaskDto.kt
    mapper/
      TaskMapper.kt
    repository/
      TaskRepositoryImpl.kt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;LLD should define interfaces, models, method contracts, state transitions, error types, threading rules, and test boundaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A useful rule:&lt;/strong&gt; HLD explains the system's shape; LLD explains how each part works.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Choosing an Android application architecture
&lt;/h2&gt;

&lt;p&gt;There is no universally correct architecture. Choose one that makes responsibilities clear and fits the size and lifetime of the product.&lt;/p&gt;

&lt;h3&gt;
  
  
  MVVM
&lt;/h3&gt;

&lt;p&gt;Model-View-ViewModel separates UI rendering from UI state and presentation logic.&lt;/p&gt;

&lt;p&gt;A typical flow is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The UI sends an action to the ViewModel.&lt;/li&gt;
&lt;li&gt;The ViewModel invokes a use case or repository.&lt;/li&gt;
&lt;li&gt;The data layer performs the operation.&lt;/li&gt;
&lt;li&gt;The ViewModel exposes observable state.&lt;/li&gt;
&lt;li&gt;The UI renders the new state.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TaskUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;emptyList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TaskListViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;getTasks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;GetTasksUseCase&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;TaskUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;TaskUiState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;loadTasks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="nf"&gt;getTasks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tasks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onFailure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                    &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                            &lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="n"&gt;errorMessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s2"&gt;"Unable to load tasks"&lt;/span&gt;
                        &lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an illustrative example. The exact result type and error policy should be defined by the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clean Architecture
&lt;/h3&gt;

&lt;p&gt;Clean Architecture separates the application into boundaries, commonly described as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Presentation:&lt;/strong&gt; UI, ViewModels, UI state, and user actions.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Domain:&lt;/strong&gt; business rules, domain models, and use cases.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data:&lt;/strong&gt; repository implementations, data sources, DTOs, entities, and mapping.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The important dependency rule is that outer layers depend inward. Business rules should not depend on Retrofit, Room, Compose, or Android framework classes.&lt;/p&gt;

&lt;p&gt;For example, the domain layer can declare:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;TaskRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getTasks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;updateTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;task&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The data layer implements that interface using local and remote data sources.&lt;/p&gt;

&lt;p&gt;Do not create a use case for every trivial line of code just to increase the number of classes. Use cases are most valuable when they represent meaningful business operations, coordinate repositories, or enforce business rules.&lt;/p&gt;

&lt;h3&gt;
  
  
  MVI and unidirectional data flow
&lt;/h3&gt;

&lt;p&gt;MVI-style designs make state transitions explicit:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Action
    |
    v
ViewModel / Reducer
    |
    v
Business Operation
    |
    v
New UI State
    |
    v
UI Rendering
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can be especially useful for complex screens with many interactions. The goal is predictable state, not adopting a particular naming convention.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Modularization for growing applications
&lt;/h2&gt;

&lt;p&gt;A single-module app may be perfectly reasonable for a small product. As teams and features grow, modularization can improve build isolation, ownership, reuse, and testability.&lt;/p&gt;

&lt;p&gt;A possible structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;:app
:core:common
:core:model
:core:designsystem
:core:network
:core:database
:core:datastore
:core:testing
:feature:auth
:feature:home
:feature:tasks
:feature:profile
:feature:notifications
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Some practical guidelines:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Prefer feature modules for cohesive product capabilities.&lt;/li&gt;
&lt;li&gt;Keep shared modules small and intentional.&lt;/li&gt;
&lt;li&gt;Avoid circular dependencies.&lt;/li&gt;
&lt;li&gt;Keep implementation details internal where possible.&lt;/li&gt;
&lt;li&gt;Define stable contracts between modules.&lt;/li&gt;
&lt;li&gt;Measure build performance before adding complex build logic.&lt;/li&gt;
&lt;li&gt;Do not split every class into its own Gradle module.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Kotlin Multiplatform projects, identify which business logic and data abstractions can be shared, while keeping platform-specific UI and integrations behind appropriate boundaries.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Networking: design for failure, not just success
&lt;/h2&gt;

&lt;p&gt;Mobile networks are unpredictable. Requests can time out, connections can disappear, servers can return errors, and users can repeat actions.&lt;/p&gt;

&lt;p&gt;A robust network layer should define:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connection and read timeouts&lt;/li&gt;
&lt;li&gt;Request cancellation&lt;/li&gt;
&lt;li&gt;Error mapping&lt;/li&gt;
&lt;li&gt;Retry policy&lt;/li&gt;
&lt;li&gt;Authentication handling&lt;/li&gt;
&lt;li&gt;Serialization rules&lt;/li&gt;
&lt;li&gt;Logging with sensitive-data redaction&lt;/li&gt;
&lt;li&gt;Request identifiers and tracing context&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Retry with care
&lt;/h3&gt;

&lt;p&gt;Retries are useful for transient failures, but retrying every request can create duplicate operations.&lt;/p&gt;

&lt;p&gt;A retry policy should consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the error transient?&lt;/li&gt;
&lt;li&gt;Is the operation safe to retry?&lt;/li&gt;
&lt;li&gt;Does the endpoint support idempotency?&lt;/li&gt;
&lt;li&gt;Is there a maximum attempt count?&lt;/li&gt;
&lt;li&gt;Is exponential backoff with jitter appropriate?&lt;/li&gt;
&lt;li&gt;Should the user be informed?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, retrying a read request after a temporary connection failure is often reasonable. Retrying a payment or task-creation request without an idempotency strategy may create duplicates.&lt;/p&gt;

&lt;h3&gt;
  
  
  Token refresh and concurrent requests
&lt;/h3&gt;

&lt;p&gt;Consider this situation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The access token expires.&lt;/li&gt;
&lt;li&gt;Five API requests receive an unauthorized response.&lt;/li&gt;
&lt;li&gt;All five try to refresh the token simultaneously.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A naive implementation may issue five refresh requests and create inconsistent authentication state.&lt;/p&gt;

&lt;p&gt;A better design coordinates refresh operations so only one refresh is active at a time. Other requests wait for its result. The implementation must also handle refresh failure, logout, cancellation, and token replacement safely.&lt;/p&gt;

&lt;p&gt;Use OAuth 2.0 and OpenID Connect flows appropriate to the application. For native apps, follow current best practices such as Authorization Code with PKCE when using an external identity provider. Keep client secrets out of the mobile binary.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Offline-first architecture and data synchronization
&lt;/h2&gt;

&lt;p&gt;Offline-first design is not simply caching an API response. It defines which data is authoritative, how local changes are recorded, and how conflicts are resolved.&lt;/p&gt;

&lt;p&gt;A common approach is to make the local database the source read by the UI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 UI
                  |
               ViewModel
                  |
               Use Case
                  |
              Repository
              /        \
         Local DB     Remote API
              |
         Observable Data
              |
               UI
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The UI observes local data. Network responses update the local database, and local writes can be queued for synchronization.&lt;/p&gt;

&lt;h3&gt;
  
  
  A practical synchronization flow
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The user edits a task.&lt;/li&gt;
&lt;li&gt;The app validates the input.&lt;/li&gt;
&lt;li&gt;The local database is updated immediately.&lt;/li&gt;
&lt;li&gt;A pending operation is recorded.&lt;/li&gt;
&lt;li&gt;WorkManager schedules synchronization when constraints are met.&lt;/li&gt;
&lt;li&gt;The worker sends the operation to the backend.&lt;/li&gt;
&lt;li&gt;The backend validates and processes it.&lt;/li&gt;
&lt;li&gt;The local record is marked synchronized or retains a retryable failure state.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conflict resolution
&lt;/h3&gt;

&lt;p&gt;Imagine a task is edited on two devices while both are offline.&lt;/p&gt;

&lt;p&gt;When both reconnect, the system needs a defined policy. Options include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Last-write-wins using server-controlled timestamps&lt;/li&gt;
&lt;li&gt;Version-based optimistic concurrency&lt;/li&gt;
&lt;li&gt;Field-level conflict resolution&lt;/li&gt;
&lt;li&gt;User-assisted conflict resolution&lt;/li&gt;
&lt;li&gt;Domain-specific merge rules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not assume that the device clock is trustworthy. For important data, use server versions or other server-coordinated mechanisms.&lt;/p&gt;

&lt;p&gt;Also distinguish between a request being sent and a change being confirmed by the server. An optimistic UI should be able to represent pending, synchronized, and failed states.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Concurrency and background work
&lt;/h2&gt;

&lt;p&gt;Android applications need to manage work across UI, I/O, CPU, and lifecycle boundaries.&lt;/p&gt;

&lt;h3&gt;
  
  
  Kotlin Coroutines
&lt;/h3&gt;

&lt;p&gt;Understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Structured concurrency&lt;/li&gt;
&lt;li&gt;CoroutineScope and Job&lt;/li&gt;
&lt;li&gt;Dispatchers&lt;/li&gt;
&lt;li&gt;Cancellation&lt;/li&gt;
&lt;li&gt;Exception propagation&lt;/li&gt;
&lt;li&gt;SupervisorJob&lt;/li&gt;
&lt;li&gt;Flow, StateFlow, and SharedFlow&lt;/li&gt;
&lt;li&gt;Mutex and thread safety&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use &lt;code&gt;viewModelScope&lt;/code&gt; for work tied to a ViewModel's lifetime. Use lifecycle-aware collection in the UI. Avoid unmanaged global scopes for application operations.&lt;/p&gt;

&lt;h3&gt;
  
  
  WorkManager
&lt;/h3&gt;

&lt;p&gt;Use WorkManager for deferrable, persistent work that should survive process death and be rescheduled when constraints become available.&lt;/p&gt;

&lt;p&gt;Examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Uploading queued attachments&lt;/li&gt;
&lt;li&gt;Periodic synchronization&lt;/li&gt;
&lt;li&gt;Retrying pending operations&lt;/li&gt;
&lt;li&gt;Sending diagnostic reports&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;WorkManager is not a guarantee of exact execution time. If a requirement needs immediate user-visible work, select an appropriate platform API and foreground execution model while respecting Android background execution limits.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Performance: design for real devices
&lt;/h2&gt;

&lt;p&gt;Performance is a system quality, not a final polishing step.&lt;/p&gt;

&lt;p&gt;Important areas include:&lt;/p&gt;

&lt;h3&gt;
  
  
  Startup
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Avoid expensive work on the main thread.&lt;/li&gt;
&lt;li&gt;Defer non-critical initialization.&lt;/li&gt;
&lt;li&gt;Measure cold, warm, and hot startup.&lt;/li&gt;
&lt;li&gt;Use Baseline Profiles where appropriate.&lt;/li&gt;
&lt;li&gt;Keep dependency initialization intentional.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Rendering
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Keep Compose state ownership clear.&lt;/li&gt;
&lt;li&gt;Avoid unnecessary recomposition.&lt;/li&gt;
&lt;li&gt;Use stable keys for lazy lists.&lt;/li&gt;
&lt;li&gt;Avoid expensive calculations during composition.&lt;/li&gt;
&lt;li&gt;Profile frame timing and rendering behavior.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Memory
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Understand object lifetimes.&lt;/li&gt;
&lt;li&gt;Avoid holding Activity or View references beyond their lifecycle.&lt;/li&gt;
&lt;li&gt;Close resources when required.&lt;/li&gt;
&lt;li&gt;Avoid unbounded in-memory caches.&lt;/li&gt;
&lt;li&gt;Inspect heap dumps and allocation patterns.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Images and media
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Resize images for the target display size.&lt;/li&gt;
&lt;li&gt;Use memory and disk caching appropriately.&lt;/li&gt;
&lt;li&gt;Avoid loading full-resolution media into memory unnecessarily.&lt;/li&gt;
&lt;li&gt;Handle cancellation when list items leave the screen.&lt;/li&gt;
&lt;li&gt;Consider paging for large datasets.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use Android Studio Profiler, Macrobenchmark, Microbenchmark, Perfetto, and system tracing to measure actual behavior. Avoid optimizing based only on assumptions.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Mobile application security
&lt;/h2&gt;

&lt;p&gt;Security should be considered at every boundary.&lt;/p&gt;

&lt;h3&gt;
  
  
  Protect sensitive data
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Store cryptographic keys using Android Keystore-backed mechanisms.&lt;/li&gt;
&lt;li&gt;Avoid storing passwords or long-lived secrets in plain text.&lt;/li&gt;
&lt;li&gt;Use platform-secure storage patterns for tokens.&lt;/li&gt;
&lt;li&gt;Encrypt sensitive local data when the threat model requires it.&lt;/li&gt;
&lt;li&gt;Minimize the amount of sensitive data stored on the device.&lt;/li&gt;
&lt;li&gt;Redact tokens and personal data from logs.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Secure communication
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use HTTPS and modern TLS configurations.&lt;/li&gt;
&lt;li&gt;Validate server certificates.&lt;/li&gt;
&lt;li&gt;Do not implement custom trust-all certificate logic.&lt;/li&gt;
&lt;li&gt;Consider certificate pinning only with a clear threat model and a safe rotation/recovery plan.&lt;/li&gt;
&lt;li&gt;Validate authorization on the server; hiding a button in the UI is not access control.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Defense in depth
&lt;/h3&gt;

&lt;p&gt;Depending on the application risk, consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Play Integrity API&lt;/li&gt;
&lt;li&gt;BiometricPrompt&lt;/li&gt;
&lt;li&gt;R8 shrinking and obfuscation&lt;/li&gt;
&lt;li&gt;Root and tampering signals&lt;/li&gt;
&lt;li&gt;Secure deep-link validation&lt;/li&gt;
&lt;li&gt;Exported component review&lt;/li&gt;
&lt;li&gt;WebView hardening&lt;/li&gt;
&lt;li&gt;OWASP MASVS-based verification&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No single control makes an app secure. Root detection, obfuscation, and integrity signals can raise the cost of attacks, but they do not replace server-side authorization and sound cryptographic design.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Scalability and distributed systems
&lt;/h2&gt;

&lt;p&gt;The Android client is only one participant in a distributed system.&lt;/p&gt;

&lt;p&gt;As usage grows, backend teams may introduce:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;API gateways&lt;/li&gt;
&lt;li&gt;Load balancers&lt;/li&gt;
&lt;li&gt;Horizontally scaled services&lt;/li&gt;
&lt;li&gt;Distributed caches&lt;/li&gt;
&lt;li&gt;Message brokers&lt;/li&gt;
&lt;li&gt;Database replication&lt;/li&gt;
&lt;li&gt;Object storage and CDNs&lt;/li&gt;
&lt;li&gt;Observability pipelines&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Android engineers should understand how these choices affect the client.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rate limiting should produce actionable error responses.&lt;/li&gt;
&lt;li&gt;Pagination should have stable ordering.&lt;/li&gt;
&lt;li&gt;Push notifications should not be treated as the only source of truth.&lt;/li&gt;
&lt;li&gt;WebSocket connections need reconnect and resubscription behavior.&lt;/li&gt;
&lt;li&gt;Backend changes should preserve API compatibility where possible.&lt;/li&gt;
&lt;li&gt;Long-running operations need status polling or server-driven updates.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Real-time communication
&lt;/h3&gt;

&lt;p&gt;WebSockets can support bidirectional communication, such as chat or live task updates. But a robust design also needs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reconnection with backoff and jitter&lt;/li&gt;
&lt;li&gt;Authentication renewal&lt;/li&gt;
&lt;li&gt;Heartbeats where appropriate&lt;/li&gt;
&lt;li&gt;Message identifiers&lt;/li&gt;
&lt;li&gt;Duplicate handling&lt;/li&gt;
&lt;li&gt;Delivery acknowledgements&lt;/li&gt;
&lt;li&gt;Resubscription after reconnect&lt;/li&gt;
&lt;li&gt;Fallback behavior when real-time transport is unavailable&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A push notification can alert the user that something changed. The app should still fetch or synchronize authoritative data from the backend.&lt;/p&gt;

&lt;h2&gt;
  
  
  12. Testing and observability
&lt;/h2&gt;

&lt;p&gt;A system is not production-ready just because it compiles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing strategy
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Test type&lt;/th&gt;
&lt;th&gt;What it verifies&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Unit tests&lt;/td&gt;
&lt;td&gt;Business rules and transformations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;ViewModel tests&lt;/td&gt;
&lt;td&gt;State transitions and UI logic&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repository tests&lt;/td&gt;
&lt;td&gt;Local/remote coordination&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database tests&lt;/td&gt;
&lt;td&gt;Queries and migrations&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API tests&lt;/td&gt;
&lt;td&gt;Serialization and error mapping&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Integration tests&lt;/td&gt;
&lt;td&gt;Component collaboration&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;UI tests&lt;/td&gt;
&lt;td&gt;Important user journeys&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Performance tests&lt;/td&gt;
&lt;td&gt;Startup and rendering budgets&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Use fakes for deterministic unit tests. Use real database or network test environments where integration behavior matters.&lt;/p&gt;

&lt;h3&gt;
  
  
  Observability
&lt;/h3&gt;

&lt;p&gt;Collect enough information to diagnose production issues without exposing sensitive data.&lt;/p&gt;

&lt;p&gt;Useful signals include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Crash-free sessions&lt;/li&gt;
&lt;li&gt;ANR rate&lt;/li&gt;
&lt;li&gt;Startup duration&lt;/li&gt;
&lt;li&gt;API latency and error rates&lt;/li&gt;
&lt;li&gt;Synchronization success rate&lt;/li&gt;
&lt;li&gt;Upload failure rate&lt;/li&gt;
&lt;li&gt;WebSocket reconnect frequency&lt;/li&gt;
&lt;li&gt;App version and device/API-level context&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Tools such as Firebase Crashlytics, Android Vitals, structured logs, and backend tracing can help. Define privacy and retention policies before collecting diagnostic data.&lt;/p&gt;

&lt;h2&gt;
  
  
  13. Case studies: apply the concepts
&lt;/h2&gt;

&lt;p&gt;The best way to learn system design is to design complete systems and explain your trade-offs.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case study A: Messaging application
&lt;/h3&gt;

&lt;p&gt;Requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One-to-one and group conversations&lt;/li&gt;
&lt;li&gt;Text and media messages&lt;/li&gt;
&lt;li&gt;Delivery and read receipts&lt;/li&gt;
&lt;li&gt;Offline message composition&lt;/li&gt;
&lt;li&gt;Multiple devices&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Local message database&lt;/li&gt;
&lt;li&gt;WebSocket for real-time delivery&lt;/li&gt;
&lt;li&gt;Push notifications for background alerts&lt;/li&gt;
&lt;li&gt;Message IDs and deduplication&lt;/li&gt;
&lt;li&gt;Attachment upload to object storage&lt;/li&gt;
&lt;li&gt;End-to-end encryption where required&lt;/li&gt;
&lt;li&gt;Message synchronization and pagination&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important question: How will the client recover messages missed while it was disconnected?&lt;/p&gt;

&lt;p&gt;A common answer is to maintain a server-supported synchronization cursor or sequence and fetch messages after reconnect. WebSocket delivery alone is not a durable synchronization strategy.&lt;/p&gt;

&lt;h3&gt;
  
  
  Case study B: Social media feed
&lt;/h3&gt;

&lt;p&gt;Requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Personalized feed&lt;/li&gt;
&lt;li&gt;Infinite scrolling&lt;/li&gt;
&lt;li&gt;Images and videos&lt;/li&gt;
&lt;li&gt;Likes and comments&lt;/li&gt;
&lt;li&gt;Refresh and offline behavior&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Cursor-based pagination&lt;/li&gt;
&lt;li&gt;Local feed cache&lt;/li&gt;
&lt;li&gt;Image loading and caching&lt;/li&gt;
&lt;li&gt;Optimistic interactions with rollback&lt;/li&gt;
&lt;li&gt;CDN delivery&lt;/li&gt;
&lt;li&gt;Stable ordering and duplicate filtering&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important question: How do you avoid duplicate or missing items when new content arrives while the user is scrolling?&lt;/p&gt;

&lt;h3&gt;
  
  
  Case study C: Task management application
&lt;/h3&gt;

&lt;p&gt;Requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Role-based task assignment&lt;/li&gt;
&lt;li&gt;Task updates and approvals&lt;/li&gt;
&lt;li&gt;Attachments&lt;/li&gt;
&lt;li&gt;Offline editing&lt;/li&gt;
&lt;li&gt;Notifications&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Design considerations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Feature-based modules&lt;/li&gt;
&lt;li&gt;Role-aware UI backed by server authorization&lt;/li&gt;
&lt;li&gt;Local database as the UI read source&lt;/li&gt;
&lt;li&gt;Pending-operation queue&lt;/li&gt;
&lt;li&gt;WorkManager synchronization&lt;/li&gt;
&lt;li&gt;Version-based conflict handling&lt;/li&gt;
&lt;li&gt;Audit history for important transitions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Important question: What happens if a manager approves a task while an employee is editing an older offline copy?&lt;/p&gt;

&lt;p&gt;The backend should enforce valid state transitions and concurrency rules. The client should reconcile the response and present a clear outcome rather than silently overwriting newer data.&lt;/p&gt;

&lt;h2&gt;
  
  
  14. A practical 12-week learning roadmap
&lt;/h2&gt;

&lt;p&gt;Here is a structured plan for moving from Android fundamentals to end-to-end system design.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Week&lt;/th&gt;
&lt;th&gt;Topic&lt;/th&gt;
&lt;th&gt;Practical output&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Android internals and lifecycle&lt;/td&gt;
&lt;td&gt;Explain app launch and process behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;HLD, LLD, SOLID, and architecture&lt;/td&gt;
&lt;td&gt;Design a feature architecture&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Modularization and dependency management&lt;/td&gt;
&lt;td&gt;Create a multi-module sample&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Networking and authentication&lt;/td&gt;
&lt;td&gt;Build a resilient API layer&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;Room, DataStore, and offline-first&lt;/td&gt;
&lt;td&gt;Build local-first task storage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Coroutines and WorkManager&lt;/td&gt;
&lt;td&gt;Implement reliable background sync&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Performance and profiling&lt;/td&gt;
&lt;td&gt;Measure and improve a slow screen&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Mobile security&lt;/td&gt;
&lt;td&gt;Threat-model and harden a sample app&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Backend and distributed systems&lt;/td&gt;
&lt;td&gt;Design API and service boundaries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Real-time systems and notifications&lt;/td&gt;
&lt;td&gt;Build reconnect and sync behavior&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;Testing and observability&lt;/td&gt;
&lt;td&gt;Add test coverage and diagnostics&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;System design case studies&lt;/td&gt;
&lt;td&gt;Present HLD, LLD, and trade-offs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A useful daily routine:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;45 minutes: Study one concept.&lt;/li&gt;
&lt;li&gt;45 minutes: Draw the architecture and explain the trade-offs.&lt;/li&gt;
&lt;li&gt;60 minutes: Implement or test a small example.&lt;/li&gt;
&lt;li&gt;30 minutes: Answer interview questions without looking at notes.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  15. How to approach a system design interview
&lt;/h2&gt;

&lt;p&gt;Use a repeatable structure rather than jumping straight into code.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Clarify requirements:&lt;/strong&gt; users, features, scale, platforms, and constraints.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Define quality attributes:&lt;/strong&gt; availability, latency, consistency, security, and offline needs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Estimate scale:&lt;/strong&gt; data volume, request frequency, media sizes, and concurrent connections when relevant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Propose HLD:&lt;/strong&gt; components, ownership, and communication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Walk through critical flows:&lt;/strong&gt; login, read, write, sync, upload, and failure recovery.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Deep dive into LLD:&lt;/strong&gt; interfaces, models, state transitions, and error handling.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Discuss trade-offs:&lt;/strong&gt; explain alternatives and why you chose one.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Cover operations:&lt;/strong&gt; testing, monitoring, rollout, and backward compatibility.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For every major decision, ask:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What problem does this solve?&lt;/li&gt;
&lt;li&gt;What complexity does it introduce?&lt;/li&gt;
&lt;li&gt;What happens when it fails?&lt;/li&gt;
&lt;li&gt;How will we test it?&lt;/li&gt;
&lt;li&gt;How will we know it works in production?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  16. Recommended resources
&lt;/h2&gt;

&lt;p&gt;Start with primary documentation and real codebases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/" rel="noopener noreferrer"&gt;Android Developers&lt;/a&gt; — platform, architecture, performance, and app quality.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/topic/architecture" rel="noopener noreferrer"&gt;Guide to app architecture&lt;/a&gt; — Android architecture guidance.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/android/nowinandroid" rel="noopener noreferrer"&gt;Now in Android&lt;/a&gt; — a production-style sample using modern Android practices.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://kotlinlang.org/docs/coroutines-overview.html" rel="noopener noreferrer"&gt;Kotlin Coroutines&lt;/a&gt; — coroutine concepts and structured concurrency.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/topic/libraries/architecture/workmanager" rel="noopener noreferrer"&gt;WorkManager&lt;/a&gt; — persistent background work.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://developer.android.com/topic/performance" rel="noopener noreferrer"&gt;Android Performance&lt;/a&gt; — profiling and performance practices.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://mas.owasp.org/MASVS/" rel="noopener noreferrer"&gt;OWASP MASVS&lt;/a&gt; — mobile application security verification.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/donnemartin/system-design-primer" rel="noopener noreferrer"&gt;System Design Primer&lt;/a&gt; — general distributed system design concepts.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Android system design is about making deliberate decisions across the complete lifecycle of an application.&lt;/p&gt;

&lt;p&gt;MVVM and Clean Architecture can help organize code, but production quality also depends on lifecycle awareness, data consistency, offline behavior, concurrency, network resilience, security, performance, testing, and observability.&lt;/p&gt;

&lt;p&gt;Do not try to learn every technology at once. Pick one real application, write its requirements, draw its HLD, define its data flows, implement a small vertical slice, test failure scenarios, and improve the design as requirements evolve.&lt;/p&gt;

&lt;p&gt;The goal is not to build the most complicated architecture. The goal is to build the simplest architecture that reliably satisfies the product's requirements and can evolve without becoming difficult to maintain.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Author:&lt;/strong&gt; Padmakar Garg&lt;br&gt;&lt;br&gt;
Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture, mobile security, and high-performance app development.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Discussion:&lt;/strong&gt; Which part of Android system design do you find most challenging: architecture, offline-first synchronization, networking, performance, security, or distributed systems?&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>architecture</category>
      <category>systemdesign</category>
    </item>
    <item>
      <title>Building KotlinImageLoader: A Modular Image Loading Library for Android</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Fri, 27 Nov 2026 17:55:25 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/building-kotlinimageloader-a-modular-image-loading-library-for-android-254f</link>
      <guid>https://dev.to/gargpadmakar/building-kotlinimageloader-a-modular-image-loading-library-for-android-254f</guid>
      <description>&lt;p&gt;When building modern Android applications, loading images seems deceptively simple until you encounter edge cases: OutOfMemory (OOM) crashes during fast scrolling in lists, redundant network fetches for identical image URLs, corrupted disk caches after unexpected process termination, rotated JPEG photos from phone cameras, and layout jank during Jetpack Compose recomposition.&lt;/p&gt;

&lt;p&gt;While mature libraries like Glide and Coil exist, understanding the underlying mechanics of an image loading pipeline—and engineering a clean, modular SDK adhering to Clean Architecture—is essential for building resilient, high-performance Android apps.&lt;/p&gt;

&lt;p&gt;In this deep dive, we break down the design, architecture, and internals of &lt;strong&gt;&lt;a href="https://github.com/gargpadmakar/KotlinImageLoader" rel="noopener noreferrer"&gt;KotlinImageLoader 1.1.0&lt;/a&gt;&lt;/strong&gt;: an open-source Android image loading SDK built from scratch in 100% Kotlin with Coroutines, StateFlow, and Jetpack Compose.&lt;/p&gt;




&lt;h2&gt;
  
  
  Architecture &amp;amp; Module Decomposition
&lt;/h2&gt;

&lt;p&gt;To prevent monolithic bloat, guarantee strict layer boundaries, and optimize build times for consuming applications, KotlinImageLoader is architected across 7 focused Gradle modules:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;image-compose, image-view ──► image-loader, image-core
image-loader              ──► image-data, image-domain, image-core
image-data                ──► image-domain, image-core
image-mvi                 ──► image-domain, image-core
image-domain              ──► image-core
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;image-core&lt;/code&gt;&lt;/strong&gt;: Defines core models (&lt;code&gt;ImageRequest&lt;/code&gt;, &lt;code&gt;ImageSize&lt;/code&gt;, &lt;code&gt;ImageResult&lt;/code&gt;), the transformation interface and built-in transformations, and network monitoring. It uses Android APIs such as &lt;code&gt;Bitmap&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;image-domain&lt;/code&gt;&lt;/strong&gt;: Houses &lt;code&gt;LoadImageUseCase&lt;/code&gt;, request validation, and the &lt;code&gt;ImageRepository&lt;/code&gt; contract.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;image-data&lt;/code&gt;&lt;/strong&gt;: Encapsulates data sources: thread-safe memory LRU cache, disk LRU cache with temp-file-and-rename writes, HTTP network fetcher, repository-level retry with increasing delay, and format decoders (GIF, WebP, SVG, JPEG/PNG).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;image-loader&lt;/code&gt;&lt;/strong&gt;: The main client-facing API (&lt;code&gt;ImageLoader&lt;/code&gt;, &lt;code&gt;RealImageLoader&lt;/code&gt;, &lt;code&gt;SingletonImageLoader&lt;/code&gt;) that wires together the use case, repository, caches and coroutine dispatchers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;image-compose&lt;/code&gt;&lt;/strong&gt;: Jetpack Compose UI components (&lt;code&gt;AsyncImage&lt;/code&gt;, &lt;code&gt;ZoomableAsyncImage&lt;/code&gt;) featuring state management, crossfade animations, and smooth touch gestures.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;image-view&lt;/code&gt;&lt;/strong&gt;: Classic Android View integration (&lt;code&gt;ImageView.loadImage()&lt;/code&gt;, &lt;code&gt;ZoomableImageView&lt;/code&gt;) with automatic item recycling cancellation in RecyclerViews.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;image-mvi&lt;/code&gt;&lt;/strong&gt;: Unidirectional data-flow (MVI) state container exposing &lt;code&gt;StateFlow&amp;lt;ImageState&amp;gt;&lt;/code&gt; for advanced custom UI rendering pipelines.&lt;/li&gt;
&lt;/ol&gt;




&lt;h2&gt;
  
  
  Core Engine Internals
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Two-Tier Caching
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Request ──► [Memory LRU Cache] ──(Hit)──► Render Bitmap
                   │ (Miss)
                   ▼
            [Disk LRU Cache] ───(Hit)────► Decode ──► Populate Memory ──► Render
                   │ (Miss)
                   ▼
            [Network Fetcher] ──(HTTP 200)──────► Disk Write (temp + rename) ──► Decode ──► Render
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Memory Cache&lt;/strong&gt;: A synchronized, access-ordered &lt;code&gt;LinkedHashMap&lt;/code&gt;, sized by default to 25% of the maximum JVM heap (&lt;code&gt;Runtime.maxMemory()&lt;/code&gt;). Bitmap sizes are measured with &lt;code&gt;bitmap.byteCount&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disk Cache&lt;/strong&gt;: After a download completes, the repository passes the image bytes to the disk cache, which writes them to a temporary file and then renames it to the final key (an MD5 hash of the request data). A process killed mid-write does not leave a partial file under that key. This is not a journaled cache. Version 1.1.0 has no startup cleanup for leftover temp files, and it does not &lt;code&gt;fsync&lt;/code&gt; before renaming, so it makes no durability guarantee across power loss.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Request Deduplication
&lt;/h3&gt;

&lt;p&gt;When rendering a feed containing duplicate author avatars, firing redundant HTTP requests degrades bandwidth and battery life. &lt;/p&gt;

&lt;p&gt;The suspend load path (&lt;code&gt;execute()&lt;/code&gt;) uses a &lt;code&gt;Mutex&lt;/code&gt;-guarded map of in-flight &lt;code&gt;Deferred&lt;/code&gt; results:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first request starts the fetch and decode, and registers its &lt;code&gt;Deferred&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Concurrent requests with the same data and target size join it instead of making a new network call.&lt;/li&gt;
&lt;li&gt;When it completes, waiting callers receive the result and the entry is removed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Current limitations: the key does not include transformations, so concurrent requests that differ only in transformations can receive the same result. The shared work also belongs to the first caller's coroutine scope, so cancelling that caller can cancel it for the others. The &lt;code&gt;flow()&lt;/code&gt; path used by &lt;code&gt;AsyncImage&lt;/code&gt; and &lt;code&gt;ImageView.loadImage()&lt;/code&gt; does not use this deduplication.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Cooperative Lifecycle Cancellation
&lt;/h3&gt;

&lt;p&gt;Image requests in fast-scrolling lists must abort immediately when an item leaves the viewport:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Jetpack Compose&lt;/strong&gt;: &lt;code&gt;AsyncImage&lt;/code&gt; collects the request flow inside a &lt;code&gt;LaunchedEffect&lt;/code&gt; keyed on the resolved request, the loader and network state. Leaving the composition cancels that coroutine.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Android Views / RecyclerView&lt;/strong&gt;: &lt;code&gt;ImageView.loadImage()&lt;/code&gt; stores a &lt;code&gt;Disposable&lt;/code&gt; in a view tag. Binding a new request to the same view, or detaching the view, disposes the previous one. A request ID check stops stale results from being applied.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Automatic EXIF Orientation (1–8)
&lt;/h3&gt;

&lt;p&gt;Smartphone camera sensors frequently write photos rotated 90° or 270°, embedding EXIF orientation metadata tags (1 through 8) in the JPEG APP1 segment (&lt;code&gt;0xFFE1&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;KotlinImageLoader has an &lt;code&gt;ExifOrientationHandler&lt;/code&gt; in &lt;code&gt;image-data&lt;/code&gt;, with its own Kotlin parser and a fallback to Android's &lt;code&gt;ExifInterface&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It reads the orientation tag from the encoded JPEG bytes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Orientation 1 (Normal)&lt;/strong&gt;: No new bitmap is created; the decoded bitmap is returned as is.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Orientations 2–8&lt;/strong&gt;: A &lt;code&gt;Matrix&lt;/code&gt; transform (rotation and/or flip) is applied after downsampled decoding and &lt;em&gt;before&lt;/em&gt; user transformations.&lt;/li&gt;
&lt;li&gt;Cached raw image bytes on disk remain intact and unaltered.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  🖼 Modular Image Formats &amp;amp; API Limitations
&lt;/h2&gt;

&lt;p&gt;KotlinImageLoader supports modern and legacy image formats with graceful platform degradation:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Format&lt;/th&gt;
&lt;th&gt;Decoding Mechanism&lt;/th&gt;
&lt;th&gt;Supported Android APIs&lt;/th&gt;
&lt;th&gt;Runtime Behavior &amp;amp; Graceful Degradation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;JPEG / PNG&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;BitmapFactory&lt;/code&gt; with &lt;code&gt;inSampleSize&lt;/code&gt; downsampling&lt;/td&gt;
&lt;td&gt;API 26+&lt;/td&gt;
&lt;td&gt;Standard hardware-accelerated bitmap decoding&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Static WebP&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;BitmapFactory&lt;/code&gt; with WebP headers&lt;/td&gt;
&lt;td&gt;API 26+&lt;/td&gt;
&lt;td&gt;Native decoding on all supported Android versions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Animated GIF&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ImageDecoder&lt;/code&gt; / &lt;code&gt;BitmapFactory&lt;/code&gt; fallback&lt;/td&gt;
&lt;td&gt;API 26+&lt;/td&gt;
&lt;td&gt;Static 1st frame on API 26–27; animated drawable on API 28+ (Views)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Animated WebP&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;ImageDecoder&lt;/code&gt; / &lt;code&gt;BitmapFactory&lt;/code&gt; fallback&lt;/td&gt;
&lt;td&gt;API 26+&lt;/td&gt;
&lt;td&gt;Static 1st frame on API 26–27; animated drawable on API 28+ (Views)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;SVG Vectors&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;AndroidX &lt;code&gt;PathParser&lt;/code&gt; + Canvas renderer&lt;/td&gt;
&lt;td&gt;API 26+&lt;/td&gt;
&lt;td&gt;XML parsing and custom vector canvas rendering&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;AsyncImage&lt;/code&gt; currently draws animated drawables into a bitmap, so they do not animate in Compose.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Benchmark Harness
&lt;/h2&gt;

&lt;p&gt;The repository includes a JVM benchmark test (&lt;code&gt;ImageLoaderBenchmarkTest&lt;/code&gt;) covering memory hits, disk hits (30 iterations each), cold loads (10 iterations) and 10 concurrent requests. It uses a mock fetcher with a simulated 12 ms delay and a mock decoder with a simulated 4 ms delay.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: Its timings do not measure real image decoding, network latency or on-device performance.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Quick Start &amp;amp; Installation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Repository Configuration
&lt;/h3&gt;

&lt;p&gt;Add the JitPack repository to your root &lt;code&gt;settings.gradle.kts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;dependencyResolutionManagement&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;repositories&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;google&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;mavenCentral&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;maven&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;url&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;uri&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://jitpack.io"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Add Dependencies
&lt;/h3&gt;

&lt;p&gt;Add the required modular artifacts to your module &lt;code&gt;build.gradle.kts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Core image loader engine&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.github.gargpadmakar.KotlinImageLoader:image-loader:1.1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Jetpack Compose components (AsyncImage, ZoomableAsyncImage)&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.github.gargpadmakar.KotlinImageLoader:image-compose:1.1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Android View extensions (ImageView.loadImage, ZoomableImageView)&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.github.gargpadmakar.KotlinImageLoader:image-view:1.1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Optional: Unidirectional MVI state management&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"com.github.gargpadmakar.KotlinImageLoader:image-mvi:1.1.0"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Usage Examples
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Jetpack Compose &lt;code&gt;AsyncImage&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foundation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Composable&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;padmakar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AsyncImage&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;padmakar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ImageRequest&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;padmakar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RoundedCornersTransformation&lt;/span&gt;

&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;UserAvatar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imageUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;AsyncImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ImageRequest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imageUrl&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;crossfade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;RoundedCornersTransformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;radiusPixels&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16f&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;contentDescription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"User Avatar"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;96&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Jetpack Compose &lt;code&gt;ZoomableAsyncImage&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;foundation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fillMaxSize&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runtime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Composable&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;androidx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ui&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Modifier&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;padmakar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;compose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ZoomableAsyncImage&lt;/span&gt;

&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;PhotoViewer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;imageUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ZoomableAsyncImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;model&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;imageUrl&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;contentDescription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Zoomable High-Res Photo"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;modifier&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Modifier&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fillMaxSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;maxScale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;4.0f&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Android View &lt;code&gt;ImageView.loadImage()&lt;/code&gt;
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;padmakar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;loadImage&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;com&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;padmakar&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageloader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;core&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;CircleCropTransformation&lt;/span&gt;

&lt;span class="n"&gt;imageView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;loadImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://images.unsplash.com/photo-1534528741775-53994a69daeb"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drawable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ic_placeholder&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;drawable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ic_error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;crossfade&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;CircleCropTransformation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Padmakar Garg&lt;/strong&gt; is an Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, KMP, scalable architecture, mobile security, and high-performance app development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GitHub&lt;/strong&gt;: &lt;a href="https://github.com/gargpadmakar" rel="noopener noreferrer"&gt;github.com/gargpadmakar&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Project Repository&lt;/strong&gt;: &lt;a href="https://github.com/gargpadmakar/KotlinImageLoader" rel="noopener noreferrer"&gt;KotlinImageLoader on GitHub&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Release Tag&lt;/strong&gt;: &lt;a href="https://jitpack.io/#gargpadmakar/KotlinImageLoader/1.1.0" rel="noopener noreferrer"&gt;v1.1.0 on JitPack&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;License&lt;/strong&gt;: Apache 2.0&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>jetpackcompose</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Saga, Circuit Breaker &amp; Friends on Android: Surviving a Checkout in a Metro Tunnel</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Fri, 27 Nov 2026 06:32:51 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/saga-circuit-breaker-friends-on-android-surviving-a-checkout-in-a-metro-tunnel-4276</link>
      <guid>https://dev.to/gargpadmakar/saga-circuit-breaker-friends-on-android-surviving-a-checkout-in-a-metro-tunnel-4276</guid>
      <description>&lt;p&gt;Riya is on the metro. She opens &lt;strong&gt;QuickBite&lt;/strong&gt; (a made-up food delivery app), adds a biryani, applies a coupon and taps &lt;strong&gt;Pay ₹349&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The train enters a tunnel.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The spinner keeps spinning.&lt;/li&gt;
&lt;li&gt;Did the payment go through?&lt;/li&gt;
&lt;li&gt;Is the coupon gone?&lt;/li&gt;
&lt;li&gt;If she taps &lt;strong&gt;Pay&lt;/strong&gt; again, will she be charged twice?&lt;/li&gt;
&lt;li&gt;If she swipes the app away, what happens to the half-finished order?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Patterns like &lt;strong&gt;Circuit Breaker&lt;/strong&gt; and &lt;strong&gt;Saga&lt;/strong&gt; are usually taught with backend microservices. But a mobile app is also one part of a distributed system, with an unreliable network and a process that the OS can kill. This post shows which patterns help on Android, and where each one stops helping.&lt;/p&gt;

&lt;h2&gt;
  
  
  The cheat sheet
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Pattern&lt;/th&gt;
&lt;th&gt;Question it answers&lt;/th&gt;
&lt;th&gt;Android / Kotlin tool&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Timeout&lt;/td&gt;
&lt;td&gt;How long do I wait?&lt;/td&gt;
&lt;td&gt;OkHttp timeouts, &lt;code&gt;withTimeout&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Retry + backoff + jitter&lt;/td&gt;
&lt;td&gt;Was it a temporary glitch?&lt;/td&gt;
&lt;td&gt;Arrow &lt;code&gt;Schedule&lt;/code&gt;, WorkManager backoff&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Circuit breaker&lt;/td&gt;
&lt;td&gt;Should I even try right now?&lt;/td&gt;
&lt;td&gt;Arrow &lt;code&gt;CircuitBreaker&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Fallback&lt;/td&gt;
&lt;td&gt;What do I show if it fails?&lt;/td&gt;
&lt;td&gt;Room cache&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bulkhead&lt;/td&gt;
&lt;td&gt;Can one slow feature starve the others?&lt;/td&gt;
&lt;td&gt;coroutine &lt;code&gt;Semaphore&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Single-flight&lt;/td&gt;
&lt;td&gt;Why are 5 calls refreshing the token?&lt;/td&gt;
&lt;td&gt;OkHttp &lt;code&gt;Authenticator&lt;/code&gt; + lock&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Idempotency key&lt;/td&gt;
&lt;td&gt;Is it safe to send this again?&lt;/td&gt;
&lt;td&gt;UUID + &lt;strong&gt;server support&lt;/strong&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Saga&lt;/td&gt;
&lt;td&gt;Step 3 failed. How do I undo 1 and 2?&lt;/td&gt;
&lt;td&gt;Arrow &lt;code&gt;saga&lt;/code&gt;, or Room + WorkManager&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Outbox&lt;/td&gt;
&lt;td&gt;How do I avoid losing a user's write?&lt;/td&gt;
&lt;td&gt;Room transaction + WorkManager&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Setup (check the latest versions and Kotlin compatibility before copying):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;dependencies&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"io.arrow-kt:arrow-resilience:2.3.0.1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"androidx.work:work-runtime-ktx:&amp;lt;latest&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;implementation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"androidx.room:room-ktx:&amp;lt;latest&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// plus Retrofit and OkHttp&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The snippets focus on the patterns. DI, DTOs and most imports are left out.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Timeout: never wait forever
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OkHttpClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connectTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SECONDS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SECONDS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;writeTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SECONDS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;callTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SECONDS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// limit for the whole call&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Treat these numbers as starting points and tune them from your own latency data.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The most important rule in this post:&lt;/strong&gt; a timeout does &lt;strong&gt;not&lt;/strong&gt; mean "it failed". It means "I don't know". The server may have charged Riya, and only the response got lost.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  2. Retry with exponential backoff and jitter
&lt;/h2&gt;

&lt;p&gt;Retry only errors that can fix themselves. A &lt;code&gt;400 Invalid coupon&lt;/code&gt; will stay invalid however often you send it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;Throwable&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isTransient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Simplification: some IOExceptions (e.g. certificate errors) are permanent&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;HttpException&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;408&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;429&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;599&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;networkRetry&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nc"&gt;Schedule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exponential&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Throwable&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;milliseconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;jittered&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// so thousands of phones don't retry in sync&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="ow"&gt;and&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Schedule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recurs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// 1 call + max 2 retries&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;doWhile&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isTransient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// usage (import arrow.resilience.retry)&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;menu&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;networkRetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;retry&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMenu&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;restaurantId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Watch out for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Retry only reads, or writes that are idempotent.&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't stack retries blindly.&lt;/strong&gt; OkHttp has its own connection-failure recovery. An interceptor, a repository and a worker that each make 3 attempts can multiply into 27 calls.&lt;/li&gt;
&lt;li&gt;If the server sends &lt;code&gt;Retry-After&lt;/code&gt;, respect it. The simple policy above does not read it.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. Circuit breaker: stop knocking on a closed door
&lt;/h2&gt;

&lt;p&gt;The recommendations service is down. Without a breaker, every screen waits, retries and fails, which costs battery, data and patience.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CLOSED ──too many failures──▶ OPEN ──reset timeout over──▶ HALF-OPEN
  ▲                            ▲                              │
  │                            └────────trial fails───────────┤
  └───────────────────────────────────trial succeeds──────────┘
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Closed:&lt;/strong&gt; calls go through.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Open:&lt;/strong&gt; calls fail immediately with &lt;code&gt;ExecutionRejected&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Half-open:&lt;/strong&gt; one trial call is allowed through.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// ONE shared instance per backend service (e.g. a Hilt @Singleton).&lt;/span&gt;
&lt;span class="c1"&gt;// A new breaker per call or per ViewModel remembers nothing useful.&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;recoBreaker&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;openingStrategy&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OpeningStrategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;maxFailures&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;resetTimeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;exponentialBackoffFactor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;maxResetTimeout&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With the &lt;code&gt;Count&lt;/code&gt; strategy, the breaker opens once failures &lt;strong&gt;exceed&lt;/strong&gt; the limit, and a success resets the counter.&lt;/p&gt;

&lt;h3&gt;
  
  
  The trap: 4xx errors can open your breaker
&lt;/h3&gt;

&lt;p&gt;A Retrofit &lt;code&gt;suspend&lt;/code&gt; function that returns the body type throws &lt;code&gt;HttpException&lt;/code&gt; for non-2xx responses. The breaker sees any exception from its block as a failure, so a few "invalid coupon" responses could block a healthy service. Return business rejections as &lt;strong&gt;values&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Rejected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Nothing&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;guard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="n"&gt;protectOrThrow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;block&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isTransient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt; &lt;span class="c1"&gt;// counts against the breaker&lt;/span&gt;
            &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rejected&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;code&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// the service is alive&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A mobile caveat: your breaker lives on &lt;strong&gt;one phone&lt;/strong&gt; and can't tell "server is down" from "Riya is in a tunnel". It also resets when the process dies. It complements server-side protection and does not replace it.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Fallback: fail soft
&lt;/h2&gt;

&lt;p&gt;Combine retry, breaker and a cached answer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Fresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dish&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Stale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dish&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;Hidden&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RecoRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RecoApi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DishDao&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;breaker&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// retry is outside, so every attempt is counted by the breaker.&lt;/span&gt;
        &lt;span class="c1"&gt;// ExecutionRejected is not transient, so an open circuit isn't retried.&lt;/span&gt;
        &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;networkRetry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;retry&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;breaker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;guard&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;recommendations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Success&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="nc"&gt;RecoResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fresh&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nc"&gt;Outcome&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Rejected&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CircuitBreaker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ExecutionRejected&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// No catch (Throwable): it would also swallow coroutine cancellation.&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;cached&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Hidden&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;RecoResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Stale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fallbacks suit menus and recommendations. &lt;strong&gt;Never fake success for a payment.&lt;/strong&gt; Show "Checking payment status" instead.&lt;/p&gt;

&lt;p&gt;For core screens, go further with &lt;strong&gt;offline-first&lt;/strong&gt;: the UI observes Room, and the network only updates Room. &lt;a href="https://developer.android.com/topic/architecture/data-layer/offline-first" rel="noopener noreferrer"&gt;Android's guide&lt;/a&gt; treats the local source as the source of truth.&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Bulkhead: isolate the noisy neighbour
&lt;/h2&gt;

&lt;p&gt;Image uploads for reviews shouldn't take up all the capacity while checkout waits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReviewUploader&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ReviewApi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Semaphore&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;permits&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// kotlinx.coroutines.sync&lt;/span&gt;

    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;upload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;photo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Photo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;permits&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withPermit&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;upload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;photo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Keep one shared &lt;code&gt;ReviewUploader&lt;/code&gt;. Note that &lt;code&gt;limitedParallelism(2)&lt;/code&gt; limits running threads, not in-flight suspending calls, so the semaphore is the right tool here.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Single-flight token refresh
&lt;/h2&gt;

&lt;p&gt;The token expires and five parallel calls get &lt;code&gt;401&lt;/code&gt;. You want &lt;strong&gt;one&lt;/strong&gt; refresh, not five.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TokenAuthenticator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TokenStore&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Authenticator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;route&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Route&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;priorResponse&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="c1"&gt;// simple loop guard&lt;/span&gt;

        &lt;span class="k"&gt;synchronized&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Authorization"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accessToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;fresh&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sent&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"Bearer $current"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;current&lt;/span&gt; &lt;span class="c1"&gt;// someone already refreshed&lt;/span&gt;
                        &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;tokens&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refreshBlocking&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;

            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;newBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Authorization"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Bearer $fresh"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;refreshBlocking()&lt;/code&gt; must use a separate client without this authenticator.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Idempotency key: the double-charge killer
&lt;/h2&gt;

&lt;p&gt;Create one ID for one user &lt;strong&gt;intent&lt;/strong&gt;, save it, and reuse it on every retry.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CheckoutApi&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"holds"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;holdStock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Body&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HoldRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Idempotency-Key"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Hold&lt;/span&gt;

    &lt;span class="nd"&gt;@POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"payments"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;charge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Body&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ChargeRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Idempotency-Key"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Payment&lt;/span&gt;

    &lt;span class="nd"&gt;@POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"orders"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;confirm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Body&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ConfirmRequest&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Idempotency-Key"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;

    &lt;span class="nd"&gt;@POST&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"payments/{id}/refund"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Idempotency-Key"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@DELETE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"holds/{id}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;releaseHold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;@Path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;@Header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Idempotency-Key"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The header alone does nothing. &lt;strong&gt;The backend must store the key&lt;/strong&gt; and, for a repeated request, return the original result without doing the work again. A new UUID per retry defeats the whole idea.&lt;/p&gt;

&lt;p&gt;(&lt;code&gt;charge&lt;/code&gt; here means a wallet or saved payment method. With a payment gateway SDK, follow its flow and verify the status on your server.)&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Saga: undo what you already did
&lt;/h2&gt;

&lt;p&gt;QuickBite's checkout has three remote steps. There is no single database transaction across them, so each step needs a &lt;strong&gt;compensating action&lt;/strong&gt;.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Step&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Compensation&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;Hold stock&lt;/td&gt;
&lt;td&gt;Release hold&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;Charge ₹349&lt;/td&gt;
&lt;td&gt;Refund&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Confirm order&lt;/td&gt;
&lt;td&gt;(final step)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  First: where should the saga live?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;For money and inventory, prefer a server-side orchestrator.&lt;/strong&gt; The app sends one &lt;code&gt;POST /checkouts&lt;/code&gt; with an idempotency key, then reads the status. The server stays alive when Riya's phone doesn't, and you don't trust a client to issue refunds.&lt;/p&gt;

&lt;p&gt;A client-side saga makes sense for flows such as &lt;em&gt;create draft → upload photos → publish listing&lt;/em&gt;, or when you must coordinate APIs you don't control. We keep the checkout example because its failures are easy to picture. Any refund endpoint must still be validated by the server.&lt;/p&gt;

&lt;h3&gt;
  
  
  8a. In-memory saga with Arrow
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;checkout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Cart&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;checkoutId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;saga&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;hold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;saga&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;holdStock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HoldRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$checkoutId:hold"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;releaseHold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$checkoutId:release"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;saga&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ChargeRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$checkoutId:charge"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$checkoutId:refund"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nf"&gt;saga&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;confirm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ConfirmRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hold&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$checkoutId:confirm"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* last step: nothing after it can fail */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If a later step throws, Arrow runs the compensations of the completed steps. It's clean, but it has &lt;strong&gt;two serious holes&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Ambiguous failures.&lt;/strong&gt; If &lt;code&gt;charge&lt;/code&gt; times out, that step never "completed", so its refund is not run, yet money may have moved. If &lt;code&gt;confirm&lt;/code&gt; times out, the code refunds and releases, but the order may actually be confirmed. You must &lt;strong&gt;check the status or retry with the same key&lt;/strong&gt; before compensating.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Process death.&lt;/strong&gt; This saga's progress is only in memory. If the OS kills the app after the charge, nothing resumes it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So don't use this version as is for real payments.&lt;/p&gt;

&lt;h3&gt;
  
  
  8b. Durable saga with Room + WorkManager
&lt;/h3&gt;

&lt;p&gt;Save progress after every step. On an unknown result, &lt;strong&gt;retry the same step with the same key&lt;/strong&gt;. Compensate only on a definite rejection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;STARTED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;STOCK_HELD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PAID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;CONFIRMED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;COMPENSATING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;FAILED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;NEEDS_SUPPORT&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Progress of each compensation, tracked separately&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="no"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;DONE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;REJECTED&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="c1"&gt;// DONE also means "nothing to undo"&lt;/span&gt;

&lt;span class="nd"&gt;@Entity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tableName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"checkout_saga"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CheckoutSaga&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nd"&gt;@PrimaryKey&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// the checkoutId&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cartId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;amountPaise&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Long&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STARTED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;holdId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;paymentId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nd"&gt;@Dao&lt;/span&gt;
&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;SagaDao&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Query&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SELECT * FROM checkout_saga WHERE id = :id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CheckoutSaga&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;

    &lt;span class="nd"&gt;@Upsert&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;saga&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CheckoutSaga&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CheckoutWorker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;params&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;WorkerParameters&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SagaDao&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;   &lt;span class="c1"&gt;// inject with a WorkerFactory / @HiltWorker&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CheckoutApi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CoroutineWorker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;params&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;doWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;inputData&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STARTED&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;hold&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;holdStock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HoldRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cartId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$id:hold"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STOCK_HELD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;holdId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;hold&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STOCK_HELD&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;charge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                            &lt;span class="nc"&gt;ChargeRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cartId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amountPaise&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$id:charge"&lt;/span&gt;
                        &lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PAID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;paymentId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PAID&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;confirm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                            &lt;span class="nc"&gt;ConfirmRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;holdId&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paymentId&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"$id:confirm"&lt;/span&gt;
                        &lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONFIRMED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;COMPENSATING&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;compensate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="c1"&gt;// still unresolved: try again later&lt;/span&gt;
                        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;COMPENSATING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                        &lt;span class="n"&gt;next&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONFIRMED&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;success&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FAILED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEEDS_SUPPORT&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// outcome unknown: same step, same key&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isTransient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="c1"&gt;// Definite rejection. Assumes your API contract guarantees&lt;/span&gt;
                &lt;span class="c1"&gt;// that this response means the step did NOT happen.&lt;/span&gt;
                &lt;span class="c1"&gt;// compensate() handles its own errors, so this is a forward step&lt;/span&gt;
                &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;COMPENSATING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// Tries both compensations (refund first). A failed refund&lt;/span&gt;
    &lt;span class="c1"&gt;// doesn't stop the hold release. Progress is saved after each one.&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;compensate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;start&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CheckoutSaga&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CheckoutSaga&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;start&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refund&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;paymentId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paymentId&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paymentId&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DONE&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;attemptUndo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paymentId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"${start.id}:refund"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;refund&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;release&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;holdId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;holdId&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;holdId&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DONE&lt;/span&gt;
                &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="n"&gt;attemptUndo&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;releaseHold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;holdId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"${start.id}:release"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;release&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;refund&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;release&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;COMPENSATING&lt;/span&gt;
            &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REJECTED&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NEEDS_SUPPORT&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Step&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FAILED&lt;/span&gt; &lt;span class="c1"&gt;// checkout failed, cleanup complete&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;step&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;attemptUndo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;call&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DONE&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IOException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt; &lt;span class="c1"&gt;// outcome unknown: retry with the same key&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isTransient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PENDING&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nc"&gt;UndoStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;REJECTED&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start it as &lt;strong&gt;unique work&lt;/strong&gt;, so a double tap on the same checkout doesn't create two workers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;enqueueCheckout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;checkoutId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;OneTimeWorkRequestBuilder&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;CheckoutWorker&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setInputData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;workDataOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"id"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;checkoutId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setConstraints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;Constraints&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setRequiredNetworkType&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;NetworkType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;CONNECTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setBackoffCriteria&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;BackoffPolicy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;EXPONENTIAL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;TimeUnit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;SECONDS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nc"&gt;WorkManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueueUniqueWork&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"checkout-$checkoutId"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ExistingWorkPolicy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEEP&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Production checklist:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the app dies &lt;strong&gt;after the server succeeds but before &lt;code&gt;dao.save&lt;/code&gt;&lt;/strong&gt;, the step runs again. Idempotency keys make that safe.&lt;/li&gt;
&lt;li&gt;Saving the row and enqueuing the work are not one atomic operation. On app start, re-enqueue unfinished sagas.&lt;/li&gt;
&lt;li&gt;Create the &lt;code&gt;checkoutId&lt;/code&gt; once and reuse it while that checkout is unfinished. Unique work can't help if each tap creates a new ID.&lt;/li&gt;
&lt;li&gt;WorkManager is for persistent work, not instant work. It may run later, so show a "Processing" state by observing the Room row.&lt;/li&gt;
&lt;li&gt;Keys expire on many servers, and holds expire too. After a long offline gap, check status with the server first. Often you shouldn't charge hours later without asking the user.&lt;/li&gt;
&lt;li&gt;Give &lt;code&gt;NEEDS_SUPPORT&lt;/code&gt; a real UI and alerting. Compensations can fail too. Show support which one was rejected: refund, release or both. If one is rejected while the other is still pending, consider alerting right away.&lt;/li&gt;
&lt;li&gt;Releasing the hold even when the refund fails assumes your business rules allow it. If they don't, keep the hold and send both to support.&lt;/li&gt;
&lt;li&gt;Your API should return success for "already refunded" or "already released". Otherwise a finished compensation can look rejected.&lt;/li&gt;
&lt;li&gt;If you already ship the old table, the two new columns need a Room migration.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  9. Outbox: never lose a user's write
&lt;/h2&gt;

&lt;p&gt;After delivery, Riya writes a review while offline. Save the review &lt;strong&gt;and&lt;/strong&gt; a "to be sent" record in one Room transaction.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;submitReview&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;review&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Review&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withTransaction&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reviewDao&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;review&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;synced&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;outboxDao&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;OutboxItem&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;randomUUID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reviewId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;review&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;scheduleOutboxSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;// unique work with a network constraint&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The worker reads pending items, sends each with &lt;code&gt;OutboxItem.id&lt;/code&gt; as the idempotency key, and removes it only after the server accepts it. Move permanently rejected items to a failed state so they don't block the queue.&lt;/p&gt;

&lt;p&gt;Think about scheduling too: with &lt;code&gt;KEEP&lt;/code&gt;, an item added just as the running worker finishes can be left behind. &lt;code&gt;APPEND_OR_REPLACE&lt;/code&gt; is one option, plus a sweep on app start.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Saga vs Outbox:&lt;/strong&gt; a saga coordinates &lt;em&gt;multiple steps with undo&lt;/em&gt;. An outbox makes sure &lt;em&gt;a saved write isn't forgotten&lt;/em&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  Back to Riya in the tunnel
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Moment&lt;/th&gt;
&lt;th&gt;What protects her&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Request hangs&lt;/td&gt;
&lt;td&gt;Timeout&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Brief signal drop&lt;/td&gt;
&lt;td&gt;Retry with backoff&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Recommendations service down&lt;/td&gt;
&lt;td&gt;Circuit breaker + cached fallback&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;She taps Pay again&lt;/td&gt;
&lt;td&gt;Disabled button + same checkout ID + idempotency key&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;She swipes the app away&lt;/td&gt;
&lt;td&gt;Saga state in Room + WorkManager&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Order is rejected after payment&lt;/td&gt;
&lt;td&gt;Compensation: refund + release&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Refund is rejected&lt;/td&gt;
&lt;td&gt;Hold release is still attempted, then &lt;code&gt;NEEDS_SUPPORT&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Offline review&lt;/td&gt;
&lt;td&gt;Outbox&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;(Force-stopping the app from system settings is different from swiping it away: scheduled work won't run until the app is opened again.)&lt;/p&gt;

&lt;h2&gt;
  
  
  What about other libraries?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Arrow Resilience:&lt;/strong&gt; Kotlin-first and multiplatform, with &lt;code&gt;Schedule&lt;/code&gt;, &lt;code&gt;CircuitBreaker&lt;/code&gt; and &lt;code&gt;saga&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resilience4j:&lt;/strong&gt; popular on JVM backends. Check its Java and Android compatibility for your version before adding it to an app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Plain coroutines:&lt;/strong&gt; enough for timeouts and a semaphore. Think twice before hand-writing a circuit breaker, because concurrency and half-open behaviour are easy to get wrong.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Don't over-engineer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A simple read screen: timeout + cache is often enough.&lt;/li&gt;
&lt;li&gt;A flaky, optional backend feature: add a circuit breaker.&lt;/li&gt;
&lt;li&gt;An important write: idempotency + outbox.&lt;/li&gt;
&lt;li&gt;A multi-step transaction: server-side saga first, durable client saga only if you must.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Test the unhappy paths
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;strong&gt;MockWebServer&lt;/strong&gt; to return 500s, 429s, slow responses and dropped connections.&lt;/li&gt;
&lt;li&gt;Kill the process between saga steps and check that it resumes.&lt;/li&gt;
&lt;li&gt;Drop the response &lt;strong&gt;after&lt;/strong&gt; the server processes a charge, and verify there is only one charge.&lt;/li&gt;
&lt;li&gt;Test a rejected refund and verify that the hold release is still attempted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The happy path shows that your app works. These tests show whether Riya can trust it.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;References:&lt;/strong&gt; &lt;a href="https://arrow-kt.io/learn/resilience/intro" rel="noopener noreferrer"&gt;Arrow Resilience&lt;/a&gt; · &lt;a href="https://arrow-kt.io/learn/resilience/circuitbreaker/" rel="noopener noreferrer"&gt;Arrow CircuitBreaker&lt;/a&gt; · &lt;a href="https://arrow-kt.io/learn/resilience/saga" rel="noopener noreferrer"&gt;Arrow Saga&lt;/a&gt; · &lt;a href="https://developer.android.com/topic/architecture/data-layer/offline-first" rel="noopener noreferrer"&gt;Android offline-first guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Which failure has bitten your app in production? Tell me in the comments.&lt;/em&gt; 👇&lt;/p&gt;




&lt;h2&gt;
  
  
  Related Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/one-codebase-two-attack-surfaces-a-practical-guide-to-securing-kotlin-multiplatform-apps-787"&gt;One Codebase, Two Attack Surfaces: A Practical Guide to Securing Kotlin Multiplatform Apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/kotlin-multiplatform-for-android-developers-share-your-business-logic-with-ktor-koin-and-clean-5hhl"&gt;Kotlin Multiplatform for Android Developers: Share Your Business Logic with Ktor, Koin and Clean Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/real-time-chat-on-android-with-socketio-kotlin-flow-and-room-offline-first-architecture-1a13"&gt;Real-Time Chat on Android with Socket.IO, Kotlin Flow and Room (Offline-First Architecture)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Padmakar Garg&lt;/strong&gt; is an Android developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture and mobile security.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Portfolio: &lt;a href="https://gargpadmakar.github.io/" rel="noopener noreferrer"&gt;gargpadmakar.github.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/gargpadmakar" rel="noopener noreferrer"&gt;github.com/gargpadmakar&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>architecture</category>
      <category>programming</category>
    </item>
    <item>
      <title>Kotlin Multiplatform in 2026: Complete Guide with Real-World Architecture, Kotlin Examples &amp; Interview Questions</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Thu, 26 Nov 2026 07:20:25 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/kotlin-multiplatform-in-2026-complete-guide-with-real-world-architecture-kotlin-examples--4i2a</link>
      <guid>https://dev.to/gargpadmakar/kotlin-multiplatform-in-2026-complete-guide-with-real-world-architecture-kotlin-examples--4i2a</guid>
      <description>&lt;p&gt;If you are an Android developer, you probably know Kotlin, Jetpack Compose, MVVM, Coroutines, Retrofit, Room, and modern Android architecture.&lt;/p&gt;

&lt;p&gt;But what if you need to build the same product for Android and iOS?&lt;/p&gt;

&lt;p&gt;Traditionally, teams maintain separate implementations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kotlin for Android.&lt;/li&gt;
&lt;li&gt;Swift for iOS.&lt;/li&gt;
&lt;li&gt;Separate networking and data layers.&lt;/li&gt;
&lt;li&gt;Separate business rules and validation.&lt;/li&gt;
&lt;li&gt;Separate database and synchronization logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This can lead to duplicated effort and inconsistent behavior. Kotlin Multiplatform (KMP) offers another approach: share suitable Kotlin code across platforms while retaining native capabilities and, when desired, native user interfaces.&lt;/p&gt;

&lt;p&gt;This guide explains KMP from an Android developer's perspective, with architecture examples, Kotlin snippets, production considerations, real-world scenarios, and interview questions.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is Kotlin Multiplatform?
&lt;/h2&gt;

&lt;p&gt;Kotlin Multiplatform is a technology for sharing Kotlin code across multiple platforms, including Android, iOS, desktop, and other supported targets.&lt;/p&gt;

&lt;p&gt;You can share:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Business rules and use cases.&lt;/li&gt;
&lt;li&gt;Networking and serialization.&lt;/li&gt;
&lt;li&gt;Repository implementations.&lt;/li&gt;
&lt;li&gt;Database access.&lt;/li&gt;
&lt;li&gt;Validation and data mapping.&lt;/li&gt;
&lt;li&gt;Authentication workflows.&lt;/li&gt;
&lt;li&gt;Offline synchronization logic.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can still write Android UI with Jetpack Compose and iOS UI with SwiftUI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The key idea: share code where it makes sense, and keep platform-specific code where it is needed.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  2. KMP vs Compose Multiplatform
&lt;/h2&gt;

&lt;p&gt;These terms are related, but they are not interchangeable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Kotlin Multiplatform (KMP)&lt;/strong&gt; provides the technology and project model for sharing Kotlin code across targets. It does not require sharing UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compose Multiplatform&lt;/strong&gt; is a declarative UI framework that lets you share UI code across supported platforms.&lt;/p&gt;

&lt;p&gt;Two common approaches are:&lt;/p&gt;

&lt;h3&gt;
  
  
  Option A: Shared business logic, native UI
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Android app                         iOS app
Jetpack Compose                     SwiftUI
      |                                |
      +---------------+----------------+
                      |
                Shared KMP
          Domain / Data / Networking
                      |
                API and Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is useful when an existing product already has native Android and iOS applications, or when each platform needs a distinct native experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Option B: Shared UI and business logic
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Android app                         iOS app
      |                                |
      +---------------+----------------+
                      |
             Compose Multiplatform
                      |
               Shared KMP logic
                      |
                API and Database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This can reduce duplicated UI work when a shared design and interaction model is appropriate.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Area&lt;/th&gt;
&lt;th&gt;KMP&lt;/th&gt;
&lt;th&gt;Compose Multiplatform&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Shared business logic&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Can use KMP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared networking&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Can use KMP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared database logic&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Can use KMP&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared UI&lt;/td&gt;
&lt;td&gt;Not required&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Native UI possible&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes, through platform integration&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; KMP is about sharing Kotlin code. Compose Multiplatform is an option for sharing UI on top of the multiplatform ecosystem.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. KMP vs Flutter vs React Native
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Kotlin Multiplatform&lt;/th&gt;
&lt;th&gt;Flutter&lt;/th&gt;
&lt;th&gt;React Native&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary language&lt;/td&gt;
&lt;td&gt;Kotlin&lt;/td&gt;
&lt;td&gt;Dart&lt;/td&gt;
&lt;td&gt;JavaScript / TypeScript&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Native UI option&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Platform integration&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Shared UI&lt;/td&gt;
&lt;td&gt;With Compose Multiplatform&lt;/td&gt;
&lt;td&gt;Flutter widgets&lt;/td&gt;
&lt;td&gt;React Native components&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Existing native app integration&lt;/td&gt;
&lt;td&gt;Incremental sharing is a core use case&lt;/td&gt;
&lt;td&gt;Possible&lt;/td&gt;
&lt;td&gt;Possible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Business logic sharing&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There is no universally best framework. Choose based on team skills, existing applications, product requirements, UI requirements, platform integrations, and long-term maintenance.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Recommended KMP Architecture
&lt;/h2&gt;

&lt;p&gt;A practical production architecture can use Presentation, Domain, Data, and Platform Integration boundaries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                Android app                 iOS app
              Jetpack Compose              SwiftUI
                    |                         |
                    +-----------+-------------+
                                |
                         Shared KMP code
                                |
              +-----------------+-----------------+
              |                 |                 |
         Presentation         Domain             Data
              |                 |                 |
         UI state          Use cases          Repositories
                                |                 |
                           Business rules     +---+------+
                                              |          |
                                            Ktor      SQLDelight
                                              |          |
                                              +----+-----+
                                                   |
                                             Platform APIs
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Presentation layer
&lt;/h3&gt;

&lt;p&gt;Responsible for screen state and user interaction. Depending on the project, presentation may be native on each platform or shared using Compose Multiplatform.&lt;/p&gt;

&lt;h3&gt;
  
  
  Domain layer
&lt;/h3&gt;

&lt;p&gt;Contains business rules, entities, repository contracts, and use cases.&lt;/p&gt;

&lt;p&gt;Examples:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;LoginUserUseCase&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GetUserProfileUseCase&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;CreateTaskUseCase&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;ValidateEmailUseCase&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SyncPendingTasksUseCase&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Keep this layer independent of Android and iOS UI frameworks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data layer
&lt;/h3&gt;

&lt;p&gt;Implements repository contracts and handles remote and local data sources, mapping, caching, and synchronization.&lt;/p&gt;

&lt;h3&gt;
  
  
  Platform integration layer
&lt;/h3&gt;

&lt;p&gt;Provides platform-specific capabilities such as secure storage, camera access, biometrics, push notifications, and file selection.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Suggested Project Structure
&lt;/h2&gt;

&lt;p&gt;For a starter project with a shared module and Compose application module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MyKmpApplication/
|
+-- composeApp/
|   +-- src/
|       +-- commonMain/
|       +-- androidMain/
|       +-- iosMain/
|
+-- shared/
|   +-- src/
|       +-- commonMain/
|       |   +-- kotlin/
|       |       +-- domain/
|       |       |   +-- model/
|       |       |   +-- repository/
|       |       |   +-- usecase/
|       |       +-- data/
|       |       |   +-- remote/
|       |       |   +-- local/
|       |       |   +-- repository/
|       |       |   +-- mapper/
|       |       +-- di/
|       |       +-- utils/
|       |
|       +-- commonTest/
|       +-- androidMain/
|       +-- iosMain/
|
+-- iosApp/
    +-- App.swift
    +-- ContentView.swift
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact template and source-set hierarchy depend on the Kotlin Gradle plugin version and configured targets.&lt;/p&gt;

&lt;h3&gt;
  
  
  Important source sets
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Source set&lt;/th&gt;
&lt;th&gt;Purpose&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;commonMain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Shared production Kotlin code&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;commonTest&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Shared tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;androidMain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Android-specific implementation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;androidUnitTest&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Android unit tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;iosMain&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;iOS-target implementation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;iosTest&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;iOS tests&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  6. &lt;code&gt;expect&lt;/code&gt; and &lt;code&gt;actual&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;Sometimes shared code needs functionality that differs by platform. Kotlin provides &lt;code&gt;expect&lt;/code&gt; and &lt;code&gt;actual&lt;/code&gt; declarations for this purpose.&lt;/p&gt;

&lt;p&gt;For example, imagine that you need the device name.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common declaration
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// commonMain&lt;/span&gt;
&lt;span class="n"&gt;expect&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeviceInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getDeviceName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Android implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// androidMain&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;android&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Build&lt;/span&gt;

&lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeviceInfo&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getDeviceName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"${Build.MANUFACTURER} ${Build.MODEL}"&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  iOS implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// iosMain&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;platform&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UIKit&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;UIDevice&lt;/span&gt;

&lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DeviceInfo&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getDeviceName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;UIDevice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;currentDevice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Shared code can now depend on the common declaration:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GetDeviceNameUseCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;deviceInfo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DeviceInfo&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;deviceInfo&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getDeviceName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use &lt;code&gt;expect&lt;/code&gt;/&lt;code&gt;actual&lt;/code&gt; when a platform-specific API genuinely needs a platform-specific implementation. For larger services, an interface plus dependency injection can offer a more flexible boundary.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Networking with Ktor Client
&lt;/h2&gt;

&lt;p&gt;Ktor Client lets you write shared HTTP client and API code in Kotlin.&lt;/p&gt;

&lt;h3&gt;
  
  
  Data model
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;kotlinx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;serialization&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Serializable&lt;/span&gt;

&lt;span class="nd"&gt;@Serializable&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserDto&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Create the client
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createHttpClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpClientEngine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nc"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;install&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ContentNegotiation&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="nc"&gt;Json&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;ignoreUnknownKeys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
                    &lt;span class="n"&gt;isLenient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;expectSuccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  API service
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserApi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserDto&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"https://api.example.com/users"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;body&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Repository contract and implementation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserDto&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserRepositoryImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserApi&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserDto&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use case
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;GetUsersUseCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUsers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ktor uses a platform-appropriate HTTP engine. Common choices include OkHttp or Android on Android, and Darwin on Apple platforms.&lt;/p&gt;

&lt;p&gt;Production networking should also consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Connection and request timeouts.&lt;/li&gt;
&lt;li&gt;Authentication and token refresh.&lt;/li&gt;
&lt;li&gt;Structured error mapping.&lt;/li&gt;
&lt;li&gt;Cancellation.&lt;/li&gt;
&lt;li&gt;Retry policy and idempotency.&lt;/li&gt;
&lt;li&gt;Serialization failures.&lt;/li&gt;
&lt;li&gt;Secure transport and certificate validation.&lt;/li&gt;
&lt;li&gt;Safe logging that never exposes credentials or sensitive data.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  8. Dependency Injection with Koin
&lt;/h2&gt;

&lt;p&gt;Koin is a Kotlin-first dependency injection framework commonly used in KMP applications.&lt;/p&gt;

&lt;p&gt;A simplified shared module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;sharedModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;single&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;UserApi&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;UserRepository&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;UserRepositoryImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;GetUsersUseCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Provide the HTTP engine from each platform:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// androidMain&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;androidModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;HttpClientEngine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;OkHttp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// iosMain&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;iosModule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;module&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;single&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;HttpClientEngine&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Darwin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize Koin with the common and platform modules during application startup. The exact startup integration depends on whether the UI is native or shared.&lt;/p&gt;

&lt;p&gt;Why use dependency injection?&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Dependencies are explicit.&lt;/li&gt;
&lt;li&gt;Implementations can be replaced in tests.&lt;/li&gt;
&lt;li&gt;Platform-specific implementations can be supplied at the platform boundary.&lt;/li&gt;
&lt;li&gt;Object creation is centralized.&lt;/li&gt;
&lt;li&gt;Modules can be organized by feature.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Koin is not the only option. Choose a DI approach that fits the project's scale and team experience.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Database with SQLDelight
&lt;/h2&gt;

&lt;p&gt;SQLDelight generates type-safe Kotlin APIs from SQL statements and supports multiplatform database drivers.&lt;/p&gt;

&lt;p&gt;Example schema:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="nc"&gt;TaskEntity&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt; &lt;span class="kt"&gt;TEXT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;is_completed&lt;/span&gt; &lt;span class="kt"&gt;INTEGER&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="kc"&gt;NULL&lt;/span&gt; &lt;span class="k"&gt;DEFAULT&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;selectAllTasks&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="nc"&gt;TaskEntity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;insertTask&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;OR&lt;/span&gt; &lt;span class="k"&gt;REPLACE&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="nc"&gt;TaskEntity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;is_completed&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simplified data source:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TaskLocalDataSource&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AppDatabase&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getTasks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskQueries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;selectAllTasks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;insertTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;isCompleted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;database&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;taskQueries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insertTask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;description&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;isCompleted&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The generated query API depends on the SQLDelight schema and configuration.&lt;/p&gt;

&lt;p&gt;For production, also consider migrations, indexes, transactions, database driver setup, and test databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  10. Offline-First Architecture
&lt;/h2&gt;

&lt;p&gt;Offline-first behavior is a valuable real-world KMP use case.&lt;/p&gt;

&lt;p&gt;Imagine an employee creates a task while working in an area with no internet connection.&lt;/p&gt;

&lt;p&gt;A robust application should not force the user to wait for the server before seeing the task.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User creates task
       |
       v
Repository
       |
       v
Local database
       |
       +------&amp;gt; UI observes local data
       |
       v
Pending sync operation
       |
       v
Network becomes available
       |
       v
Remote API
       |
       v
Conflict / response handling
       |
       v
Update local database
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A practical synchronization strategy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Save the change locally.&lt;/li&gt;
&lt;li&gt;Display the local result immediately.&lt;/li&gt;
&lt;li&gt;Record a pending operation with a unique ID.&lt;/li&gt;
&lt;li&gt;Attempt synchronization when appropriate.&lt;/li&gt;
&lt;li&gt;Retry transient failures with bounded backoff.&lt;/li&gt;
&lt;li&gt;Make server operations idempotent where possible.&lt;/li&gt;
&lt;li&gt;Resolve conflicts using an explicit product policy.&lt;/li&gt;
&lt;li&gt;Update local synchronization status.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Real-world problem: Duplicate requests
&lt;/h3&gt;

&lt;p&gt;A user taps Create twice because the network is slow. The client may submit two requests.&lt;/p&gt;

&lt;p&gt;Possible protections:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Disable or debounce repeated UI actions where appropriate.&lt;/li&gt;
&lt;li&gt;Assign a unique operation ID.&lt;/li&gt;
&lt;li&gt;Use backend idempotency keys.&lt;/li&gt;
&lt;li&gt;Track pending operations.&lt;/li&gt;
&lt;li&gt;Make retry behavior safe.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Network availability does not guarantee that a request will succeed.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Android and iOS Interoperability
&lt;/h2&gt;

&lt;p&gt;Android can consume shared Kotlin classes through the shared module.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello from shared Kotlin"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Android usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On iOS, shared Kotlin code can be exposed through a framework or another supported integration mechanism.&lt;/p&gt;

&lt;p&gt;A simplified Swift usage example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;greeting&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Greeting&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;greeting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The exact Swift-facing API depends on framework configuration and exported declarations.&lt;/p&gt;

&lt;p&gt;Pay attention to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Public API design.&lt;/li&gt;
&lt;li&gt;Kotlin and Swift type mapping.&lt;/li&gt;
&lt;li&gt;Nullability.&lt;/li&gt;
&lt;li&gt;Exception handling.&lt;/li&gt;
&lt;li&gt;Coroutine and Flow interoperability.&lt;/li&gt;
&lt;li&gt;Framework size and build integration.&lt;/li&gt;
&lt;li&gt;Binary compatibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Coroutines and Swift concurrency
&lt;/h3&gt;

&lt;p&gt;Shared Kotlin code can expose &lt;code&gt;suspend&lt;/code&gt; functions and &lt;code&gt;Flow&lt;/code&gt;, but Swift consumption requires an interoperability strategy.&lt;/p&gt;

&lt;p&gt;Possible approaches include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Kotlin/Native's supported interop.&lt;/li&gt;
&lt;li&gt;Swift-facing wrapper APIs.&lt;/li&gt;
&lt;li&gt;A compatible coroutine/Flow bridging library.&lt;/li&gt;
&lt;li&gt;SKIE or another compatible integration tool.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do not assume Kotlin &lt;code&gt;Flow&lt;/code&gt; automatically behaves like Swift &lt;code&gt;AsyncSequence&lt;/code&gt; without a bridge.&lt;/p&gt;

&lt;h2&gt;
  
  
  12. Native UI vs Shared UI
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Native UI
&lt;/h3&gt;

&lt;p&gt;Android:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;HomeScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Welcome Android"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;iOS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;HomeView&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;some&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Welcome iOS"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both platforms can use the same domain and data logic while maintaining platform-specific UI.&lt;/p&gt;

&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native navigation and interaction patterns.&lt;/li&gt;
&lt;li&gt;Platform-specific accessibility behavior.&lt;/li&gt;
&lt;li&gt;Straightforward integration with existing native applications.&lt;/li&gt;
&lt;li&gt;Freedom to tailor each platform's UX.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trade-off: UI code and some presentation logic may be duplicated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shared UI with Compose Multiplatform
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;WelcomeScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Column&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Welcome, $username"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Continue"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shared UI components.&lt;/li&gt;
&lt;li&gt;Shared design system.&lt;/li&gt;
&lt;li&gt;Less duplicated UI implementation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native integrations still need platform work.&lt;/li&gt;
&lt;li&gt;Platform-specific behavior and accessibility need testing.&lt;/li&gt;
&lt;li&gt;Shared UI may not be the right choice for every existing product.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Choose based on product requirements, not simply the desire to maximize code sharing.&lt;/p&gt;

&lt;h2&gt;
  
  
  13. Security in KMP
&lt;/h2&gt;

&lt;p&gt;KMP does not automatically make an application secure. Shared code and platform-specific code both need security reviews.&lt;/p&gt;

&lt;p&gt;For authentication, avoid storing sensitive tokens in plain preferences or ordinary database fields without appropriate protection.&lt;/p&gt;

&lt;p&gt;A useful architecture is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Shared authentication repository
              |
       SecureStorage interface
              |
       +------+------+
       |             |
    Android          iOS
    Keystore         Keychain
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Common contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;saveToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;clearToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each platform provides a secure implementation appropriate to its security model.&lt;/p&gt;

&lt;p&gt;Security checklist:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use Android Keystore-backed protection where appropriate.&lt;/li&gt;
&lt;li&gt;Use iOS Keychain for sensitive credentials.&lt;/li&gt;
&lt;li&gt;Never hardcode private API secrets in the client.&lt;/li&gt;
&lt;li&gt;Never log access tokens or refresh tokens.&lt;/li&gt;
&lt;li&gt;Use secure network transport.&lt;/li&gt;
&lt;li&gt;Validate authorization on the backend.&lt;/li&gt;
&lt;li&gt;Handle refresh-token rotation and invalidation.&lt;/li&gt;
&lt;li&gt;Protect sensitive local data.&lt;/li&gt;
&lt;li&gt;Review root/jailbreak threat models where relevant.&lt;/li&gt;
&lt;li&gt;Keep platform-specific security integrations behind clear interfaces.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  14. Testing KMP Applications
&lt;/h2&gt;

&lt;p&gt;Shared tests help verify business logic consistently across targets.&lt;/p&gt;

&lt;p&gt;Example validator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailValidator&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"@"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tests:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailValidatorTest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;validator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;EmailValidator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;validEmailReturnsTrue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;assertTrue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;validator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"test@example.com"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Test&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;invalidEmailReturnsFalse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;assertFalse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;validator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isValid&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"invalid-email"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is intentionally a simplified validation example; real email validation should follow the application's requirements.&lt;/p&gt;

&lt;h3&gt;
  
  
  Testing strategy
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Layer&lt;/th&gt;
&lt;th&gt;Testing approach&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Domain&lt;/td&gt;
&lt;td&gt;Common unit tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use cases&lt;/td&gt;
&lt;td&gt;Unit tests with fakes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Repository&lt;/td&gt;
&lt;td&gt;Integration tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ktor&lt;/td&gt;
&lt;td&gt;MockEngine tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Database&lt;/td&gt;
&lt;td&gt;SQLDelight tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Android UI&lt;/td&gt;
&lt;td&gt;Compose UI tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iOS UI&lt;/td&gt;
&lt;td&gt;XCTest / SwiftUI tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Platform integration&lt;/td&gt;
&lt;td&gt;Android and iOS integration tests&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Passing common tests does not guarantee that the iOS application works correctly. Test framework integration, native APIs, database drivers, coroutine bridging, permissions, and real-device behavior too.&lt;/p&gt;

&lt;h2&gt;
  
  
  15. Performance Considerations
&lt;/h2&gt;

&lt;p&gt;KMP does not automatically make an application faster or slower. Performance depends on implementation and platform integration.&lt;/p&gt;

&lt;p&gt;Consider:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Database indexes and query efficiency.&lt;/li&gt;
&lt;li&gt;Pagination and caching.&lt;/li&gt;
&lt;li&gt;Network timeouts and request duplication.&lt;/li&gt;
&lt;li&gt;Application startup work.&lt;/li&gt;
&lt;li&gt;Memory ownership across platform boundaries.&lt;/li&gt;
&lt;li&gt;Coroutine cancellation and structured concurrency.&lt;/li&gt;
&lt;li&gt;Framework and dependency size.&lt;/li&gt;
&lt;li&gt;Release-build benchmarks on actual devices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Measure before optimizing. Test Android and iOS separately because platform behavior and bottlenecks can differ.&lt;/p&gt;

&lt;h2&gt;
  
  
  16. Real-World Project: Enterprise Task Management App
&lt;/h2&gt;

&lt;p&gt;Imagine building a task management application for Android and iOS.&lt;/p&gt;

&lt;p&gt;Requirements:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login and OTP verification.&lt;/li&gt;
&lt;li&gt;Role-based access.&lt;/li&gt;
&lt;li&gt;Task creation and assignment.&lt;/li&gt;
&lt;li&gt;Task status and priority.&lt;/li&gt;
&lt;li&gt;Offline task management.&lt;/li&gt;
&lt;li&gt;Push notifications.&lt;/li&gt;
&lt;li&gt;Image and document uploads.&lt;/li&gt;
&lt;li&gt;Real-time updates.&lt;/li&gt;
&lt;li&gt;Secure token storage.&lt;/li&gt;
&lt;li&gt;Synchronization across devices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A possible architecture:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                 ANDROID APP
                Jetpack Compose
                      |
                      |
                   SHARED KMP
                      |
          +-----------+-----------+
          |           |           |
        Domain       Data     Shared state
          |           |
       Use cases   Repository
                      |
                +-----+------+
                |            |
               Ktor      SQLDelight
                |            |
                +-----+------+
                      |
               Platform APIs
                /         \
           Android         iOS
           Keystore       Keychain
           FCM             APNs
           Camera          Camera
           Permissions     Permissions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A feature-oriented structure could be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;shared/
|
+-- core/
|   +-- network/
|   +-- database/
|   +-- security/
|   +-- logging/
|
+-- feature-auth/
|   +-- domain/
|   +-- data/
|
+-- feature-task/
|   +-- domain/
|   +-- data/
|
+-- feature-profile/
|   +-- domain/
|   +-- data/
|
+-- feature-notification/
|
+-- platform/
    +-- android/
    +-- ios/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Start with a single shared module if the project is small. Introduce more Gradle modules when they provide meaningful dependency boundaries, build improvements, or team ownership.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenario A: User login
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Login screen
    |
Login presentation logic
    |
Login use case
    |
Authentication repository
    |
Ktor API
    |
Backend authentication
    |
Tokens returned
    |
Platform secure storage
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Handle input validation, network failures, token expiry, refresh coordination, logout, and credential invalidation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenario B: Offline task creation
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;Save the task locally.&lt;/li&gt;
&lt;li&gt;Display it immediately.&lt;/li&gt;
&lt;li&gt;Mark it as pending synchronization.&lt;/li&gt;
&lt;li&gt;Detect suitable opportunities to sync.&lt;/li&gt;
&lt;li&gt;Upload it to the backend.&lt;/li&gt;
&lt;li&gt;Handle server conflicts.&lt;/li&gt;
&lt;li&gt;Update the local record.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Scenario C: Push notifications
&lt;/h3&gt;

&lt;p&gt;Android commonly uses Firebase Cloud Messaging. iOS uses APNs, potentially through Firebase Messaging integration.&lt;/p&gt;

&lt;p&gt;Share notification models, business rules, and navigation decisions where appropriate. Keep token registration, permission prompts, and platform notification handling in platform-specific code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenario D: Document upload
&lt;/h3&gt;

&lt;p&gt;Share validation rules, upload contracts, repository logic, progress state, and error mapping. Keep file picking and platform-specific file access in native integrations.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenario E: Real-time updates
&lt;/h3&gt;

&lt;p&gt;You can share message models, business event processing, and repository logic. However, connection lifecycle, background execution, and platform restrictions must be handled carefully on each platform.&lt;/p&gt;

&lt;h2&gt;
  
  
  17. Common KMP Mistakes
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mistake&lt;/th&gt;
&lt;th&gt;Better approach&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Trying to share every line of code&lt;/td&gt;
&lt;td&gt;Share logic that is genuinely common&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assuming KMP requires shared UI&lt;/td&gt;
&lt;td&gt;Choose native or shared UI deliberately&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Putting API calls directly in UI&lt;/td&gt;
&lt;td&gt;Use use cases and repositories&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Using &lt;code&gt;expect&lt;/code&gt;/&lt;code&gt;actual&lt;/code&gt; for everything&lt;/td&gt;
&lt;td&gt;Prefer common abstractions where practical&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ignoring iOS integration&lt;/td&gt;
&lt;td&gt;Test Swift interoperability early&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Storing tokens insecurely&lt;/td&gt;
&lt;td&gt;Use platform-secure storage&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ignoring offline scenarios&lt;/td&gt;
&lt;td&gt;Design synchronization explicitly&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assuming common tests are sufficient&lt;/td&gt;
&lt;td&gt;Add platform integration tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Over-modularizing from day one&lt;/td&gt;
&lt;td&gt;Extract modules when boundaries justify them&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Ignoring lifecycle and background restrictions&lt;/td&gt;
&lt;td&gt;Integrate with native platform lifecycle rules&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h1&gt;
  
  
  18. Kotlin Multiplatform Interview Questions and Answers
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Beginner-level questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. What is Kotlin Multiplatform?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;KMP is a technology that allows Kotlin code to be shared across multiple platforms, including Android and iOS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What is the main purpose of KMP?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To reduce duplicated code by sharing common business logic and other suitable functionality while retaining platform-specific capabilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. What is the difference between KMP and Compose Multiplatform?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;KMP enables Kotlin code sharing. Compose Multiplatform enables shared declarative UI on supported platforms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Can we use native UI with KMP?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. Android can use Jetpack Compose and iOS can use SwiftUI while both consume shared Kotlin logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. What is &lt;code&gt;commonMain&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The source set containing shared production Kotlin code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. What is &lt;code&gt;androidMain&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The source set for Android-specific implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. What is &lt;code&gt;iosMain&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The source set used for configured iOS target implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. What are &lt;code&gt;expect&lt;/code&gt; and &lt;code&gt;actual&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;expect&lt;/code&gt; declares a common API requirement, and &lt;code&gt;actual&lt;/code&gt; provides the platform-specific implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. Can KMP share database logic?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. Libraries such as SQLDelight support multiplatform database access.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. Can KMP share networking?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. Ktor Client is commonly used to write shared networking code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Intermediate-level questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;11. What is the difference between KMP and Flutter?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;KMP focuses on sharing Kotlin code and can retain native UI. Flutter uses Dart and its own UI framework. KMP can also use Compose Multiplatform for shared UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;12. How does KMP communicate with iOS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Kotlin/Native can expose shared Kotlin code through supported framework integration and Objective-C/Swift interoperability.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13. What is Kotlin/Native?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is the Kotlin compiler technology that compiles Kotlin code to native binaries for supported targets, including Apple platforms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14. Can we use Kotlin Coroutines in iOS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. Shared Kotlin code can use coroutines, but Swift consumption of asynchronous APIs needs an appropriate interoperability strategy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;15. What is Koin?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Kotlin-first dependency injection framework often used to define shared and platform-specific dependencies in KMP projects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;16. What is Ktor?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Kotlin framework for client and server development. Ktor Client is frequently used for shared HTTP networking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;17. What is SQLDelight?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A library that generates type-safe Kotlin APIs from SQL statements and supports multiplatform database implementations.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;18. How do you handle platform-specific APIs?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;expect&lt;/code&gt;/&lt;code&gt;actual&lt;/code&gt;, interfaces with platform implementations, or dependency injection.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;19. Can Android and iOS use different UI implementations with shared business logic?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. This is a common KMP architecture.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;20. What are the benefits of KMP over two independent implementations?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It can reduce duplicated business logic, improve consistency, and simplify maintenance of shared functionality.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced-level questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;21. How do you design Clean Architecture in KMP?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Separate presentation, domain, and data responsibilities. Keep domain logic independent of platform frameworks and inject platform implementations through abstractions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;22. How do you handle secure token storage?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use a shared secure-storage contract with platform-specific implementations backed by Android Keystore and iOS Keychain.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;23. How do you implement offline-first architecture?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use a local database as a source of truth, record pending operations, synchronize with the backend, and handle conflicts and retries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;24. How do you manage dependency injection across platforms?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Define common dependency modules and supply platform-specific dependencies through platform modules.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25. How do you test shared KMP code?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use &lt;code&gt;commonTest&lt;/code&gt; for common logic and platform-specific tests for native integration.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;26. How does iOS consume a Kotlin shared framework?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Shared Kotlin code can be compiled and exposed through an iOS-compatible framework or another supported integration mechanism. Swift consumes the exported API.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;27. What is the difference between a shared repository and a platform-specific repository?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A shared repository implements common data access and business rules. A platform-specific implementation is appropriate when data access depends on native APIs or platform-specific behavior.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;28. How do you manage exceptions across Kotlin and Swift?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Define clear error contracts, handle exceptions at interop boundaries, and avoid exposing implementation-specific exceptions unnecessarily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;29. How do you manage Flow in iOS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use an appropriate Flow-to-Swift integration strategy, such as a compatible bridge or Swift-facing wrapper. Manage collection and cancellation according to the iOS lifecycle.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;30. How do you optimize KMP framework size?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Review dependencies, exported APIs, compiler configuration, and unnecessary framework exposure. Measure release binaries.&lt;/p&gt;

&lt;h2&gt;
  
  
  Senior-level scenario-based questions
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;31. You have an existing Android application and want to migrate it to KMP. How would you approach it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I would avoid rewriting the whole application.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Analyze the existing architecture.&lt;/li&gt;
&lt;li&gt;Identify reusable business logic.&lt;/li&gt;
&lt;li&gt;Extract pure Kotlin domain models and use cases.&lt;/li&gt;
&lt;li&gt;Introduce shared repository contracts.&lt;/li&gt;
&lt;li&gt;Move suitable networking and data logic into shared code.&lt;/li&gt;
&lt;li&gt;Keep platform-specific functionality behind abstractions.&lt;/li&gt;
&lt;li&gt;Integrate the shared module with iOS.&lt;/li&gt;
&lt;li&gt;Add common and platform-specific tests.&lt;/li&gt;
&lt;li&gt;Migrate features incrementally.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The objective is to reduce migration risk while keeping the existing Android application stable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;32. Android works, but iOS crashes when calling a shared Kotlin API. What would you investigate?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I would investigate framework integration, Swift interoperability, nullability mapping, exception propagation, coroutine bridging, native dependencies, platform-specific implementations, and binary compatibility. I would reproduce the problem in an iOS test environment and inspect native logs.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;33. Your application has 100 APIs. Should every API implementation move into &lt;code&gt;commonMain&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not automatically. I would identify which networking contracts, models, authentication rules, and repository behaviors are common. Native SDK integrations and platform-specific capabilities may remain platform-specific.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;34. How would you ensure consistent offline behavior on Android and iOS?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Share data models, business rules, repository contracts, synchronization policies, and conflict resolution. Use a local source of truth, idempotent operations, and tests for concurrent updates and network interruption.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;35. Your shared module has become too large. What would you do?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Organize code around business features and technical responsibilities. Introduce Gradle modules when they improve dependency boundaries, build performance, or team ownership.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;36. Should networking, database, UI, and business logic all be shared?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not necessarily. Share code according to product requirements. For an existing native application, start with domain and data logic. Evaluate shared UI separately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;37. A refresh token expires while multiple API requests are running. How would you handle it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Centralize authentication handling in the networking layer. Coordinate refresh so concurrent requests do not independently refresh the token, retry original requests only when safe, handle refresh-token expiry, clear invalid credentials, and return an authentication-required state to the UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;38. How would you design a KMP library for multiple applications?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Define a stable public API, keep implementation details internal, minimize platform-specific assumptions, document supported targets, provide tests, manage semantic versioning, and verify binary compatibility.&lt;/p&gt;

&lt;h1&gt;
  
  
  19. Learning Roadmap: Android Developer to KMP Engineer
&lt;/h1&gt;

&lt;p&gt;If you already know Android development, you do not need to start from zero.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Phase&lt;/th&gt;
&lt;th&gt;Topics&lt;/th&gt;
&lt;th&gt;Practical exercise&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;KMP fundamentals and source sets&lt;/td&gt;
&lt;td&gt;Create Android + iOS shared module&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;expect&lt;/code&gt;/&lt;code&gt;actual&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Implement device information&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;Ktor&lt;/td&gt;
&lt;td&gt;Build a shared REST API client&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;Koin&lt;/td&gt;
&lt;td&gt;Configure shared dependency injection&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;5&lt;/td&gt;
&lt;td&gt;SQLDelight&lt;/td&gt;
&lt;td&gt;Build a shared local database&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;6&lt;/td&gt;
&lt;td&gt;Coroutines and Flow&lt;/td&gt;
&lt;td&gt;Expose shared asynchronous state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;7&lt;/td&gt;
&lt;td&gt;Clean Architecture&lt;/td&gt;
&lt;td&gt;Implement shared use cases and repositories&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;Native integration&lt;/td&gt;
&lt;td&gt;Call shared Kotlin from Swift&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;9&lt;/td&gt;
&lt;td&gt;Security&lt;/td&gt;
&lt;td&gt;Integrate Keystore and Keychain&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;Testing&lt;/td&gt;
&lt;td&gt;Add common and platform integration tests&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;Compose Multiplatform&lt;/td&gt;
&lt;td&gt;Build a shared UI feature&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;12&lt;/td&gt;
&lt;td&gt;Advanced KMP&lt;/td&gt;
&lt;td&gt;Modularization, CI/CD, and performance&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  20. Practical Project: Build a KMP Task Management Application
&lt;/h2&gt;

&lt;p&gt;The best way to learn KMP is to build a real application rather than only reading documentation.&lt;/p&gt;

&lt;p&gt;Suggested features:&lt;/p&gt;

&lt;h3&gt;
  
  
  Authentication
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Login and registration.&lt;/li&gt;
&lt;li&gt;OTP verification.&lt;/li&gt;
&lt;li&gt;Access and refresh tokens.&lt;/li&gt;
&lt;li&gt;Secure token storage.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Task management
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create and update tasks.&lt;/li&gt;
&lt;li&gt;Assign tasks to users.&lt;/li&gt;
&lt;li&gt;Update task status.&lt;/li&gt;
&lt;li&gt;Manage task priority.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Offline support
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;SQLDelight.&lt;/li&gt;
&lt;li&gt;Local caching.&lt;/li&gt;
&lt;li&gt;Pending synchronization.&lt;/li&gt;
&lt;li&gt;Retry mechanism.&lt;/li&gt;
&lt;li&gt;Conflict resolution.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Architecture
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;MVVM.&lt;/li&gt;
&lt;li&gt;Clean Architecture.&lt;/li&gt;
&lt;li&gt;Use Cases.&lt;/li&gt;
&lt;li&gt;Repository Pattern.&lt;/li&gt;
&lt;li&gt;Koin.&lt;/li&gt;
&lt;li&gt;Ktor.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Platforms
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Android: Jetpack Compose.&lt;/li&gt;
&lt;li&gt;iOS: SwiftUI initially.&lt;/li&gt;
&lt;li&gt;Optional shared UI: Compose Multiplatform.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Testing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Common unit tests.&lt;/li&gt;
&lt;li&gt;Repository tests.&lt;/li&gt;
&lt;li&gt;ViewModel tests.&lt;/li&gt;
&lt;li&gt;Android integration tests.&lt;/li&gt;
&lt;li&gt;iOS integration tests.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Build one feature end-to-end before adding more features. For example, implement login across shared domain/data code and both native UIs, then add offline task creation.&lt;/p&gt;

&lt;h1&gt;
  
  
  21. Final Thoughts
&lt;/h1&gt;

&lt;p&gt;Kotlin Multiplatform is valuable because it gives Kotlin developers a way to share business logic across platforms without abandoning native development.&lt;/p&gt;

&lt;p&gt;The biggest advantage is not simply writing fewer lines of code. It is creating consistent business behavior across platforms while preserving the capabilities and user experience each platform needs.&lt;/p&gt;

&lt;p&gt;For Android developers, a practical learning path is to start by sharing domain logic, networking, repositories, and database operations. Once you understand the architecture and native integration, explore Compose Multiplatform.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember: The goal of KMP is not to make Android and iOS identical. The goal is to share what should be shared and preserve what should remain platform-specific.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Related Reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/one-codebase-two-attack-surfaces-a-practical-guide-to-securing-kotlin-multiplatform-apps-787"&gt;One Codebase, Two Attack Surfaces: A Practical Guide to Securing Kotlin Multiplatform Apps&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/kotlin-multiplatform-for-android-developers-share-your-business-logic-with-ktor-koin-and-clean-5hhl"&gt;Kotlin Multiplatform for Android Developers: Share Your Business Logic with Ktor, Koin and Clean Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/real-time-chat-on-android-with-socket"&gt;Real-Time Chat on Android with Socket.IO&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  About the Author
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Padmakar Garg&lt;/strong&gt; is an Android Developer with 8+ years of experience in Kotlin, Java, Jetpack Compose, Kotlin Multiplatform, scalable architecture, mobile security, and high-performance app development.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Portfolio: &lt;a href="https://gargpadmakar.github.io/" rel="noopener noreferrer"&gt;gargpadmakar.github.io&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/gargpadmakar" rel="noopener noreferrer"&gt;github.com/gargpadmakar&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>kotlin</category>
      <category>android</category>
      <category>ios</category>
      <category>multiplatform</category>
    </item>
    <item>
      <title>Android Activity and Fragment Lifecycle: Real-World Examples and Interview Questions</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Thu, 26 Nov 2026 06:48:08 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/android-activity-and-fragment-lifecycle-real-world-examples-and-interview-questions-5e8d</link>
      <guid>https://dev.to/gargpadmakar/android-activity-and-fragment-lifecycle-real-world-examples-and-interview-questions-5e8d</guid>
      <description>&lt;p&gt;Almost every Android interview has a lifecycle question, and almost every production crash report has a lifecycle bug somewhere in it. The callbacks themselves are easy to memorize. What trips people up is knowing which callback to use for which job, and what happens when the system kills your process while the user is away.&lt;/p&gt;

&lt;p&gt;This guide covers the Activity and Fragment lifecycle the way it shows up in real apps: rotation, process death, back stack, view binding leaks and collecting flows safely. It ends with interview questions and answers.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Activity lifecycle in one table
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Callback&lt;/th&gt;
&lt;th&gt;Called when&lt;/th&gt;
&lt;th&gt;Use it for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onCreate()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Activity is first created&lt;/td&gt;
&lt;td&gt;Inflate UI, set up ViewModel, restore saved state&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onStart()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Activity becomes visible&lt;/td&gt;
&lt;td&gt;Start things that should run while visible&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onResume()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Activity is in the foreground and interactive&lt;/td&gt;
&lt;td&gt;Camera, sensors, anything exclusive&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onPause()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Another activity is taking focus&lt;/td&gt;
&lt;td&gt;Release exclusive resources, keep it fast&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onStop()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Activity is no longer visible&lt;/td&gt;
&lt;td&gt;Stop updates, save drafts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onDestroy()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Activity is finishing or being recreated&lt;/td&gt;
&lt;td&gt;Final cleanup (not guaranteed on process death)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;onRestart()&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Coming back from stopped state&lt;/td&gt;
&lt;td&gt;Rarely needed&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Two rules that save a lot of pain:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Pair your callbacks.&lt;/strong&gt; Whatever you start in &lt;code&gt;onStart()&lt;/code&gt;, stop in &lt;code&gt;onStop()&lt;/code&gt;. Whatever you acquire in &lt;code&gt;onResume()&lt;/code&gt;, release in &lt;code&gt;onPause()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;onDestroy()&lt;/code&gt; is not a safe place to save anything.&lt;/strong&gt; If the system kills your process for memory, it does not call it.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  What actually happens on rotation
&lt;/h2&gt;

&lt;p&gt;By default, a configuration change (rotation, dark mode switch, language change, font size) destroys the Activity and creates a new one. The sequence looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onPause -&amp;gt; onStop -&amp;gt; onSaveInstanceState -&amp;gt; onDestroy
-&amp;gt; onCreate (new instance) -&amp;gt; onStart -&amp;gt; onRestoreInstanceState -&amp;gt; onResume
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Everything in the old Activity instance is gone: fields, views, listeners. This is where the classic bug comes from.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-world bug 1: the form that clears itself
&lt;/h3&gt;

&lt;p&gt;A user fills in a long signup form, rotates the phone, and every field is empty.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bad: state lives in the Activity and dies with it&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SignupActivity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AppCompatActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;draftEmail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="c1"&gt;// rotate the phone and draftEmail is "" again&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fix it with a &lt;code&gt;ViewModel&lt;/code&gt;, which survives configuration changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SignupViewModel&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;_state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SignupState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;SignupState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onEmailChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SignupState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SignupActivity&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AppCompatActivity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SignupViewModel&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;viewModels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onCreate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nf"&gt;setContentView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activity_signup&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;lifecycleScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;repeatOnLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Lifecycle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STARTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;ViewModel&lt;/code&gt; survives rotation, but it does &lt;strong&gt;not&lt;/strong&gt; survive process death. That leads to the next problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Process death: the one most developers miss
&lt;/h2&gt;

&lt;p&gt;When the user leaves your app and the system needs memory, it can kill your whole process. When the user comes back, Android recreates the Activity and restores the back stack, but your &lt;code&gt;ViewModel&lt;/code&gt; is brand new and your singletons are empty.&lt;/p&gt;

&lt;p&gt;You will not see this while testing normally. To reproduce it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Open your app and navigate to a deep screen.&lt;/li&gt;
&lt;li&gt;Press Home.&lt;/li&gt;
&lt;li&gt;Run &lt;code&gt;adb shell am kill your.package.name&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Open the app again from recents.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Or turn on &lt;strong&gt;Don't keep activities&lt;/strong&gt; in Developer options.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-world bug 2: the checkout screen that crashes on return
&lt;/h3&gt;

&lt;p&gt;A checkout screen reads the cart from an in-memory singleton. The user switches to a banking app to copy an OTP, Android kills your process, and the user returns to a checkout screen with an empty cart and a &lt;code&gt;NullPointerException&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The fix is to keep small, essential state in &lt;code&gt;SavedStateHandle&lt;/code&gt; and keep real data in a persistent store:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CheckoutViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;savedState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SavedStateHandle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cartRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CartRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="c1"&gt;// survives both rotation and process death&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;selectedAddressId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
        &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;savedState&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"address_id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;savedState&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"address_id"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// reloaded from Room/disk, not from a singleton&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cart&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cartRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observeCart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stateIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SharingStarted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;WhileSubscribed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5_000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simple rule: &lt;strong&gt;IDs and small UI choices go in &lt;code&gt;SavedStateHandle&lt;/code&gt;; real data comes from a database or network.&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Choosing the right callback for the job
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Real-world example: camera and video
&lt;/h3&gt;

&lt;p&gt;Camera is an exclusive resource. Open it in &lt;code&gt;onResume()&lt;/code&gt; and release it in &lt;code&gt;onPause()&lt;/code&gt;, so another app can use it the moment yours loses focus.&lt;/p&gt;

&lt;p&gt;With CameraX you rarely do this by hand, because &lt;code&gt;bindToLifecycle()&lt;/code&gt; ties the camera to the lifecycle owner for you:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;cameraProviderFuture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;provider&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cameraProviderFuture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;preview&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Preview&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;also&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setSurfaceProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;previewView&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;surfaceProvider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unbindAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bindToLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nc"&gt;CameraSelector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DEFAULT_BACK_CAMERA&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;preview&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ContextCompat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMainExecutor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;requireContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For video playback with ExoPlayer, the common advice is to create the player in &lt;code&gt;onStart()&lt;/code&gt; and release it in &lt;code&gt;onStop()&lt;/code&gt; on API 24+, because split-screen and multi-window mean your Activity can be visible without being in the resumed state.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-world example: location updates
&lt;/h3&gt;

&lt;p&gt;Do not leave location updates running after the screen is gone. It drains battery and Play Store vitals will flag you.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;fusedClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;requestLocationUpdates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Looper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMainLooper&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onStop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;fusedClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeLocationUpdates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onStop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Fragment lifecycle: two lifecycles, not one
&lt;/h2&gt;

&lt;p&gt;This is the single most important thing to understand about Fragments, and the most common interview trap.&lt;/p&gt;

&lt;p&gt;A Fragment has its &lt;strong&gt;own lifecycle&lt;/strong&gt; and a separate &lt;strong&gt;view lifecycle&lt;/strong&gt;. The Fragment instance can stay alive (for example on the back stack) while its view is destroyed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;onAttach -&amp;gt; onCreate -&amp;gt; onCreateView -&amp;gt; onViewCreated -&amp;gt; onViewStateRestored
-&amp;gt; onStart -&amp;gt; onResume
-&amp;gt; onPause -&amp;gt; onStop -&amp;gt; onDestroyView -&amp;gt; onDestroy -&amp;gt; onDetach
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you navigate forward and the old Fragment goes onto the back stack, it goes through &lt;code&gt;onDestroyView()&lt;/code&gt; but &lt;strong&gt;not&lt;/strong&gt; &lt;code&gt;onDestroy()&lt;/code&gt;. Its view is gone, its instance and ViewModel are still there. When the user presses back, &lt;code&gt;onCreateView()&lt;/code&gt; runs again on the same Fragment instance.&lt;/p&gt;

&lt;h3&gt;
  
  
  Real-world bug 3: the ViewBinding memory leak
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bad: binding outlives the view&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProfileFragment&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fragment_profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;lateinit&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;binding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;FragmentProfileBinding&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onViewCreated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;binding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FragmentProfileBinding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When this Fragment sits on the back stack, &lt;code&gt;binding&lt;/code&gt; still references the destroyed view hierarchy. That keeps the whole view tree, and the Activity context behind it, in memory. LeakCanary will flag it.&lt;/p&gt;

&lt;p&gt;The standard fix is to null the binding in &lt;code&gt;onDestroyView()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProfileFragment&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;R&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;layout&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;fragment_profile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;_binding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;FragmentProfileBinding&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;binding&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_binding&lt;/span&gt;&lt;span class="o"&gt;!!&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onViewCreated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_binding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FragmentProfileBinding&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onDestroyView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;_binding&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
        &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;onDestroyView&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Real-world bug 4: duplicate observers and double events
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Bad: observing with the Fragment lifecycle&lt;/span&gt;
&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onViewCreated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submitList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time the view is recreated (back from another screen), you add another observer tied to the Fragment lifecycle. Updates fire multiple times and views that no longer exist get touched. Use the &lt;strong&gt;view&lt;/strong&gt; lifecycle owner:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onViewCreated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;savedInstanceState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bundle&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lifecycleScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeatOnLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Lifecycle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STARTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;collect&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;adapter&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;submitList&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For LiveData the same rule applies: &lt;code&gt;viewModel.items.observe(viewLifecycleOwner) { ... }&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Collecting Flow safely
&lt;/h2&gt;

&lt;p&gt;Collecting a Flow directly in &lt;code&gt;lifecycleScope.launch { }&lt;/code&gt; keeps collecting while the app is in the background, wasting work and sometimes crashing when you touch UI that is not there. Use &lt;code&gt;repeatOnLifecycle&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lifecycleScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;repeatOnLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Lifecycle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;State&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;STARTED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;events&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;handleEvent&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The block starts when the lifecycle reaches &lt;code&gt;STARTED&lt;/code&gt; and is cancelled when it drops below it. When the user returns, it starts again from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fragment Manager, back stack and results
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Passing data between Fragments
&lt;/h3&gt;

&lt;p&gt;Do not call methods on another Fragment directly, and do not use a static field. Two clean options:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Fragment Result API&lt;/strong&gt; for one-off results:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Receiver (e.g. list screen)&lt;/span&gt;
&lt;span class="n"&gt;parentFragmentManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setFragmentResultListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"filter_key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;viewLifecycleOwner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bundle&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;category&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bundle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;applyFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;category&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Sender (e.g. filter bottom sheet)&lt;/span&gt;
&lt;span class="n"&gt;parentFragmentManager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setFragmentResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;"filter_key"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nf"&gt;bundleOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"category"&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="s2"&gt;"shoes"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Shared ViewModel&lt;/strong&gt; scoped to the Activity or a navigation graph, for state that two screens genuinely share:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;sharedViewModel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CheckoutFlowViewModel&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="nf"&gt;activityViewModels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  commit vs commitAllowingStateLoss
&lt;/h3&gt;

&lt;p&gt;If you commit a Fragment transaction after &lt;code&gt;onSaveInstanceState()&lt;/code&gt;, you get &lt;code&gt;IllegalStateException: Can not perform this action after onSaveInstanceState&lt;/code&gt;. This usually happens when a network callback arrives after the user has left the screen. The right fix is not to reach for &lt;code&gt;commitAllowingStateLoss()&lt;/code&gt; blindly. Collect results with &lt;code&gt;repeatOnLifecycle&lt;/code&gt; so navigation only happens while the screen is started.&lt;/p&gt;

&lt;h2&gt;
  
  
  A word on Jetpack Compose
&lt;/h2&gt;

&lt;p&gt;Compose changes how you write UI, not how Android works underneath. Your Activity still goes through the same lifecycle, and process death still kills your state. In Compose:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use &lt;code&gt;rememberSaveable&lt;/code&gt; for UI state that must survive recreation.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;collectAsStateWithLifecycle()&lt;/code&gt; to collect flows lifecycle-aware.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;DisposableEffect&lt;/code&gt; or &lt;code&gt;LifecycleEventEffect&lt;/code&gt; to react to lifecycle events.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;ProfileScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ProfileViewModel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collectAsStateWithLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;rememberSaveable&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;mutableStateOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Interview questions and answers
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. What is the order of Activity lifecycle callbacks when you open a new Activity B from Activity A?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;onPause()&lt;/code&gt;, then B &lt;code&gt;onCreate()&lt;/code&gt;, &lt;code&gt;onStart()&lt;/code&gt;, &lt;code&gt;onResume()&lt;/code&gt;, and only then A &lt;code&gt;onStop()&lt;/code&gt;. This is why &lt;code&gt;onPause()&lt;/code&gt; must be fast: B does not appear until A's &lt;code&gt;onPause()&lt;/code&gt; returns.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What happens when the screen rotates?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Activity is destroyed and recreated: &lt;code&gt;onPause&lt;/code&gt;, &lt;code&gt;onStop&lt;/code&gt;, &lt;code&gt;onSaveInstanceState&lt;/code&gt;, &lt;code&gt;onDestroy&lt;/code&gt;, then &lt;code&gt;onCreate&lt;/code&gt;, &lt;code&gt;onStart&lt;/code&gt;, &lt;code&gt;onRestoreInstanceState&lt;/code&gt;, &lt;code&gt;onResume&lt;/code&gt;. A &lt;code&gt;ViewModel&lt;/code&gt; survives this; fields on the Activity do not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. What is the difference between &lt;code&gt;onSaveInstanceState()&lt;/code&gt; and a ViewModel?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onSaveInstanceState()&lt;/code&gt; writes a small &lt;code&gt;Bundle&lt;/code&gt; that survives both configuration change and process death, but is limited in size (keep it to a few KB). A &lt;code&gt;ViewModel&lt;/code&gt; holds larger in-memory state and survives configuration change only. In practice you combine them using &lt;code&gt;SavedStateHandle&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Is &lt;code&gt;onDestroy()&lt;/code&gt; always called?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No. If the system kills the process, it is not called. Never rely on it to save data.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. What is the difference between &lt;code&gt;onPause()&lt;/code&gt; and &lt;code&gt;onStop()&lt;/code&gt;?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;onPause()&lt;/code&gt; means the Activity lost focus but may still be partly visible (a dialog, or split-screen). &lt;code&gt;onStop()&lt;/code&gt; means it is no longer visible at all.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. How do you handle configuration changes without recreating the Activity?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Declare &lt;code&gt;android:configChanges&lt;/code&gt; in the manifest and handle &lt;code&gt;onConfigurationChanged()&lt;/code&gt; yourself. It is usually discouraged because you then have to handle every change manually, and it does nothing for process death. Prefer ViewModel and saved state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. What is the difference between Fragment lifecycle and view lifecycle?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Fragment can outlive its view. On the back stack, the view is destroyed (&lt;code&gt;onDestroyView&lt;/code&gt;) while the Fragment instance remains. That is why you observe data with &lt;code&gt;viewLifecycleOwner&lt;/code&gt; and null out view binding in &lt;code&gt;onDestroyView()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. Why use &lt;code&gt;viewLifecycleOwner&lt;/code&gt; instead of &lt;code&gt;this&lt;/code&gt; when observing LiveData in a Fragment?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;this&lt;/code&gt; ties the observer to the Fragment lifecycle. After the view is recreated you add a second observer without removing the first, causing duplicate updates and access to destroyed views.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;9. &lt;code&gt;add()&lt;/code&gt; vs &lt;code&gt;replace()&lt;/code&gt; in FragmentTransaction?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;replace()&lt;/code&gt; removes the existing Fragment's view and adds the new one, so the old Fragment goes through &lt;code&gt;onDestroyView()&lt;/code&gt;. &lt;code&gt;add()&lt;/code&gt; leaves the previous Fragment's view in place underneath. With &lt;code&gt;replace()&lt;/code&gt; plus &lt;code&gt;addToBackStack()&lt;/code&gt;, the old Fragment is kept as an instance and recreates its view on back.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. How do two Fragments communicate?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use the Fragment Result API for one-off results, or a shared ViewModel scoped to the parent Activity or navigation graph for shared state. Avoid direct references between Fragments and avoid interfaces that force the Activity to be a relay.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11. What is process death and how do you test it?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The system kills the app process while it is in the background. Test with "Don't keep activities" or &lt;code&gt;adb shell am kill &amp;lt;package&amp;gt;&lt;/code&gt;. Defend against it with &lt;code&gt;SavedStateHandle&lt;/code&gt;, &lt;code&gt;rememberSaveable&lt;/code&gt; and persistent storage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;12. Why does &lt;code&gt;lifecycleScope.launch { flow.collect {} }&lt;/code&gt; waste resources?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It keeps collecting while the app is in the background. Use &lt;code&gt;repeatOnLifecycle(Lifecycle.State.STARTED)&lt;/code&gt; or &lt;code&gt;flowWithLifecycle&lt;/code&gt; so collection stops when the UI is not visible.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13. What is &lt;code&gt;onNewIntent()&lt;/code&gt; and when does it matter?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When an Activity with &lt;code&gt;launchMode="singleTop"&lt;/code&gt;, &lt;code&gt;singleTask&lt;/code&gt; or &lt;code&gt;singleInstance&lt;/code&gt; is already running and receives a new intent, the system calls &lt;code&gt;onNewIntent()&lt;/code&gt; instead of creating a new instance. Typical case: tapping a notification should open the existing screen with new data, not stack a second copy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14. Where would you start and stop a video player?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On API 24 and above, start in &lt;code&gt;onStart()&lt;/code&gt; and release in &lt;code&gt;onStop()&lt;/code&gt;, since multi-window means the Activity can be visible without being resumed. Below API 24, use &lt;code&gt;onResume()&lt;/code&gt; and &lt;code&gt;onPause()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;15. How do you debug a lifecycle bug?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Log each callback with &lt;code&gt;Log.d&lt;/code&gt; or use Android Studio's Logcat filters, enable "Don't keep activities", test rotation and process death, and run LeakCanary to catch leaked views and Contexts.&lt;/p&gt;

&lt;h2&gt;
  
  
  Quick checklist before you ship a screen
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Rotate the device mid-flow. Does state survive?&lt;/li&gt;
&lt;li&gt;Enable "Don't keep activities". Does the screen restore correctly?&lt;/li&gt;
&lt;li&gt;Go to another screen and back. Do observers fire only once?&lt;/li&gt;
&lt;li&gt;Is every resource acquired in a callback released in its matching one?&lt;/li&gt;
&lt;li&gt;Is view binding nulled in &lt;code&gt;onDestroyView()&lt;/code&gt;?&lt;/li&gt;
&lt;li&gt;Are Flows collected with &lt;code&gt;repeatOnLifecycle&lt;/code&gt; or &lt;code&gt;collectAsStateWithLifecycle&lt;/code&gt;?&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/guide/components/activities/activity-lifecycle" rel="noopener noreferrer"&gt;Activity lifecycle (Android Developers)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/guide/fragments/lifecycle" rel="noopener noreferrer"&gt;Fragment lifecycle (Android Developers)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/topic/libraries/architecture/saving-states" rel="noopener noreferrer"&gt;Saving UI states (Android Developers)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://developer.android.com/topic/libraries/architecture/coroutines" rel="noopener noreferrer"&gt;Collect flows lifecycle-aware (Android Developers)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want to see these patterns in real code, my open-source Android projects are on &lt;a href="https://github.com/gargpadmakar" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;About the author:&lt;/strong&gt; Padmakar Garg is a Senior Android Developer with 8+ years of experience in Kotlin, Jetpack Compose, Kotlin Multiplatform and mobile security. Portfolio and blog: &lt;a href="https://gargpadmakar.github.io/" rel="noopener noreferrer"&gt;gargpadmakar.github.io&lt;/a&gt;. Connect on &lt;a href="https://linkedin.com/in/padmakar-garg" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>mobile</category>
      <category>interview</category>
    </item>
    <item>
      <title>Android Architecture Complete Guide: MVC, MVP, MVVM, Clean Architecture, Design Patterns, and SOLID</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Thu, 26 Nov 2026 06:34:19 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/android-architecture-complete-guide-mvc-mvp-mvvm-clean-architecture-design-patterns-and-solid-m98</link>
      <guid>https://dev.to/gargpadmakar/android-architecture-complete-guide-mvc-mvp-mvvm-clean-architecture-design-patterns-and-solid-m98</guid>
      <description>&lt;p&gt;When an Android application starts growing, putting API calls, business&lt;br&gt;
rules, database operations, and UI code into one Activity or Composable&lt;br&gt;
quickly becomes difficult to maintain.&lt;/p&gt;

&lt;p&gt;A small feature may work perfectly today, but what happens when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The API response format changes?&lt;/li&gt;
&lt;li&gt;  You need to add offline support?&lt;/li&gt;
&lt;li&gt;  The same business logic is required on another screen?&lt;/li&gt;
&lt;li&gt;  You want to unit-test business rules without launching Android?&lt;/li&gt;
&lt;li&gt;  Several developers need to work on different parts of the
application?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is where software architecture, design patterns, and SOLID&lt;br&gt;
principles become important.&lt;/p&gt;

&lt;p&gt;In this guide, we will understand the concepts step by step, compare&lt;br&gt;
MVC, MVP, and MVVM, explore Clean Architecture, learn commonly used&lt;br&gt;
design patterns, apply SOLID principles, and finish with Android-focused&lt;br&gt;
interview questions.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Important:&lt;/strong&gt; These concepts solve different problems. MVC, MVP, and&lt;br&gt;
MVVM describe ways to organize presentation logic. Clean Architecture&lt;br&gt;
describes dependency boundaries and separation of concerns. Design&lt;br&gt;
patterns are reusable solutions to recurring design problems. SOLID is&lt;br&gt;
a set of principles that helps us design maintainable object-oriented&lt;br&gt;
code.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2&gt;
  
  
  1. What is software architecture?
&lt;/h2&gt;

&lt;p&gt;Software architecture is the high-level structure of an application. It&lt;br&gt;
defines how major parts of the system are organized, how they&lt;br&gt;
communicate, and which responsibilities belong to each part.&lt;/p&gt;

&lt;p&gt;Think about a food delivery application:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The UI displays restaurants and orders.&lt;/li&gt;
&lt;li&gt;  The presentation layer manages screen state and user actions.&lt;/li&gt;
&lt;li&gt;  The domain layer contains business rules such as placing an order.&lt;/li&gt;
&lt;li&gt;  The data layer communicates with APIs, databases, and other sources.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good architecture aims for:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Separation of concerns:&lt;/strong&gt; Each component has a clear
responsibility.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Maintainability:&lt;/strong&gt; Changes in one area should not unnecessarily
affect other areas.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Testability:&lt;/strong&gt; Business logic should be testable independently.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Reusability:&lt;/strong&gt; Shared logic should not be duplicated.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Scalability:&lt;/strong&gt; New features should fit into the existing
structure.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Understandability:&lt;/strong&gt; Developers should be able to locate and
understand code quickly.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Architecture is not about creating the maximum number of packages or&lt;br&gt;
classes. It is about choosing boundaries that make the application&lt;br&gt;
easier to change.&lt;/p&gt;
&lt;h2&gt;
  
  
  2. MVC --- Model View Controller
&lt;/h2&gt;

&lt;p&gt;MVC stands for &lt;strong&gt;Model--View--Controller&lt;/strong&gt;. It separates an application&lt;br&gt;
into three main responsibilities.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Model:&lt;/strong&gt; Holds application data and business rules.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;View:&lt;/strong&gt; Displays information to the user.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Controller:&lt;/strong&gt; Handles user input and coordinates updates between
the Model and View.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  MVC flow
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Action
    |
    v
   View
    |
    v
Controller
    |
    v
  Model
    |
    v
Controller updates View
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Simple example
&lt;/h3&gt;

&lt;p&gt;Imagine a login screen.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  The View contains the email and password fields and the login
button.&lt;/li&gt;
&lt;li&gt;  The Controller handles the button click and coordinates login.&lt;/li&gt;
&lt;li&gt;  The Model represents the user data and authentication operation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A simplified Kotlin example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginModel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;

        &lt;span class="c1"&gt;// Simplified example. A real app calls a repository/API.&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginController&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginModel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginView&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;onLoginClicked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;showSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Welcome ${user.email}"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;showError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Please enter valid details"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;LoginView&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;showSuccess&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;showError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a conceptual example, not a complete Android implementation. In&lt;br&gt;
Android, the View might be an Activity, Fragment, or custom UI&lt;br&gt;
component.&lt;/p&gt;
&lt;h3&gt;
  
  
  Advantages of MVC
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Easy to understand for small applications.&lt;/li&gt;
&lt;li&gt;  Separates UI rendering from coordination logic.&lt;/li&gt;
&lt;li&gt;  Can be useful in frameworks that naturally provide controller
structures.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Disadvantages of MVC
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Controllers can become large.&lt;/li&gt;
&lt;li&gt;  UI and business logic may become tightly coupled if boundaries are
not enforced.&lt;/li&gt;
&lt;li&gt;  Testing can be difficult when the controller depends directly on
Android UI classes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When should you use it?&lt;/strong&gt; MVC can work well for small applications or&lt;br&gt;
platforms where the framework strongly encourages the pattern. For&lt;br&gt;
larger Android applications, teams often prefer MVVM with additional&lt;br&gt;
architectural boundaries.&lt;/p&gt;
&lt;h2&gt;
  
  
  3. MVP --- Model View Presenter
&lt;/h2&gt;

&lt;p&gt;MVP stands for &lt;strong&gt;Model--View--Presenter&lt;/strong&gt;. It is similar to MVC, but the&lt;br&gt;
Presenter takes responsibility for presentation logic and communicates&lt;br&gt;
with the View through an interface.&lt;/p&gt;

&lt;p&gt;The main components are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Model:&lt;/strong&gt; Provides data and business operations.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;View:&lt;/strong&gt; Displays data and forwards user actions.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Presenter:&lt;/strong&gt; Receives actions, obtains data, and tells the View
what to display.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  MVP flow
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Action
    |
    v
   View
    |
    v
 Presenter
    |
    v
  Model
    |
    v
 Presenter
    |
    v
   View
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  Kotlin example
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;LoginContract&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;View&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;showLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;hideLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;showMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Presenter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginPresenter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;View&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginContract&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Presenter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;showMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Email and password are required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;showLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;authenticate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hideLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;view&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;showMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="s2"&gt;"Login successful"&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s2"&gt;"Login failed"&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The Activity or Fragment implements &lt;code&gt;LoginContract.View&lt;/code&gt; and connects&lt;br&gt;
the UI to the Presenter.&lt;/p&gt;
&lt;h3&gt;
  
  
  Advantages of MVP
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Presentation logic is moved out of the Activity or Fragment.&lt;/li&gt;
&lt;li&gt;  View interfaces make Presenter tests easier.&lt;/li&gt;
&lt;li&gt;  The View can be replaced without rewriting the Presenter, provided
the contract remains stable.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Disadvantages of MVP
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  The Presenter can become large if it handles too many
responsibilities.&lt;/li&gt;
&lt;li&gt;  The View interface may contain many methods.&lt;/li&gt;
&lt;li&gt;  Developers must manage the View lifecycle carefully. A Presenter
that retains an Activity can cause memory leaks.&lt;/li&gt;
&lt;li&gt;  More interfaces and wiring can mean additional boilerplate.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When should you use it?&lt;/strong&gt; MVP is useful in legacy Android applications&lt;br&gt;
or projects where explicit View interfaces and presenter-driven tests&lt;br&gt;
are preferred.&lt;/p&gt;
&lt;h2&gt;
  
  
  4. MVVM --- Model View ViewModel
&lt;/h2&gt;

&lt;p&gt;MVVM stands for &lt;strong&gt;Model--View--ViewModel&lt;/strong&gt;. It is widely used in modern&lt;br&gt;
Android development, particularly with Android Jetpack and Jetpack&lt;br&gt;
Compose.&lt;/p&gt;

&lt;p&gt;The components are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;Model:&lt;/strong&gt; Data sources and business operations, commonly accessed
through repositories and use cases.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;View:&lt;/strong&gt; Activity, Fragment, or Compose UI. It observes state and
renders the screen.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;ViewModel:&lt;/strong&gt; Holds and exposes UI state, handles UI actions, and
coordinates operations needed by the screen.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  MVVM flow
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User Action
    |
    v
   View
    |
    v
 ViewModel
    |
    v
 Repository / Use Case
    |
    v
 API / Database
    |
    v
 Repository / Use Case
    |
    v
 ViewModel updates UI State
    |
    v
   View observes state
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The ViewModel should not hold a reference to an Activity, Fragment,&lt;br&gt;
View, or other lifecycle-bound UI object.&lt;/p&gt;
&lt;h3&gt;
  
  
  Android Kotlin example
&lt;/h3&gt;

&lt;p&gt;First, define a UI state:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a repository contract:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;AuthRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then implement the ViewModel:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;authRepository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AuthRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;MutableStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;LoginUiState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;StateFlow&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;LoginUiState&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asStateFlow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Email and password are required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;authRepository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="n"&gt;_uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;update&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;isLoading&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;fold&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                        &lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"Login successful"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="n"&gt;onFailure&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                            &lt;span class="n"&gt;error&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="s2"&gt;"Login failed"&lt;/span&gt;
                        &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A simplified Jetpack Compose screen:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Composable&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;LoginScreen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginViewModel&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt; &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;uiState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;collectAsStateWithLifecycle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="nc"&gt;Column&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isLoading&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;CircularProgressIndicator&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nc"&gt;Button&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;onClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;viewModel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"user@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                    &lt;span class="n"&gt;password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"password"&lt;/span&gt;
                &lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Login"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This example demonstrates the direction of communication. A production&lt;br&gt;
screen should collect actual form input, handle one-time effects such as&lt;br&gt;
navigation appropriately, and use dependency injection to provide the&lt;br&gt;
repository.&lt;/p&gt;
&lt;h3&gt;
  
  
  Advantages of MVVM
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  UI state is separated from UI rendering.&lt;/li&gt;
&lt;li&gt;  ViewModels survive configuration changes when scoped appropriately.&lt;/li&gt;
&lt;li&gt;  StateFlow and Compose work naturally with observable state.&lt;/li&gt;
&lt;li&gt;  ViewModels can be tested without creating an Activity.&lt;/li&gt;
&lt;li&gt;  Repository abstractions make data sources easier to replace.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Disadvantages of MVVM
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  A ViewModel can become a large class if it owns unrelated screen
responsibilities.&lt;/li&gt;
&lt;li&gt;  Poorly designed state and event handling can create complex UI
behavior.&lt;/li&gt;
&lt;li&gt;  It does not automatically enforce separation between business logic
and data access.&lt;/li&gt;
&lt;li&gt;  Teams still need to manage lifecycle, coroutine cancellation, and
dependency boundaries correctly.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;When should you use it?&lt;/strong&gt; MVVM is a strong default for modern Android&lt;br&gt;
UI development, especially with Jetpack Compose, StateFlow, and&lt;br&gt;
lifecycle-aware state collection.&lt;/p&gt;
&lt;h2&gt;
  
  
  5. MVC vs MVP vs MVVM
&lt;/h2&gt;



&lt;p&gt;Area              MVC               MVP               MVVM&lt;/p&gt;



&lt;p&gt;Main coordinator  Controller        Presenter         ViewModel&lt;/p&gt;

&lt;p&gt;UI communication  Controller        Presenter calls   View observes&lt;br&gt;
                    coordinates View  View interface    ViewModel state&lt;/p&gt;

&lt;p&gt;View dependency   Can be tightly    Usually           Should be&lt;br&gt;
                    coupled           interface-based   lightweight&lt;/p&gt;

&lt;p&gt;Testability       Depends on        Good with View    Good with state&lt;br&gt;
                    separation        contracts         and injected&lt;br&gt;
                                                        dependencies&lt;/p&gt;

&lt;p&gt;Common Android    Simple or legacy  Legacy and        Modern Android&lt;br&gt;
  use               structures        explicit          apps&lt;br&gt;
                                      contracts         &lt;/p&gt;

&lt;p&gt;Typical risk      Large Controller  Large Presenter   Large ViewModel&lt;br&gt;
                                      or retained View  &lt;/p&gt;



&lt;p&gt;&lt;strong&gt;Interview point:&lt;/strong&gt; MVVM is not automatically better in every&lt;br&gt;
situation. Choose a pattern based on the platform, team, application&lt;br&gt;
complexity, testability needs, and existing codebase.&lt;/p&gt;
&lt;h2&gt;
  
  
  6. Clean Architecture
&lt;/h2&gt;

&lt;p&gt;Clean Architecture focuses on separating business rules from frameworks,&lt;br&gt;
UI, and external data sources.&lt;/p&gt;

&lt;p&gt;It is commonly represented as layers:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Presentation layer:&lt;/strong&gt; Activities, Fragments, Compose screens,
ViewModels, and UI state.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Domain layer:&lt;/strong&gt; Business rules, entities, repository contracts,
and use cases.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Data layer:&lt;/strong&gt; Repository implementations, API services, database,
DTOs, and data mapping.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  Dependency rule
&lt;/h3&gt;

&lt;p&gt;The most important rule is:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Source-code dependencies should point inward, toward the domain and&lt;br&gt;
business rules.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The domain layer should not depend on Android UI classes, Retrofit,&lt;br&gt;
Room, or other implementation details.&lt;/p&gt;
&lt;h3&gt;
  
  
  Architecture diagram
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+--------------------------------------+
|             Presentation             |
|       Compose / Activity / ViewModel  |
+-------------------+------------------+
                    |
                    v
+--------------------------------------+
|                Domain                |
|       Use Cases / Entities /         |
|       Repository Interfaces          |
+-------------------+------------------+
                    ^
                    |
+--------------------------------------+
|                 Data                 |
| Repository Implementations / API /   |
| Database / DTOs / Mappers            |
+--------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;The arrows in a real implementation represent dependency direction: the&lt;br&gt;
data implementation depends on domain contracts, while the presentation&lt;br&gt;
layer invokes domain operations. The domain layer remains independent.&lt;/p&gt;
&lt;h3&gt;
  
  
  Recommended Android project structure
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;com.example.app
|
+-- presentation
|   +-- login
|       +-- LoginScreen.kt
|       +-- LoginViewModel.kt
|       +-- LoginUiState.kt
|
+-- domain
|   +-- model
|   |   +-- User.kt
|   +-- repository
|   |   +-- AuthRepository.kt
|   +-- usecase
|       +-- LoginUseCase.kt
|
+-- data
    +-- repository
    |   +-- AuthRepositoryImpl.kt
    +-- remote
    |   +-- AuthApi.kt
    +-- local
    +-- mapper
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;For a small app, these can be packages in one module. For a larger app,&lt;br&gt;
they may become separate Gradle modules. Do not create modules just to&lt;br&gt;
follow a diagram; introduce boundaries when they provide real value.&lt;/p&gt;
&lt;h3&gt;
  
  
  Step-by-step Kotlin example
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Step 1: Domain model&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 2: Repository interface&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;AuthRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 3: Use case&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginUseCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AuthRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;operator&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="nc"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Email is required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isBlank&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="nc"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Password is required"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Step 4: Data implementation&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthRepositoryImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AuthApi&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AuthRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="nc"&gt;User&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="n"&gt;email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;AuthApi&lt;/code&gt; and &lt;code&gt;LoginResponse&lt;/code&gt; are simplified placeholders for your&lt;br&gt;
Retrofit or Ktor API definitions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 5: ViewModel&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;loginUseCase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;LoginUseCase&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;viewModelScope&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;loginUseCase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

            &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onSuccess&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="c1"&gt;// Update success state or emit a UI effect.&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;onFailure&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;error&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;
                &lt;span class="c1"&gt;// Update error state.&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why use cases?
&lt;/h3&gt;

&lt;p&gt;A use case represents one application action or business operation, such&lt;br&gt;
as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  LoginUser&lt;/li&gt;
&lt;li&gt;  GetUserProfile&lt;/li&gt;
&lt;li&gt;  PlaceOrder&lt;/li&gt;
&lt;li&gt;  SyncOfflineTasks&lt;/li&gt;
&lt;li&gt;  UploadDocument&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use cases are valuable when they encapsulate meaningful business rules,&lt;br&gt;
coordinate multiple dependencies, or are reused. A use case that only&lt;br&gt;
forwards one method call without adding a useful boundary may be&lt;br&gt;
unnecessary.&lt;/p&gt;
&lt;h3&gt;
  
  
  Advantages of Clean Architecture
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  Business logic is independent of frameworks.&lt;/li&gt;
&lt;li&gt;  Repository contracts make testing easier.&lt;/li&gt;
&lt;li&gt;  Data sources can be changed with less impact on business rules.&lt;/li&gt;
&lt;li&gt;  Responsibilities are clearer as the application grows.&lt;/li&gt;
&lt;li&gt;  It supports modularization and parallel development.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Disadvantages
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;  More interfaces and files can increase initial development time.&lt;/li&gt;
&lt;li&gt;  Too many trivial use cases can create unnecessary boilerplate.&lt;/li&gt;
&lt;li&gt;  Incorrect dependency direction can make the architecture complicated
rather than clean.&lt;/li&gt;
&lt;li&gt;  It requires consistency and discipline across the team.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  7. MVVM and Clean Architecture: Are they the same?
&lt;/h2&gt;

&lt;p&gt;No.&amp;nbsp;They solve different problems and can be used together.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;MVVM&lt;/strong&gt; organizes the presentation layer.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Clean Architecture&lt;/strong&gt; organizes the application's boundaries and
dependency direction.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A typical Android application can use both:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Compose UI
    |
    v
ViewModel                 &amp;lt;- MVVM
    |
    v
Use Case                  &amp;lt;- Clean Architecture
    |
    v
Repository Interface      &amp;lt;- Domain boundary
    |
    v
Repository Implementation &amp;lt;- Data layer
    |
    v
Retrofit / Room / Ktor
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This combination is common, but not mandatory for every application.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Design Patterns in Android Development
&lt;/h2&gt;

&lt;p&gt;A design pattern is a reusable approach to a recurring software design&lt;br&gt;
problem. Patterns are not ready-made code that must be applied&lt;br&gt;
everywhere. Use them when they simplify a real problem.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.1 Singleton Pattern
&lt;/h3&gt;

&lt;p&gt;Ensures that a class has one shared instance within a defined scope.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;AppConfig&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="no"&gt;BASE_URL&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"https://api.example.com"&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Android use cases:&lt;/strong&gt; Shared configuration or a carefully scoped&lt;br&gt;
manager.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Caution:&lt;/strong&gt; Avoid turning every dependency into a global singleton.&lt;br&gt;
Global state makes tests and lifecycle management harder. Dependency&lt;br&gt;
injection scopes are often a better choice.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.2 Factory Pattern
&lt;/h3&gt;

&lt;p&gt;Creates objects while hiding the construction details from the caller.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CardPayment&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Paid by card: $amount"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UpiPayment&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;pay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Paid by UPI: $amount"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;PaymentFactory&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PaymentMethod&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lowercase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="s2"&gt;"card"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;CardPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="s2"&gt;"upi"&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;UpiPayment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Unsupported payment type"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Android use cases:&lt;/strong&gt; Creating parsers, payment handlers, or&lt;br&gt;
implementations based on configuration.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.3 Observer Pattern
&lt;/h3&gt;

&lt;p&gt;Allows an object to notify multiple observers when its state changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TemperatureSensor&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;observers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mutableListOf&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;observe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Unit&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;observers&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;observer&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;updateTemperature&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;observers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forEach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;it&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Android use cases:&lt;/strong&gt; LiveData, Flow, StateFlow, and reactive UI&lt;br&gt;
updates use observer-like ideas.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.4 Repository Pattern
&lt;/h3&gt;

&lt;p&gt;Provides a consistent interface for accessing data, hiding whether it&lt;br&gt;
comes from a remote API, local database, cache, or another source.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserRepositoryImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserApi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserDao&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dao&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="n"&gt;api&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toDomain&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Android use cases:&lt;/strong&gt; Combining Room, Retrofit/Ktor, caching, and&lt;br&gt;
offline-first behavior.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.5 Strategy Pattern
&lt;/h3&gt;

&lt;p&gt;Defines a family of interchangeable algorithms behind a common&lt;br&gt;
interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;SortStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AscendingSort&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SortStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DescendingSort&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SortStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sortedDescending&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NumberSorter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SortStrategy&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;List&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Android use cases:&lt;/strong&gt; Different sorting, pricing, validation, or&lt;br&gt;
authentication strategies.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.6 Builder Pattern
&lt;/h3&gt;

&lt;p&gt;Constructs complex objects step by step.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;soundEnabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotificationConfigBuilder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;soundEnabled&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Boolean&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;

    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NotificationConfig&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;priority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;priority&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;soundEnabled&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;soundEnabled&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Usage:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NotificationConfigBuilder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;title&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"New task"&lt;/span&gt;
    &lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"A task has been assigned"&lt;/span&gt;
    &lt;span class="n"&gt;priority&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Android use cases:&lt;/strong&gt; Configuring complex objects, test data, or&lt;br&gt;
notification options. Kotlin named and default arguments may make a&lt;br&gt;
Builder unnecessary for simpler objects.&lt;/p&gt;
&lt;h3&gt;
  
  
  8.7 Dependency Injection Pattern
&lt;/h3&gt;

&lt;p&gt;Supplies an object with its dependencies instead of making it construct&lt;br&gt;
those dependencies itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProfileViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A dependency injection framework such as &lt;strong&gt;Koin&lt;/strong&gt; or &lt;strong&gt;Hilt&lt;/strong&gt; can create&lt;br&gt;
and provide the repository and ViewModel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits:&lt;/strong&gt; Lower coupling, easier testing, and centralized dependency&lt;br&gt;
creation.&lt;/p&gt;
&lt;h2&gt;
  
  
  9. SOLID Principles
&lt;/h2&gt;

&lt;p&gt;SOLID is a group of five object-oriented design principles. They help&lt;br&gt;
developers create code that is easier to extend, test, and maintain.&lt;/p&gt;
&lt;h3&gt;
  
  
  S --- Single Responsibility Principle
&lt;/h3&gt;

&lt;p&gt;A class should have one clear responsibility, or one primary reason to&lt;br&gt;
change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bad example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserManager&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* authentication */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* database operation */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* notification */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This class handles authentication, persistence, and notifications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better approach:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* authentication */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserLocalDataSource&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;saveUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* database operation */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EmailService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sendEmail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="cm"&gt;/* notification */&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Android example:&lt;/strong&gt; Keep API operations in data sources or services,&lt;br&gt;
business rules in domain operations, and UI state management in&lt;br&gt;
ViewModels.&lt;/p&gt;
&lt;h3&gt;
  
  
  O --- Open/Closed Principle
&lt;/h3&gt;

&lt;p&gt;Software entities should be open for extension but closed for&lt;br&gt;
modification.&lt;/p&gt;

&lt;p&gt;You should be able to add a new behavior without repeatedly editing&lt;br&gt;
stable existing logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;DiscountStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;RegularDiscount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DiscountStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;PremiumDiscount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DiscountStrategy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.15&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Checkout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;discountStrategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DiscountStrategy&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;finalPrice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;total&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;discountStrategy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;calculate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;total&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To add another discount type, create another implementation rather than&lt;br&gt;
adding more conditions inside &lt;code&gt;Checkout&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  L --- Liskov Substitution Principle
&lt;/h3&gt;

&lt;p&gt;Objects of a subtype should be usable wherever the parent type is&lt;br&gt;
expected without breaking the correctness of the program.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problematic example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="n"&gt;open&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;open&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Penguins cannot fly"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A caller expecting every &lt;code&gt;Bird&lt;/code&gt; to fly may fail when it receives a&lt;br&gt;
Penguin.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better approach:&lt;/strong&gt; Model capabilities explicitly.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Eagle&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;FlyingBird&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fly&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Eagle is flying"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Penguin&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Bird&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Do not force a subtype to support behavior that does not make sense for&lt;br&gt;
it.&lt;/p&gt;
&lt;h3&gt;
  
  
  I --- Interface Segregation Principle
&lt;/h3&gt;

&lt;p&gt;Clients should not be forced to depend on methods they do not use.&lt;br&gt;
Prefer small, focused interfaces over large interfaces with unrelated&lt;br&gt;
responsibilities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problematic interface:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Device&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A basic printer may not support scanning or faxing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better approach:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Scanner&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;FaxMachine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fax&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BasicPrinter&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Printer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Printing"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  D --- Dependency Inversion Principle
&lt;/h3&gt;

&lt;p&gt;High-level business logic should not depend directly on low-level&lt;br&gt;
implementation details. Both should depend on abstractions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problematic example:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserViewModel&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FirebaseUserRepository&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The ViewModel is tightly coupled to a concrete Firebase implementation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Better approach:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;getUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;User&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UserViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;repository&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UserRepository&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ViewModel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the ViewModel depends on an abstraction. Production can use a&lt;br&gt;
Firebase-backed implementation, while tests can use a fake repository.&lt;/p&gt;
&lt;h3&gt;
  
  
  SOLID quick revision
&lt;/h3&gt;



&lt;p&gt;Principle               Main idea               Quick question&lt;/p&gt;



&lt;p&gt;SRP                     One responsibility      Does this class have&lt;br&gt;
                                                  unrelated reasons to&lt;br&gt;
                                                  change?&lt;/p&gt;

&lt;p&gt;OCP                     Extend without          Can I add a behavior&lt;br&gt;
                          modifying stable code   without changing&lt;br&gt;
                                                  existing logic?&lt;/p&gt;

&lt;p&gt;LSP                     Subtypes remain         Can this implementation&lt;br&gt;
                          substitutable           safely replace its&lt;br&gt;
                                                  parent type?&lt;/p&gt;

&lt;p&gt;ISP                     Small focused           Does a client depend on&lt;br&gt;
                          interfaces              methods it never uses?&lt;/p&gt;

&lt;p&gt;DIP                     Depend on abstractions  Does high-level logic&lt;br&gt;
                                                  know concrete&lt;br&gt;
                                                  infrastructure details?&lt;/p&gt;


&lt;h2&gt;
  
  
  10. How these concepts work together in a real Android app
&lt;/h2&gt;

&lt;p&gt;Consider an offline-first task management application.&lt;/p&gt;

&lt;p&gt;A user opens the task list, creates a task, and expects it to be saved&lt;br&gt;
even if the network is unavailable.&lt;/p&gt;

&lt;p&gt;A possible architecture is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TaskListScreen (Compose)
        |
        v
TaskViewModel
        |
        v
GetTasksUseCase / CreateTaskUseCase
        |
        v
TaskRepository (domain interface)
        |
        v
TaskRepositoryImpl
       / \
      v   v
   Room  Remote API
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;How each concept contributes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;strong&gt;MVVM:&lt;/strong&gt; The ViewModel exposes task-list UI state to Compose.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Clean Architecture:&lt;/strong&gt; Use cases and repository contracts keep
business rules independent of Android and networking frameworks.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Repository Pattern:&lt;/strong&gt; The repository decides how local and remote
data are coordinated.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Observer Pattern:&lt;/strong&gt; Flow or StateFlow can notify the UI when task
data changes.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;Dependency Injection:&lt;/strong&gt; Koin or Hilt provides the required
implementations.&lt;/li&gt;
&lt;li&gt;  &lt;strong&gt;SOLID:&lt;/strong&gt; Small responsibilities and abstractions make the feature
easier to extend and test.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful rule is to make the local database the source of truth in a&lt;br&gt;
genuinely offline-first design. The UI observes local data, while&lt;br&gt;
synchronization updates the local database as remote operations succeed.&lt;br&gt;
The exact sync strategy depends on conflict resolution, retries, and&lt;br&gt;
product requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  11. Common Android architecture mistakes
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Putting everything in the ViewModel:&lt;/strong&gt; Split responsibilities when
a ViewModel starts handling unrelated features or complex business
rules.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Calling APIs directly from the UI:&lt;/strong&gt; Move data access behind
repositories or domain operations.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Creating interfaces for every class:&lt;/strong&gt; Abstractions are useful at
boundaries, but unnecessary interfaces add indirection.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Making every operation a use case:&lt;/strong&gt; Create use cases for
meaningful business actions, not merely to increase file count.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Using global singletons for everything:&lt;/strong&gt; Prefer controlled
dependency injection scopes.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Ignoring lifecycle:&lt;/strong&gt; Avoid retaining Activity or View references
in ViewModels and presenters.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Mixing UI models and API DTOs:&lt;/strong&gt; Map external response models into
models appropriate for the domain or presentation layer.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Treating all UI events as permanent state:&lt;/strong&gt; Model persistent
screen information as state; handle one-time effects such as
navigation and transient messages deliberately.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Overengineering small applications:&lt;/strong&gt; Start with the simplest
structure that preserves clear responsibilities, then introduce more
boundaries as complexity grows.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  12. Android Architecture Interview Questions and Answers
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Beginner-level questions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. What is software architecture?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Software architecture defines the structure of an application, its major&lt;br&gt;
components, their responsibilities, and how they communicate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. What is MVC?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MVC separates an application into Model, View, and Controller. The&lt;br&gt;
Controller coordinates user input and updates involving the Model and&lt;br&gt;
View.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. What is MVP?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MVP separates Model, View, and Presenter. The Presenter handles&lt;br&gt;
presentation logic and communicates with the View through an interface.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. What is MVVM?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;MVVM separates Model, View, and ViewModel. The View observes UI state&lt;br&gt;
exposed by the ViewModel and forwards user actions to it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. What is the main difference between MVP and MVVM?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In a typical MVP design, the Presenter directly calls methods on a View&lt;br&gt;
interface. In MVVM, the View observes state exposed by the ViewModel.&lt;br&gt;
Exact implementations can vary.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;6. What is a ViewModel in Android?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An Android ViewModel stores and manages UI-related data in a&lt;br&gt;
lifecycle-conscious way. It is commonly used to expose screen state and&lt;br&gt;
coordinate operations for a UI.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;7. Does ViewModel survive screen rotation?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;An AndroidX ViewModel generally survives configuration changes when it&lt;br&gt;
remains in the same ViewModelStoreOwner scope. It is not automatically&lt;br&gt;
persistent across process death.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;8. What is a Repository?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Repository provides an abstraction over data sources such as APIs,&lt;br&gt;
databases, and caches.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intermediate-level questions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;9. What is Clean Architecture?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Clean Architecture separates concerns into layers and keeps source-code&lt;br&gt;
dependencies pointing toward business rules. It aims to keep the domain&lt;br&gt;
independent of frameworks and infrastructure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;10. What is the Dependency Rule?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dependencies in source code should point inward. Outer layers can depend&lt;br&gt;
on inner layers, but inner business rules should not depend on outer&lt;br&gt;
implementation details.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;11. What is a Use Case?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Use Case represents a specific application action or business&lt;br&gt;
operation, such as creating a task or logging in a user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;12. Can MVVM be used with Clean Architecture?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Yes. MVVM structures the presentation layer, while Clean Architecture&lt;br&gt;
defines boundaries across the application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;13. What is Dependency Injection?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dependency Injection is a technique in which an object receives its&lt;br&gt;
dependencies from outside rather than constructing concrete dependencies&lt;br&gt;
itself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;14. What is the difference between Dependency Injection and Dependency&lt;br&gt;
Inversion?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dependency Injection is a technique for supplying dependencies.&lt;br&gt;
Dependency Inversion is a design principle that encourages high-level&lt;br&gt;
and low-level modules to depend on abstractions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;15. What is the Repository Pattern?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It provides a consistent interface for accessing data and hides details&lt;br&gt;
of the underlying data sources.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;16. What is the Singleton Pattern?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It provides one shared instance within a defined scope. In Kotlin, an&lt;br&gt;
&lt;code&gt;object&lt;/code&gt; declaration creates a singleton object.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;17. What is the Observer Pattern?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It allows observers to receive updates when a subject's state or data&lt;br&gt;
changes. LiveData and Flow are commonly used for observable data streams&lt;br&gt;
in Android.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;18. What is the Strategy Pattern?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It encapsulates interchangeable algorithms behind a common interface,&lt;br&gt;
allowing the behavior to be selected or replaced.&lt;/p&gt;

&lt;h3&gt;
  
  
  Advanced and scenario-based questions
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;19. Why should a ViewModel not hold an Activity reference?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A ViewModel can outlive an Activity instance during configuration&lt;br&gt;
changes. Holding the old Activity can retain its view hierarchy and&lt;br&gt;
cause memory leaks. Keep UI references in lifecycle-aware UI components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;20. Where should validation logic be placed?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It depends on the validation. Basic input feedback can live in the&lt;br&gt;
presentation layer, while business-critical validation must be enforced&lt;br&gt;
in the domain or appropriate business boundary. The backend must also&lt;br&gt;
validate untrusted input.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;21. Should every repository have an interface?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Not necessarily. Use an interface when it provides a meaningful&lt;br&gt;
boundary, such as separating domain logic from data implementation or&lt;br&gt;
enabling alternative implementations and testing. Avoid abstractions&lt;br&gt;
that add no value.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;22. What happens if a ViewModel becomes very large?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Identify separate responsibilities and extract meaningful collaborators,&lt;br&gt;
such as use cases or focused components. Keep screen state coordination&lt;br&gt;
in the ViewModel rather than moving code arbitrarily.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;23. How would you design an offline-first Android application?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Use a local database as the UI's source of truth, observe local data&lt;br&gt;
through Flow, coordinate remote synchronization through a repository,&lt;br&gt;
and define retry and conflict-resolution policies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;24. How do you unit-test a ViewModel?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Inject dependencies, provide fake or mock implementations, execute&lt;br&gt;
coroutine work with a test dispatcher, and assert the emitted UI state.&lt;br&gt;
Avoid requiring an Activity or emulator for ordinary ViewModel unit&lt;br&gt;
tests.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;25. How do SOLID principles improve Android development?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;They encourage focused classes, extensible behavior, safe abstractions,&lt;br&gt;
smaller interfaces, and reduced coupling. This makes features easier to&lt;br&gt;
test and change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;26. Is Clean Architecture mandatory for every Android application?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;No.&amp;nbsp;Architecture should match the application's complexity and expected&lt;br&gt;
lifetime. A small app may not need separate modules and many use cases.&lt;br&gt;
Larger applications benefit from stronger boundaries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;27. What is the difference between UI State and UI Events?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;UI state describes information that should remain represented on the&lt;br&gt;
screen, such as loading, content, or an error message. UI events are&lt;br&gt;
actions or one-time effects, such as navigation or showing a transient&lt;br&gt;
message. Event handling should account for lifecycle and delivery&lt;br&gt;
requirements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;28. How would you choose between MVVM and MVP for an existing&lt;br&gt;
project?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Consider the current architecture, testing needs, lifecycle handling, UI&lt;br&gt;
framework, team experience, and migration cost. Avoid rewriting a stable&lt;br&gt;
application solely because another pattern is more fashionable.&lt;/p&gt;

&lt;h2&gt;
  
  
  13. A practical learning roadmap
&lt;/h2&gt;

&lt;p&gt;If you are learning or preparing for senior Android interviews, follow&lt;br&gt;
this order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;OOP fundamentals:&lt;/strong&gt; Classes, objects, inheritance, polymorphism,
abstraction, and interfaces.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;SOLID principles:&lt;/strong&gt; Understand each principle and refactor small
Kotlin examples.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Design patterns:&lt;/strong&gt; Practice Repository, Factory, Observer,
Strategy, Builder, and Dependency Injection.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Presentation patterns:&lt;/strong&gt; Build the same small login feature using
MVC, MVP, and MVVM.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Android Jetpack:&lt;/strong&gt; Learn ViewModel, StateFlow, lifecycle-aware
collection, Room, Navigation, and coroutines.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Clean Architecture:&lt;/strong&gt; Separate presentation, domain, and data
responsibilities.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Testing:&lt;/strong&gt; Write unit tests for use cases, repositories, and
ViewModels.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;System design:&lt;/strong&gt; Study offline-first behavior, caching,
synchronization, modularization, scalability, and failure handling.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Practice project
&lt;/h3&gt;

&lt;p&gt;Build a small Task Manager application with these features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  Login and user session management.&lt;/li&gt;
&lt;li&gt;  Create, update, and delete tasks.&lt;/li&gt;
&lt;li&gt;  Task list with filtering and sorting.&lt;/li&gt;
&lt;li&gt;  Room database for offline access.&lt;/li&gt;
&lt;li&gt;  Retrofit or Ktor for remote APIs.&lt;/li&gt;
&lt;li&gt;  Repository and use case layers.&lt;/li&gt;
&lt;li&gt;  MVVM with StateFlow.&lt;/li&gt;
&lt;li&gt;  Koin or Hilt for dependency injection.&lt;/li&gt;
&lt;li&gt;  Unit tests for use cases and ViewModels.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Start with one feature and keep the design simple. Add complexity only&lt;br&gt;
when a real requirement needs it.&lt;/p&gt;

&lt;h2&gt;
  
  
  14. Final thoughts
&lt;/h2&gt;

&lt;p&gt;MVC, MVP, and MVVM are approaches to organizing presentation logic.&lt;br&gt;
Clean Architecture helps define boundaries and dependency direction.&lt;br&gt;
Design patterns offer reusable approaches to recurring problems, while&lt;br&gt;
SOLID principles guide the design of maintainable object-oriented code.&lt;/p&gt;

&lt;p&gt;For modern Android development, MVVM combined with clear domain and data&lt;br&gt;
boundaries is a practical choice. However, the goal is not to follow a&lt;br&gt;
particular architecture diagram. The goal is to build software that is&lt;br&gt;
understandable, testable, reliable, and easy to change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Remember:&lt;/strong&gt; Good architecture is not the architecture with the most&lt;br&gt;
layers. Good architecture is the simplest structure that handles today's&lt;br&gt;
requirements while allowing tomorrow's changes.&lt;/p&gt;




&lt;h2&gt;
  
  
  Further reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;  &lt;a href="https://developer.android.com/topic/architecture" rel="noopener noreferrer"&gt;Guide to app architecture --- Android
Developers&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://developer.android.com/topic/architecture/recommendations" rel="noopener noreferrer"&gt;Recommendations for Android
architecture&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://kotlinlang.org/docs/home.html" rel="noopener noreferrer"&gt;Kotlin documentation&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;  &lt;a href="https://developer.android.com/topic/architecture/ui-layer" rel="noopener noreferrer"&gt;Guide to app architecture --- Android Developers: UI
layer&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;If you want to see these patterns in real code, my open-source Android projects are on &lt;a href="https://github.com/gargpadmakar" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;About the author:&lt;/strong&gt; Padmakar Garg is a Senior Android Developer with 8+ years of experience in Kotlin, Jetpack Compose, Kotlin Multiplatform and mobile security. Portfolio and blog: &lt;a href="https://gargpadmakar.github.io/" rel="noopener noreferrer"&gt;gargpadmakar.github.io&lt;/a&gt;. Connect on &lt;a href="https://linkedin.com/in/padmakar-garg" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>architecture</category>
      <category>programming</category>
    </item>
    <item>
      <title>One Codebase, Two Attack Surfaces: A Practical Guide to Securing Kotlin Multiplatform Apps</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Wed, 25 Nov 2026 09:15:25 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/one-codebase-two-attack-surfaces-a-practical-guide-to-securing-kotlin-multiplatform-apps-787</link>
      <guid>https://dev.to/gargpadmakar/one-codebase-two-attack-surfaces-a-practical-guide-to-securing-kotlin-multiplatform-apps-787</guid>
      <description>&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt; Kotlin Multiplatform shares your logic, not your security. Keep decisions and flows in &lt;code&gt;commonMain&lt;/code&gt;, and put keys, secure storage, TLS pinning and attestation in platform code (Android Keystore, iOS Keychain, Play Integrity, App Attest), with your server as the final judge.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm Padmakar Garg, a Senior Android Developer. I work on secure messaging, encrypted calling and Android security. The code below is for &lt;strong&gt;understanding&lt;/strong&gt; where each protection belongs. For production, use vetted, maintained libraries (see the end).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; a reader pointed out that my first version of &lt;code&gt;SecureStorage&lt;/code&gt; returned &lt;code&gt;null&lt;/code&gt; for every failure, so common code couldn't tell "no value" from "the key was invalidated". I've fixed that below. See "Handling invalidated keys".&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kotlin Multiplatform lets you share business logic between Android and iOS. That's a huge productivity win, but it creates a quiet security trap: &lt;strong&gt;shared code runs on two very different platforms, each with its own attack surface.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A token stored in plain text is exposed on both. A hardcoded key in &lt;code&gt;commonMain&lt;/code&gt; ships in both binaries. And the strongest protections (hardware-backed keys, attestation) live in the &lt;strong&gt;platform layer&lt;/strong&gt;, not in shared Kotlin.&lt;/p&gt;

&lt;p&gt;This article shows how to structure a KMP app so security-sensitive work happens in the right place.&lt;/p&gt;

&lt;h2&gt;
  
  
  The core idea: share logic, delegate trust to the platform
&lt;/h2&gt;

&lt;p&gt;A good rule for KMP security:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Put &lt;em&gt;decisions and flows&lt;/em&gt; in &lt;code&gt;commonMain&lt;/code&gt;. Put &lt;em&gt;secrets, keys and trust anchors&lt;/em&gt; behind &lt;code&gt;expect/actual&lt;/code&gt; or interfaces implemented by each platform.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Shared code should never touch raw key material or decide "is this device trustworthy?" by itself. It should call a platform capability and act on the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Secure storage: Keystore and Keychain behind one interface
&lt;/h2&gt;

&lt;p&gt;Don't store tokens in plain &lt;code&gt;SharedPreferences&lt;/code&gt;, &lt;code&gt;NSUserDefaults&lt;/code&gt; or a settings library without encryption. Define a small contract in common code.&lt;/p&gt;

&lt;p&gt;The important design choice: &lt;strong&gt;don't return &lt;code&gt;String?&lt;/code&gt;&lt;/strong&gt;. A bare &lt;code&gt;null&lt;/code&gt; can't distinguish "nothing stored" from "the key is gone" from "the device is temporarily locked", and each of those needs a different reaction. Return an explicit result instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// commonMain&lt;/span&gt;
&lt;span class="k"&gt;enum&lt;/span&gt; &lt;span class="nf"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SecureStatus&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="no"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;               &lt;span class="c1"&gt;// operation succeeded&lt;/span&gt;
    &lt;span class="no"&gt;NOT_FOUND&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;        &lt;span class="c1"&gt;// nothing stored under this key&lt;/span&gt;
    &lt;span class="no"&gt;KEY_INVALIDATED&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;// the key or data can never be read again: wipe and re-enroll&lt;/span&gt;
    &lt;span class="no"&gt;FAILURE&lt;/span&gt;           &lt;span class="c1"&gt;// transient or unknown error: don't wipe, retry later&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;clearAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// delete stored data AND the platform key&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I use a flat result type instead of a generic sealed class because it maps cleanly to Swift, which matters when the iOS side implements the interface.&lt;/p&gt;

&lt;h3&gt;
  
  
  Android: AES-GCM with a key in Android Keystore
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// androidMain&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AndroidSecureStorage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;prefs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getSharedPreferences&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"secure_store"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;MODE_PRIVATE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;alias&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"app_master_key"&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecretKey&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;ks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KeyStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AndroidKeyStore"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;SecretKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;let&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;generator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KeyGenerator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEY_ALGORITHM_AES&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"AndroidKeyStore"&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;KeyGenParameterSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="n"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PURPOSE_ENCRYPT&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;PURPOSE_DECRYPT&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setBlockModes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;BLOCK_MODE_GCM&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setEncryptionPaddings&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;KeyProperties&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ENCRYPTION_PADDING_NONE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setKeySize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;256&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AES/GCM/NoPadding"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ENCRYPT_MODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;edit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;putString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;encodeToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NO_WRAP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KeyPermanentlyInvalidatedException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEY_INVALIDATED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FAILURE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;encoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="o"&gt;?:&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NOT_FOUND&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encoded&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Base64&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NO_WRAP&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copyOfRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cipherText&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copyOfRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AES/GCM/NoPadding"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DECRYPT_MODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;secretKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;GCMParameterSpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cipherText&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KeyPermanentlyInvalidatedException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEY_INVALIDATED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// key was invalidated&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UnrecoverableKeyException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEY_INVALIDATED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// key can't be used any more&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;AEADBadTagException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEY_INVALIDATED&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// wrong key, or corrupted/tampered blob&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FAILURE&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;           &lt;span class="c1"&gt;// transient or unknown&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;edit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;clearAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;prefs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;edit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;runCatching&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nc"&gt;KeyStore&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AndroidKeyStore"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;apply&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;load&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deleteEntry&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key never leaves the Keystore. Only the encrypted blob is stored.&lt;/p&gt;

&lt;h3&gt;
  
  
  iOS: Keychain
&lt;/h3&gt;

&lt;p&gt;Writing Keychain code in Kotlin/Native means verbose cinterop with &lt;code&gt;CFDictionary&lt;/code&gt;. A clean alternative is to implement the interface &lt;strong&gt;in Swift&lt;/strong&gt; and inject it from the iOS app:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// iosApp&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;IosSecureStorage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NSObject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"com.example.app"&lt;/span&gt;

    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;baseQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kSecClass&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;kSecClassGenericPassword&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;kSecAttrService&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
         &lt;span class="n"&gt;kSecAttrAccount&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;put&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SecItemDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;baseQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;CFDictionary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;baseQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kSecValueData&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kSecAttrAccessible&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kSecAttrAccessibleWhenUnlockedThisDeviceOnly&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SecItemAdd&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;CFDictionary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;errSecSuccess&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;baseQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kSecReturnData&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
        &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kSecMatchLimit&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;kSecMatchLimitOne&lt;/span&gt;
        &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;AnyObject&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SecItemCopyMatching&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;CFDictionary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

        &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;errSecSuccess&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;encoding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utf8&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;errSecItemNotFound&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notFound&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="n"&gt;errSecInteractionNotAllowed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="c1"&gt;// Device is locked: temporary. Do NOT wipe, retry later.&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;SecureResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;failure&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;remove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;SecItemDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;baseQuery&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;CFDictionary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;clearAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;query&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;kSecClass&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;kSecClassGenericPassword&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                                    &lt;span class="n"&gt;kSecAttrService&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;service&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="nc"&gt;SecItemDelete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;query&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nc"&gt;CFDictionary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;WhenUnlockedThisDeviceOnly&lt;/code&gt; keeps items off backups and other devices. (Check the generated Kotlin/Native header for the exact Swift names of the enum cases in your project.)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prefer a library?&lt;/strong&gt; Several KMP libraries wrap Keystore and Keychain behind one API, for example &lt;a href="https://github.com/ioannisa/ksafe" rel="noopener noreferrer"&gt;KSafe&lt;/a&gt; and &lt;a href="https://github.com/Alims-Repo/SecureVault-KMP" rel="noopener noreferrer"&gt;SecureVault KMP&lt;/a&gt;. Review their security notes and maintenance activity before adopting one in production.&lt;/p&gt;

&lt;h2&gt;
  
  
  Handling invalidated keys
&lt;/h2&gt;

&lt;p&gt;Hardware-backed keys can disappear or become unusable while your encrypted data is still sitting in storage. Common cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Auth-bound keys on Android.&lt;/strong&gt; If you create the key with &lt;code&gt;setUserAuthenticationRequired(true)&lt;/code&gt; (for example to gate access with biometrics), Android can invalidate it when the secure lock screen is removed or changed, or when new biometrics are enrolled. Using it then throws &lt;code&gt;KeyPermanentlyInvalidatedException&lt;/code&gt;. The key in the example above is &lt;strong&gt;not&lt;/strong&gt; auth-bound, so these events don't affect it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup restore or device migration.&lt;/strong&gt; The encrypted preferences come back on the new device, but the Keystore key does not. Decryption then fails, typically with &lt;code&gt;AEADBadTagException&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Corrupted or tampered data.&lt;/strong&gt; GCM authentication fails, so the blob can never be decrypted.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;iOS.&lt;/strong&gt; Items protected with an access control such as &lt;code&gt;.biometryCurrentSet&lt;/code&gt; stop being usable when the biometric enrollment changes. Treat that as &lt;code&gt;KEY_INVALIDATED&lt;/code&gt; too.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you want biometric-bound keys on Android, add this when building the key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="nc"&gt;KeyGenParameterSpec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;alias&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;PURPOSE_ENCRYPT&lt;/span&gt; &lt;span class="ow"&gt;or&lt;/span&gt; &lt;span class="no"&gt;PURPOSE_DECRYPT&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// ...&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setUserAuthenticationRequired&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setInvalidatedByBiometricEnrollment&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// API 24+&lt;/span&gt;
    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The reaction belongs in &lt;code&gt;commonMain&lt;/code&gt;, because it's a decision, not a platform detail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// commonMain&lt;/span&gt;
&lt;span class="k"&gt;sealed&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Restored&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;refreshToken&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;SignedOut&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;ReEnrollRequired&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt;
    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="k"&gt;object&lt;/span&gt; &lt;span class="nc"&gt;RetryLater&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;restoreSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SecureStorage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"refresh_token"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;status&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;OK&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Restored&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;requireNotNull&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;NOT_FOUND&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SignedOut&lt;/span&gt;
        &lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;KEY_INVALIDATED&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;storage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;clearAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                   &lt;span class="c1"&gt;// delete stale data and the dead key&lt;/span&gt;
            &lt;span class="nc"&gt;SessionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ReEnrollRequired&lt;/span&gt;        &lt;span class="c1"&gt;// fresh sign-in / re-enrollment&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nc"&gt;SecureStatus&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;FAILURE&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;SessionState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RetryLater&lt;/span&gt;   &lt;span class="c1"&gt;// don't wipe on transient errors&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three rules I follow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Wipe on &lt;code&gt;KEY_INVALIDATED&lt;/code&gt;, never on &lt;code&gt;FAILURE&lt;/code&gt;.&lt;/strong&gt; A locked phone or a temporary error shouldn't log the user out.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Never silently regenerate the key and carry on.&lt;/strong&gt; That hides the event from the user and the backend. Force a fresh sign-in, and revoke the old session server-side.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Make the failure visible in telemetry&lt;/strong&gt; (without logging secrets), so you can see how often it happens.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  2. Network security: pin certificates per engine
&lt;/h2&gt;

&lt;p&gt;Ktor is the usual KMP HTTP client, and it uses a different engine on each platform. TLS pinning therefore happens in the &lt;strong&gt;platform engine&lt;/strong&gt;, not in common code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// commonMain&lt;/span&gt;
&lt;span class="n"&gt;expect&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createHttpClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// androidMain (OkHttp engine)&lt;/span&gt;
&lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createHttpClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OkHttp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nf"&gt;certificatePinner&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="nc"&gt;CertificatePinner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"api.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sha256/PRIMARY_PIN_BASE64="&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"api.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sha256/BACKUP_PIN_BASE64="&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// iosMain (Darwin engine)&lt;/span&gt;
&lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;createHttpClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HttpClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Darwin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;engine&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;handleChallenge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="nc"&gt;CertificatePinner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Builder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"api.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sha256/PRIMARY_PIN_BASE64="&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"api.example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"sha256/BACKUP_PIN_BASE64="&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;build&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(Check the Ktor docs for the exact Darwin pinning API in your Ktor version.)&lt;/p&gt;

&lt;p&gt;Pinning tips:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pin the public key (SPKI)&lt;/strong&gt;, not the whole certificate, so renewals don't break the app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Always ship a backup pin&lt;/strong&gt;, or a certificate rotation can lock every user out.&lt;/li&gt;
&lt;li&gt;Plan how you'll update pins (remote config or a forced-update path).&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  3. Cryptography: don't write your own
&lt;/h2&gt;

&lt;p&gt;Avoid hand-rolling crypto in common code. Two safe patterns:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use platform primitives (Keystore/CryptoKit) through &lt;code&gt;expect/actual&lt;/code&gt;, as above.&lt;/li&gt;
&lt;li&gt;Use a well-reviewed multiplatform crypto library, such as cryptography-kotlin, which wraps each platform's native provider.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Whichever you choose: use &lt;strong&gt;AES-GCM&lt;/strong&gt; (authenticated encryption), a &lt;strong&gt;fresh random IV/nonce for every encryption&lt;/strong&gt;, and never reuse a nonce with the same key.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Secrets in &lt;code&gt;commonMain&lt;/code&gt; are secrets in both apps
&lt;/h2&gt;

&lt;p&gt;Anything compiled into your binary can be extracted: API keys, signing secrets, "hidden" endpoints. Moving it to &lt;code&gt;commonMain&lt;/code&gt; doesn't hide it. Instead:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Keep long-lived secrets on your &lt;strong&gt;backend&lt;/strong&gt; and issue &lt;strong&gt;short-lived tokens&lt;/strong&gt; to the app.&lt;/li&gt;
&lt;li&gt;Don't commit keys. Inject per-build values from CI.&lt;/li&gt;
&lt;li&gt;Enable &lt;strong&gt;R8/ProGuard&lt;/strong&gt; for the Android release build. Kotlin/Native iOS binaries are still reverse-engineerable, so never rely on obscurity.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Device and app integrity: attestation per platform
&lt;/h2&gt;

&lt;p&gt;"Is this device and app genuine?" can't be answered safely in shared code. Define the contract, implement it natively, and &lt;strong&gt;verify on your server&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// commonMain&lt;/span&gt;
&lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IntegrityProvider&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/** Returns a token your backend verifies. Nonce must come from the server. */&lt;/span&gt;
    &lt;span class="n"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;attest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;serverNonce&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Android:&lt;/strong&gt; implement with the &lt;strong&gt;Play Integrity API&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;iOS:&lt;/strong&gt; implement with &lt;strong&gt;App Attest&lt;/strong&gt; (DeviceCheck).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The shared flow stays identical: request a nonce from the server, get a token from the platform, send it back, and let the &lt;strong&gt;backend&lt;/strong&gt; decide what the user may do. Never trust a verdict decided only on the device.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Smaller things that matter
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Logging:&lt;/strong&gt; never log tokens, passwords or PII. Strip or silence verbose logging in release builds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Screens:&lt;/strong&gt; on Android use &lt;code&gt;FLAG_SECURE&lt;/code&gt; for sensitive screens; on iOS hide content when the app moves to the background (app switcher snapshots).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Biometrics:&lt;/strong&gt; use them to &lt;em&gt;unlock access to a key&lt;/em&gt; (Keystore/Keychain access control), not just as a UI gate that can be skipped. Remember that auth-bound keys can be invalidated, so handle it as shown above.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Dependencies:&lt;/strong&gt; KMP pulls in many libraries. Pin versions and watch for security advisories.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common mistakes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Treating shared code as "secure by default".&lt;/li&gt;
&lt;li&gt;Returning &lt;code&gt;String?&lt;/code&gt; from secure storage and swallowing every exception into &lt;code&gt;null&lt;/code&gt;, so "missing" and "invalidated" look identical.&lt;/li&gt;
&lt;li&gt;Wiping data on a transient error (for example a locked device) instead of only on a permanent one.&lt;/li&gt;
&lt;li&gt;Silently regenerating a lost key instead of forcing re-enrollment.&lt;/li&gt;
&lt;li&gt;One pinned certificate with no backup pin.&lt;/li&gt;
&lt;li&gt;Storing refresh tokens in unencrypted multiplatform settings.&lt;/li&gt;
&lt;li&gt;Trusting a root/jailbreak check result without server verification.&lt;/li&gt;
&lt;li&gt;Putting authorization rules (prices, roles, limits) in the client.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What to use in production
&lt;/h2&gt;

&lt;p&gt;The snippets above show the &lt;em&gt;shape&lt;/em&gt; of a secure KMP setup, but writing your own security layer is risky because small mistakes are invisible and expensive. For a real app, prefer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Platform-native storage&lt;/strong&gt; (Android Keystore and iOS Keychain), or a maintained KMP wrapper around them.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A vetted crypto library&lt;/strong&gt; (platform providers or a reviewed multiplatform library) instead of custom code.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server-verified attestation&lt;/strong&gt; (Play Integrity on Android, App Attest on iOS) for device and app trust.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use this article to understand where each protection belongs, not to replace tested libraries. If you want to see a crypto protocol implemented from a specification in Kotlin, I've published an SDK implementing MTProto 2.0 primitives (AES-IGE and Diffie-Hellman key exchange): &lt;a href="https://github.com/gargpadmakar/MTProtoCrypto" rel="noopener noreferrer"&gt;github.com/gargpadmakar/MTProtoCrypto&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Shared code is not secure by default. Each platform has its own attack surface.&lt;/li&gt;
&lt;li&gt;Keep flows and decisions in &lt;code&gt;commonMain&lt;/code&gt;; keep keys and trust anchors in platform code.&lt;/li&gt;
&lt;li&gt;Store secrets with Android Keystore and iOS Keychain behind one common interface.&lt;/li&gt;
&lt;li&gt;Return an explicit result (&lt;code&gt;OK&lt;/code&gt;, &lt;code&gt;NOT_FOUND&lt;/code&gt;, &lt;code&gt;KEY_INVALIDATED&lt;/code&gt;, &lt;code&gt;FAILURE&lt;/code&gt;) instead of a nullable string, so common code can react correctly.&lt;/li&gt;
&lt;li&gt;Wipe and re-enroll on an invalidated key, and never on a transient failure.&lt;/li&gt;
&lt;li&gt;Pin TLS certificates per Ktor engine, and always ship a backup pin.&lt;/li&gt;
&lt;li&gt;Use AES-GCM with a fresh nonce every time, and never write your own crypto.&lt;/li&gt;
&lt;li&gt;Never ship long-lived secrets in the binary; use short-lived tokens from your backend.&lt;/li&gt;
&lt;li&gt;Verify device integrity on the server with Play Integrity and App Attest.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Related reading
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/kotlin-multiplatform-for-android-developers-share-your-business-logic-with-ktor-koin-and-clean-5hhl"&gt;Kotlin Multiplatform for Android Developers: Share Your Business Logic with Ktor, Koin and Clean Architecture&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/how-end-to-end-encryption-works-in-an-android-chat-app-ecdh-aes-gcm-in-kotlin-4g72"&gt;How End-to-End Encryption Works in an Android Chat App (ECDH + AES-GCM in Kotlin)&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/gargpadmakar/building-a-cyber-threat-task-platform-with-kotlin-multiplatform-ktor-nodejs-and-react-strict-11d6"&gt;Building a Cyber Threat Task Platform with Kotlin Multiplatform, Ktor, Node.js and React (Strict 3-Role RBAC)&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;About the author:&lt;/strong&gt; Padmakar Garg is a Senior Android Developer with 8+ years of experience in Kotlin, Jetpack Compose, Kotlin Multiplatform and mobile security, building real-time encrypted messaging apps. Portfolio and blog: &lt;a href="https://gargpadmakar.github.io/blog/" rel="noopener noreferrer"&gt;gargpadmakar.github.io&lt;/a&gt;. Connect on &lt;a href="https://linkedin.com/in/padmakar-garg" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://github.com/gargpadmakar" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>kotlin</category>
      <category>kmp</category>
      <category>security</category>
      <category>android</category>
    </item>
    <item>
      <title>How End-to-End Encryption Works in an Android Chat App (ECDH + AES-GCM in Kotlin)</title>
      <dc:creator>Padmakar Garg</dc:creator>
      <pubDate>Wed, 25 Nov 2026 07:28:30 +0000</pubDate>
      <link>https://dev.to/gargpadmakar/how-end-to-end-encryption-works-in-an-android-chat-app-ecdh-aes-gcm-in-kotlin-4g72</link>
      <guid>https://dev.to/gargpadmakar/how-end-to-end-encryption-works-in-an-android-chat-app-ecdh-aes-gcm-in-kotlin-4g72</guid>
      <description>&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt; end-to-end encryption (E2EE) means only the two people chatting can read the messages, not even your server. Each device creates a key pair, they exchange &lt;strong&gt;public&lt;/strong&gt; keys, each side computes the same &lt;strong&gt;shared secret&lt;/strong&gt; with ECDH, derives an encryption key from it, and encrypts every message with an authenticated cipher such as AES-GCM. The server only ever sees ciphertext.&lt;/p&gt;

&lt;p&gt;This article explains each step in plain language and shows the Kotlin code using only standard Android APIs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I'm Padmakar Garg, a Senior Android Developer. I work on secure messaging, encrypted calling and Android security. The code below is for &lt;strong&gt;understanding&lt;/strong&gt; how E2EE works. For production, use a vetted protocol and library (see the end).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The big picture
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice                                   Server                                   Bob
 |  generate key pair (A_priv, A_pub)     |     generate key pair (B_priv, B_pub)   |
 |---- A_pub ----------------------------&amp;gt;|&amp;lt;--------------------------- B_pub ------|
 |&amp;lt;--- B_pub -----------------------------|-----------------------------&amp;gt; A_pub ---|
 |  secret = ECDH(A_priv, B_pub)          |       secret = ECDH(B_priv, A_pub)      |
 |  (both get the SAME secret)            |                                         |
 |  key = HKDF(secret)                    |       key = HKDF(secret)                |
 |---- AES-GCM(key, message) ------------&amp;gt;|--------- ciphertext only --------------&amp;gt;|
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The magic is that Alice and Bob end up with the same secret without ever sending it. The server relays public keys and ciphertext, and cannot compute the secret.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Generate a key pair
&lt;/h2&gt;

&lt;p&gt;Each device creates an elliptic-curve key pair. The private key never leaves the device.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;KeyPair&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;KeyPairGenerator&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ECGenParameterSpec&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;generateKeyPair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;KeyPair&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;generator&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KeyPairGenerator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"EC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ECGenParameterSpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"secp256r1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;generator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateKeyPair&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To send the public key to the other user, encode it to bytes. To receive one, decode it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;KeyFactory&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PublicKey&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;X509EncodedKeySpec&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nc"&gt;PublicKey&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;encoded&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;publicKeyFromBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PublicKey&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nc"&gt;KeyFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"EC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generatePublic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;X509EncodedKeySpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: Compute the shared secret with ECDH
&lt;/h2&gt;

&lt;p&gt;Elliptic-curve Diffie-Hellman (ECDH) combines &lt;strong&gt;your private key&lt;/strong&gt; with &lt;strong&gt;their public key&lt;/strong&gt;. The maths guarantees both sides get the same result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;javax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;KeyAgreement&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;PrivateKey&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;sharedSecret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myPrivate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PrivateKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;theirPublic&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PublicKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;agreement&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;KeyAgreement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"ECDH"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;agreement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;myPrivate&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;agreement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doPhase&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;theirPublic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;agreement&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;generateSecret&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alice calls &lt;code&gt;sharedSecret(A_priv, B_pub)&lt;/code&gt; and Bob calls &lt;code&gt;sharedSecret(B_priv, A_pub)&lt;/code&gt;. The two byte arrays are identical.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Derive a proper encryption key
&lt;/h2&gt;

&lt;p&gt;Never use the raw ECDH output as an AES key. Run it through a key derivation function (KDF) such as HKDF. Here is a small HMAC-SHA256 based version for a 32-byte key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;javax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Mac&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;javax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SecretKeySpec&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;hkdfSha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;mac&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"HmacSHA256"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Extract&lt;/span&gt;
    &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecretKeySpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"HmacSHA256"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;prk&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Expand (one block is enough for a 32-byte key)&lt;/span&gt;
    &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SecretKeySpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prk&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"HmacSHA256"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;info&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toByte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mac&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;hkdfSha256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"chat-v1"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;info&lt;/code&gt; string binds the key to its purpose, so the same secret can never be reused accidentally for something else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 4: Encrypt with AES-GCM
&lt;/h2&gt;

&lt;p&gt;AES-GCM gives you &lt;strong&gt;confidentiality and integrity&lt;/strong&gt; in one operation. If anyone modifies the ciphertext, decryption fails.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SecureRandom&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;javax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="n"&gt;javax&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;spec&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;GCMParameterSpec&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;encrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;aad&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;also&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nc"&gt;SecureRandom&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AES/GCM/NoPadding"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;ENCRYPT_MODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SecretKeySpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"AES"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;GCMParameterSpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateAAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;aad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;plaintext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;   &lt;span class="c1"&gt;// prepend the IV&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;decrypt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;aad&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ByteArray&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copyOfRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copyOfRange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AES/GCM/NoPadding"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;init&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="no"&gt;DECRYPT_MODE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;SecretKeySpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"AES"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;GCMParameterSpec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;128&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;updateAAD&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;aad&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;doFinal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use the chat ID and message ID as the &lt;code&gt;aad&lt;/code&gt; (additional authenticated data). That way a ciphertext cannot be moved to another conversation without detection.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistakes that quietly break E2EE
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Reusing an IV with the same key
&lt;/h3&gt;

&lt;p&gt;In GCM, one repeated IV with the same key can leak plaintext relationships and break authentication. Use a fresh random 12-byte IV for each message, or a strict counter, and rotate keys regularly.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Trusting the server for public keys
&lt;/h3&gt;

&lt;p&gt;If the server hands you the wrong public key, it can sit in the middle and read everything (a man-in-the-middle attack). Let users compare a &lt;strong&gt;fingerprint&lt;/strong&gt; (for example a hash of both public keys) out of band:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;fingerprint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PublicKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;PublicKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;listOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encoded&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;encoded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sortedBy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contentHashCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="n"&gt;digest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;java&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;security&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;MessageDigest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getInstance&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"SHA-256"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;forEach&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;joinToString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s2"&gt;"%02x"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;it&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;take&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Show it as a "safety number" that two people can compare in person or on a call.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. No forward secrecy
&lt;/h3&gt;

&lt;p&gt;If one long-term key leaks, an attacker could decrypt all past messages. Use &lt;strong&gt;ephemeral&lt;/strong&gt; keys per session and rotate them as messages flow. The Signal protocol does this with the Double Ratchet algorithm.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Storing private keys carelessly
&lt;/h3&gt;

&lt;p&gt;Never keep private keys in SharedPreferences or a plain file. Use the &lt;strong&gt;Android Keystore&lt;/strong&gt; where supported. ECDH operations inside the Keystore need a recent Android version, so check the documentation for the API level you support.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Believing the metadata is hidden
&lt;/h3&gt;

&lt;p&gt;E2EE protects message &lt;strong&gt;content&lt;/strong&gt;. The server can still see who talks to whom, when and how often, unless you design for that separately.&lt;/p&gt;

&lt;h2&gt;
  
  
  What to use in production
&lt;/h2&gt;

&lt;p&gt;Writing your own protocol is risky, because tiny mistakes are invisible and fatal. For a real app, prefer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the &lt;strong&gt;Signal protocol&lt;/strong&gt; libraries,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Google Tink&lt;/strong&gt; for safe cryptographic primitives,&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;libsodium&lt;/strong&gt; for modern, hard-to-misuse APIs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Use the code in this article to understand what those libraries do, not to replace them. If you want to see a protocol implemented from a specification, I've published a Kotlin SDK implementing the MTProto 2.0 primitives (AES-IGE and Diffie-Hellman key exchange) here: &lt;a href="https://github.com/gargpadmakar/MTProtoCrypto" rel="noopener noreferrer"&gt;github.com/gargpadmakar/MTProtoCrypto&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Each device makes a key pair, and only public keys are exchanged.&lt;/li&gt;
&lt;li&gt;ECDH gives both sides the same shared secret without sending it.&lt;/li&gt;
&lt;li&gt;Run the secret through a KDF to get the real AES key.&lt;/li&gt;
&lt;li&gt;Encrypt each message with AES-GCM, a fresh IV and the chat context as AAD.&lt;/li&gt;
&lt;li&gt;Defend against man-in-the-middle with fingerprints, and aim for forward secrecy.&lt;/li&gt;
&lt;li&gt;In production, use Signal protocol, Tink or libsodium instead of rolling your own.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;About the author:&lt;/strong&gt; Padmakar Garg is a Senior Android Developer with 8+ years of experience in Kotlin, Jetpack Compose and mobile security, building real-time encrypted messaging apps. Portfolio and blog: &lt;a href="https://gargpadmakar.github.io/" rel="noopener noreferrer"&gt;gargpadmakar.github.io&lt;/a&gt;. Connect on &lt;a href="https://linkedin.com/in/padmakar-garg" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>android</category>
      <category>kotlin</category>
      <category>security</category>
      <category>cryptography</category>
    </item>
  </channel>
</rss>
