<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pouya Heydari on Medium]]></title>
        <description><![CDATA[Stories by Pouya Heydari on Medium]]></description>
        <link>https://medium.com/@pouyaheydari?source=rss-b2cac7bd3456------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CqqehVDyb8kSysRR01RnIA.png</url>
            <title>Stories by Pouya Heydari on Medium</title>
            <link>https://medium.com/@pouyaheydari?source=rss-b2cac7bd3456------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 00:31:55 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pouyaheydari/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Encrypt Your Room Database in Android Using SQLCipher ]]></title>
            <link>https://proandroiddev.com/how-to-encrypt-your-room-database-in-android-using-sqlcipher-0bce78328bd6?source=rss-b2cac7bd3456------2</link>
            <guid isPermaLink="false">https://medium.com/p/0bce78328bd6</guid>
            <category><![CDATA[andoid]]></category>
            <category><![CDATA[room]]></category>
            <category><![CDATA[android-app-development]]></category>
            <category><![CDATA[sqlcipher]]></category>
            <category><![CDATA[database]]></category>
            <dc:creator><![CDATA[Pouya Heydari]]></dc:creator>
            <pubDate>Sat, 26 Jul 2025 20:18:31 GMT</pubDate>
            <atom:updated>2026-05-24T13:01:54.270Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*1KtgiKt0U7sRRxvb" /><figcaption>hoto by <a href="https://unsplash.com/@markuswinkler?utm_source=medium&amp;utm_medium=referral">Markus Winkler</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Database Security on Android: Part I: Room Database Encryption 🔐</h3><p>When building Android apps, we often use Room to store data locally. But have you ever thought about how easy it might be for someone to extract that data if your app gets reverse engineered?</p><p>Local databases can be a goldmine for attackers. That’s why encrypting your Room database is a solid step toward safeguarding user data. In this article, I’ll walk you through a production-ready and secure approach to encrypt your Room database using SQLCipher, Android Keystore, and a bit of cryptographic hygiene.</p><h3>🧱 Why Encrypt Room?</h3><p>Room doesn’t support encryption out of the box. But you can use <a href="https://github.com/sqlcipher/android-database-sqlcipher">SQLCipher for Android</a>, a drop-in replacement for SQLite with built-in encryption capabilities. It’s reliable, actively maintained, and widely trusted.</p><h3>⚙️ Step 1: Add Dependencies</h3><p>Start by adding the necessary dependencies in your build.gradle file:</p><pre>// SQLCipher<br>implementation &quot;net.zetetic:sqlcipher-android:4.9.0&quot; // or latest<br><br>// Room<br>implementation(&quot;androidx.room:room-runtime:$room_version&quot;)<br>ksp(&quot;androidx.room:room-compiler:$room_version&quot;)</pre><h3>🔑 Step 2: Securely Generate an Encryption Key</h3><p>Don’t hardcode passwords! Instead, generate a random 256-bit key at runtime and protect it with the Android Keystore.</p><p>Here’s the <em>wrong</em> way to do it:</p><pre>val key = SQLiteDatabase.getBytes(&quot;your-password&quot;.toCharArray())</pre><p>This is insecure — the password can be extracted from your APK.</p><p>The better way:</p><pre>val sqlCipherKey = ByteArray(32)<br>SecureRandom().nextBytes(sqlCipherKey)</pre><p>But we still need to <strong>store this securely</strong>. SharedPreferences alone is not safe enough.</p><h3>🏦 Step 3: Use Android Keystore to Protect the Key</h3><p>We’ll use the Android Keystore to encrypt the generated key before storing it.</p><h3>Create a SqlCipherKeyManager:</h3><pre>class SqlCipherKeyManager constructor(<br>    private val sharedPreferences: SharedPreferences<br>) {<br>    private val keyStore: KeyStore = KeyStore.getInstance(&quot;AndroidKeyStore&quot;).apply { load(null) }<br><br>    init {<br>        initialize()<br>    }<br><br>    private fun initialize() {<br>        generateKeystoreKeyIfNeeded()<br>        if (!sharedPreferences.contains(&quot;encrypted_key&quot;)) {<br>            generateAndEncryptSqlCipherKey()<br>        }<br>    }<br><br>    private fun generateKeystoreKeyIfNeeded() {<br>        if (!keyStore.containsAlias(&quot;sqlcipher_keystore_key&quot;)) {<br>            val keyGenerator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, &quot;AndroidKeyStore&quot;)<br>            val keyGenSpec = KeyGenParameterSpec.Builder(<br>                &quot;sqlcipher_keystore_key&quot;,<br>                KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT<br>            )<br>                .setBlockModes(KeyProperties.BLOCK_MODE_GCM)<br>                .setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)<br>                .build()<br>            keyGenerator.init(keyGenSpec)<br>            keyGenerator.generateKey()<br>        }<br>    }<br><br>    private fun generateAndEncryptSqlCipherKey() {<br>        val secretKey = getSecretKey(&quot;sqlcipher_keystore_key&quot;)<br>        val cipher = Cipher.getInstance(&quot;AES/GCM/NoPadding&quot;)<br>        cipher.init(Cipher.ENCRYPT_MODE, secretKey)<br><br>        val sqlCipherKey = ByteArray(32)<br>        SecureRandom().nextBytes(sqlCipherKey)<br><br>        val encryptedKey = cipher.doFinal(sqlCipherKey)<br>        val iv = cipher.iv<br><br>        sharedPreferences.edit {<br>            putString(&quot;encrypted_key&quot;, Base64.encodeToString(encryptedKey, Base64.NO_WRAP))<br>            putString(&quot;encryption_iv&quot;, Base64.encodeToString(iv, Base64.NO_WRAP))<br>        }<br>    }<br><br>    private fun getDecryptedSqlCipherKey(keyAlias: String, key: String, iv: String): ByteArray {<br>        val encryptedKey = Base64.decode(key, Base64.NO_WRAP)<br>        val ivBytes = Base64.decode(iv, Base64.NO_WRAP)<br><br>        val secretKey = getSecretKey(keyAlias)<br>        val cipher = Cipher.getInstance(&quot;AES/GCM/NoPadding&quot;)<br>        cipher.init(Cipher.DECRYPT_MODE, secretKey, GCMParameterSpec(128, ivBytes))<br><br>        return cipher.doFinal(encryptedKey)<br>    }<br><br>    private fun getSecretKey(keyAlias: String): SecretKey =<br>        (keyStore.getEntry(keyAlias, null) as KeyStore.SecretKeyEntry).secretKey<br><br>    fun getSupportFactory(): SupportOpenHelperFactory {<br>        val encryptedKey = sharedPreferences.getString(&quot;encrypted_key&quot;, null).orEmpty()<br>        val iv = sharedPreferences.getString(&quot;encryption_iv&quot;, null).orEmpty()<br>        val decryptedKey = getDecryptedSqlCipherKey(&quot;sqlcipher_keystore_key&quot;, encryptedKey, iv)<br>        return SupportOpenHelperFactory(decryptedKey)<br>    }<br>}</pre><blockquote><em>⚠️ Do not call </em>fill(0)<em> on the decryptedKey array before passing it to </em>SupportOpenHelperFactory<em>, and do not wrap the factory to zero it after </em>create()<em>. Room opens the database lazily, and the array is held by reference. Zeroing it would cause </em>SQLCipher<em> to encrypt the database with an all‑zero key. (</em>h/t to <a href="https://medium.com/@devon_49922/tldr-skip-the-decryptedkey-fill-0-it-causes-worse-security-vulnerabilities-than-it-solves-626b6d84ac16">this comment</a> for flagging this.<em>)</em></blockquote><h3>🏗 Step 4: Wire It All Up with Room</h3><p>Now, initialize Room with your encrypted SupportFactory:</p><pre>System.loadLibrary(&quot;sqlcipher&quot;)<br>val sqlCipherKeyManager = SqlCipherKeyManager(sharedPreferences)<br>val dbFile = context.getDatabasePath(&quot;your-db-name&quot;)<br>val db = Room.databaseBuilder(<br>    context,<br>    AppDatabase::class.java,<br>    dbFile.absolutePath<br>)<br>    .openHelperFactory(sqlCipherKeyManager.getSupportFactory())<br>    .build()</pre><h3>✅ Final Notes</h3><ul><li>This approach encrypts your entire Room database with AES-256 via SQLCipher.</li><li>The encryption key is randomly generated and protected using the Android Keystore.</li><li>The decrypted key remains in memory only for the lifetime of the open SQLCipher connection, where it is required to encrypt and decrypt database pages.</li></ul><blockquote><em>⚠️ </em><strong><em>Caution</em></strong><em>: Even with these protections, storing highly sensitive data on-device should still be avoided when possible.</em></blockquote><h3>🏁 Summary</h3><p>With just a few classes and some careful handling of encryption keys, you can dramatically increase the security of your app’s local storage.</p><p>This solution is production-grade, leverages well-tested cryptographic primitives, and keeps your users’ data safer against reverse engineering and local tampering.</p><h3>⛓️‍💥 Links</h3><ul><li><a href="https://developer.android.com/privacy-and-security/keystore">Android KeyStore</a></li><li><a href="https://developer.android.com/reference/javax/crypto/Cipher">Cipher</a></li><li><a href="https://docs.oracle.com/javase/8/docs/api/java/security/SecureRandom.html">SecureRandom</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=0bce78328bd6" width="1" height="1" alt=""><hr><p><a href="https://proandroiddev.com/how-to-encrypt-your-room-database-in-android-using-sqlcipher-0bce78328bd6">How to Encrypt Your Room Database in Android Using SQLCipher 🔐</a> was originally published in <a href="https://proandroiddev.com/">ProAndroidDev</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to Add a Scrollbar to Jetpack Compose Lists in Wear OS]]></title>
            <link>https://medium.com/@pouyaheydari/how-to-add-a-scrollbar-to-jetpack-compose-lists-in-wear-os-7144d2008834?source=rss-b2cac7bd3456------2</link>
            <guid isPermaLink="false">https://medium.com/p/7144d2008834</guid>
            <category><![CDATA[android-watch]]></category>
            <category><![CDATA[jetpack-compose]]></category>
            <category><![CDATA[horologist]]></category>
            <category><![CDATA[android-wear]]></category>
            <category><![CDATA[android-app-development]]></category>
            <dc:creator><![CDATA[Pouya Heydari]]></dc:creator>
            <pubDate>Sun, 16 Mar 2025 18:37:53 GMT</pubDate>
            <atom:updated>2025-03-16T18:37:53.739Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*6IIzYgL6KUxelFlK" /><figcaption>Photo by <a href="https://unsplash.com/@rawmurat?utm_source=medium&amp;utm_medium=referral">Murat Caliskan</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>If you’re developing a Wear OS app for Android smartwatches, there’s a high chance your app will get rejected if it contains a list without a visible scrollbar. You might receive this rejection reason from Google Play:</p><blockquote><em>“Your app does not display the scroll bar when the user interacts with a scrollable view.”</em></blockquote><p>The reason for this requirement is obvious — users need clear visual feedback when scrolling. However, the real challenge begins when you try to find a solution. Surprisingly, there’s very little information available! Even the official Jetpack Compose documentation and Stack Overflow don’t offer clear guidance.</p><p>But don’t worry! In this article, I’ll show you two simple ways to add a scrollbar to your Wear OS Compose app:</p><ol><li>Using <strong>Jetpack Compose</strong> itself</li><li>Using <strong>Horologist</strong>, a Google-backed library designed for Wear OS development</li></ol><h3>🛠️ Method 1: Using Jetpack Compose</h3><p>Jetpack Compose provides built-in support for scrollbars in Wear OS apps. Here’s how you can implement it.</p><h3>Step 1: Add Dependencies</h3><p>Make sure you have the necessary Wear OS Compose dependencies in your build.gradle:</p><pre>androidx-compose-material = { group = &quot;androidx.wear.compose&quot;, name = &quot;compose-material&quot;, version.ref = &quot;composeWear&quot; }<br>androidx-compose-foundation = { group = &quot;androidx.wear.compose&quot;, name = &quot;compose-foundation&quot;, version.ref = &quot;composeWear&quot; }</pre><h3>Step 2: Create a Scroll State</h3><p>Inside your composable function, define a ScalingLazyListState to track the scroll position:</p><pre>val scrollState = rememberScalingLazyListState()</pre><h3>Step 3: Wrap Your List with a Scaffold</h3><p>Use Scaffold and provide a PositionIndicator to display the scrollbar:</p><pre>Scaffold(positionIndicator = {<br>    PositionIndicator(scalingLazyListState = scrollState)<br>}) {<br>    ScalingLazyColumn(<br>        modifier = Modifier.fillMaxWidth(),<br>        state = scrollState,<br>    ) {<br>        item { /* List content */ }<br>    }<br>}</pre><h3>🔍 How It Works</h3><p>The PositionIndicator composable creates a scrollbar for your list. We simply pass the scrollState to it, so it knows when to update the scrollbar position dynamically.</p><h3>🔹 Method 2: Using Horologist</h3><p><a href="https://github.com/google/horologist">Horologist</a> is a set of libraries from Google that simplifies Wear OS development. It includes tools to ensure apps meet Google Play requirements — like adding a scrollbar.</p><h3>Step 1: Add Dependencies</h3><p>First, add the necessary Horologist dependencies:</p><pre>horologist-compose-material = { module = &quot;com.google.android.horologist:horologist-compose-material&quot;, version.ref = &quot;horologist&quot; }<br>horologist-compose-layout = { group = &quot;com.google.android.horologist&quot;, name = &quot;horologist-compose-layout&quot;, version.ref = &quot;horologist&quot; }</pre><h3>Step 2: Create a Scroll State</h3><p>Instead of ScalingLazyListState, Horologist uses rememberResponsiveColumnState:</p><pre>val scrollState = rememberResponsiveColumnState()</pre><h3>Step 3: Wrap Your List with ScreenScaffold</h3><p>Use Horologist’s ScreenScaffold and pass the scroll state:</p><pre>ScreenScaffold(scrollState = scrollState) {<br>    ScalingLazyColumn(<br>        modifier = Modifier.fillMaxWidth(),<br>        columnState = scrollState,<br>    ) {<br>        item { /* List content */ }<br>    }<br>}</pre><h3>🔍 How It Works</h3><p>Horologist’s ScreenScaffold automatically integrates with Wear OS’s UI guidelines. It ensures your scrollbar behaves as expected, making this method a great choice if you’re using Horologist elsewhere in your project.</p><h3>🎯 Which Method Should You Use?</h3><p>Both methods work well, but here’s when you should choose each:</p><p>✅ <strong>Use Jetpack Compose</strong> if you want a lightweight solution without adding extra dependencies.</p><p>✅ <strong>Use Horologist</strong> if your app already depends on it, or you want an easier way to comply with Google’s guidelines.</p><p>Either way, now you can ensure your Wear OS app displays a scrollbar properly — so you won’t face rejection from Google Play! 🎉</p><h3>🚀 Conclusion</h3><p>Adding a scrollbar to your Wear OS app is <strong>mandatory</strong> for approval on Google Play, yet the solution isn’t well-documented. Fortunately, Jetpack Compose and Horologist provide easy ways to implement it. Whether you choose the Compose-only approach or leverage Horologist, you can now ensure your app is visually polished and compliant with Wear OS design guidelines.</p><p>Now go ahead and update your Wear OS app with a scrollbar! 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7144d2008834" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Koin in Kotlin Multiplatform: Handling Platform-Specific Dependencies]]></title>
            <link>https://medium.com/@pouyaheydari/koin-in-kotlin-multiplatform-handling-platform-specific-dependencies-2a2e1dbd48b6?source=rss-b2cac7bd3456------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a2e1dbd48b6</guid>
            <category><![CDATA[dependency-injection]]></category>
            <category><![CDATA[koin]]></category>
            <category><![CDATA[kotlin-multiplatform]]></category>
            <category><![CDATA[android]]></category>
            <category><![CDATA[kmp]]></category>
            <dc:creator><![CDATA[Pouya Heydari]]></dc:creator>
            <pubDate>Sat, 08 Mar 2025 17:36:41 GMT</pubDate>
            <atom:updated>2025-03-08T17:36:41.700Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*rVSwU01Wg1vp-PGW" /><figcaption>Photo by <a href="https://unsplash.com/@mreichelt?utm_source=medium&amp;utm_medium=referral">Marc Reichelt</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>We all know why dependency injection (DI) is essential and the benefits it brings to our projects, so let’s skip the introduction and dive straight into a common issue and its solution.</p><h3>The Challenge: Platform-Specific Dependencies in KMP</h3><p>When working on Kotlin Multiplatform (KMP) projects, we often need to provide dependencies whose concrete implementations differ across platforms (e.g., Android and iOS). A typical approach is to use expect functions in the shared code and implement them separately for each platform.</p><p>For example, as soon as you create a new multiplatform project, one of the first generated sample classes is the Platform interface:</p><p><strong>Common Code:</strong></p><pre>interface Platform {<br>    val name: String<br>}<br><br>expect fun getPlatform(): Platform</pre><p><strong>Android implementation:</strong></p><pre>import android.os.Build<br><br>class AndroidPlatform : Platform {<br>    override val name: String = &quot;Android ${Build.VERSION.SDK_INT}&quot;<br>}<br><br>actual fun getPlatform(): Platform = AndroidPlatform()</pre><p><strong>iOS Implementation:</strong></p><pre>import platform.UIKit.UIDevice<br><br>class IOSPlatform: Platform {<br>    override val name: String = UIDevice.currentDevice.systemName() + &quot; &quot; + UIDevice.currentDevice.systemVersion<br>}<br>actual fun getPlatform(): Platform = IOSPlatform()</pre><p>So far, so good! But what happens when you’re using <strong>Koin</strong> for dependency injection and need to provide a dependency to the shared code with different implementations for Android and iOS?</p><h3>Example: Providing DataStore in KMP</h3><p>Imagine we want to use <strong>DataStore</strong> to store small pieces of data on the device. Since DataStore implementations differ on Android and iOS, we need platform-specific configurations.</p><p><strong>Android Implementation:</strong></p><pre>import android.content.Context<br>import androidx.datastore.core.DataStore<br>import androidx.datastore.preferences.core.Preferences<br><br>fun createDataStore(context: Context): DataStore&lt;Preferences&gt; = createDataStore(<br>    producePath = { context.filesDir.resolve(dataStoreFileName).absolutePath }<br>)</pre><p><strong>iOS Implementation:</strong></p><pre>import androidx.datastore.core.DataStore<br>import androidx.datastore.preferences.core.Preferences<br>import kotlinx.cinterop.ExperimentalForeignApi<br>import platform.Foundation.NSDocumentDirectory<br>import platform.Foundation.NSFileManager<br>import platform.Foundation.NSURL<br>import platform.Foundation.NSUserDomainMask<br><br>@OptIn(ExperimentalForeignApi::class)<br>fun createDataStore(): DataStore&lt;Preferences&gt; = createDataStore(<br>    producePath = {<br>        val documentDirectory: NSURL? = NSFileManager.defaultManager.URLForDirectory(<br>            directory = NSDocumentDirectory,<br>            inDomain = NSUserDomainMask,<br>            appropriateForURL = null,<br>            create = false,<br>            error = null,<br>        )<br>        requireNotNull(documentDirectory).path + &quot;/$dataStoreFileName&quot;<br>    },<br>)</pre><p>Now, the question is: <strong>How do we provide this dependency using Koin in our shared code?</strong></p><h3>The Solution: Using expect/actual with Koin Modules</h3><p>We can combine <strong>expect/actual functions</strong> with <strong>Koin&#39;s </strong><strong>module {} function</strong> to define a module in the shared code, while implementing it differently for Android and iOS.</p><p><strong>Step 1: Declare </strong><strong>expect Function in Shared Code</strong></p><pre>import org.koin.core.module.Module<br><br>expect fun dataModule() : Module</pre><p><strong>Step 2: Implement </strong><strong>actual Function in Android</strong></p><pre>import org.koin.android.ext.koin.androidApplication<br>import org.koin.core.module.Module<br>import org.koin.dsl.module<br><br>actual fun dataModule(): Module = module {<br>    single { createDataStore(androidApplication()) }<br>}</pre><p><strong>Step 3: Implement </strong><strong>actual Function in iOS</strong></p><pre>import org.koin.core.module.Module<br>import org.koin.dsl.module<br><br>actual fun dataModule(): Module = module {<br>    single { createDataStore() }<br>}</pre><p><strong>Step 4: Register the Module in Koin</strong></p><pre>fun initKoin(appDeclaration: KoinAppDeclaration = {}) = startKoin {<br>    appDeclaration()<br>    modules(dataModule())<br>}</pre><p>That’s it! Now <strong>Koin knows how to provide the correct DataStore implementation</strong> in your shared code, allowing you to inject it seamlessly into your repositories or ViewModels.</p><p>Based on your need, you can either use single{} or factory{} when providing the dependency.</p><h3>Conclusion</h3><p>This approach enables us to:</p><ul><li><strong>Leverage Koin for dependency injection in KMP.</strong></li><li><strong>Keep the shared code clean and platform-agnostic.</strong></li><li><strong>Easily manage platform-specific implementations.</strong></li></ul><p>I hope this article helps you with DI in KMP! Feel free to share your thoughts or ask questions in the comments. 🚀</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a2e1dbd48b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>