Kotlin
225 bites tagged Kotlin — interview questions with model answers, and 60-second explainers.
Android Biometric Authentication: The BiometricPrompt API
Android's BiometricPrompt is a unified API for fingerprint, face, or iris scans, replacing older, fragmented methods. Use it to authorize payments or unlock features. The footgun: never assume a specific biometric type is available; always have a fallback.
EncryptedSharedPreferences: Secure Key-Value Storage on Android
EncryptedSharedPreferences is a drop-in replacement for SharedPreferences that automatically encrypts keys and values, protecting sensitive data at rest. Use it to store small, sensitive data like API tokens. The main footgun is mismanaging the master key.
Android App Startup: From Cold to Hot
App startup has three types: cold, warm, and hot. Optimization focuses on the cold start—when the app launches from scratch. This is critical for user retention, as slow launches are a common reason for uninstalls. The footgun: don't just test warm starts.
Memory Leaks: The Silent App Killer
A memory leak is when your app holds onto objects it no longer needs, preventing memory reclamation. This often happens with static references to UI elements or background tasks. The footgun: your app won't crash immediately, but will slow down and die later.
StrictMode: Your Main Thread's Watchdog
StrictMode is a developer tool that acts like a strict supervisor for your app's main thread, catching slow operations like disk or network access. It helps you find performance issues before they cause "Application Not Responding" errors.
Testing Coroutines: Control Time with `runTest`
Test asynchronous coroutine code as if it were synchronous. The `kotlinx-coroutines-test` library lets you control a virtual clock. Use `runTest` to wrap tests calling suspend functions, ensuring your test waits for async work before asserting results.
Robolectric: Run Android Unit Tests on the JVM
Robolectric runs Android unit tests on a regular JVM, not a slow emulator. Use it in CI/TDD to test logic like Activity lifecycles without the minutes-long build-deploy cycle. The footgun is over-mocking; Robolectric tests behavior, not just implementation.
Instrumentation Tests: Testing on a Real Android Device
Instrumentation tests are like test-driving your app on a real device or emulator. They verify code that depends on the Android framework, like UI interactions or sensor access.
Truth: Fluent Assertions for Clearer Tests
Truth makes tests read like English: `assertThat(actual).is...`. It provides clearer assertions than standard JUnit, with far more helpful failure messages, especially for collections. The main footgun is a potential Guava dependency conflict.
Handler and Looper: Android's Threading Backbone
A Looper is a worker that endlessly processes a queue of tasks for a thread. A Handler is how you give that worker a new task from another thread, like updating the UI. The footgun is blocking the main thread's Looper, which freezes your app.
Expedited Work: High-Priority Background Tasks in Android
Expedited work is for important, short tasks that must run quickly. It gets higher priority than regular background work without requiring a persistent notification. Use it for sending messages or saving notes.
AlarmManager: Scheduling Precise Future Tasks in Android
AlarmManager is Android's system-wide alarm clock for your app, letting you run code at a specific time, even if your app is closed. Use it for time-sensitive tasks like calendar notifications. The footgun: alarms are wiped on reboot and must be re-registered.
AsyncTask: The Deprecated Way to Do Background Work
AsyncTask was Android's original tool for short background tasks that update the UI. It was used for simple network requests or disk I/O. It's fully deprecated due to memory leaks and lifecycle issues; use Kotlin Coroutines instead.
Testing with Hilt: Swapping Dependencies for Isolation
Hilt testing swaps real app components for fakes, isolating your code under test without manual setup. It's used in UI and unit tests to replace production dependencies like network clients with test doubles.
@HiltViewModel: Simplified ViewModel Injection
@HiltViewModel automates creating ViewModels with dependencies. Instead of writing custom factories, just annotate the ViewModel and its constructor. It's the standard for injecting dependencies like repositories into ViewModels in a Hilt-powered Android app.
@HiltAndroidApp: The Entry Point for Hilt DI
@HiltAndroidApp is the main power switch for Hilt dependency injection. You place it on your Application class to trigger code generation and create the top-level dependency container.
Dependency Injection: Your Objects Shouldn't Create Their Own Helpers
Instead of creating its own helpers (like a network client), an object receives them from an outside source. This is crucial for building testable Android apps, as it lets you swap a real `UserRepository` with a fake one during tests.
OkHttp Caching: Beyond Simple Hits and Misses
OkHttp's cache acts like a browser's, saving network trips by storing responses on disk. It handles not just full hits and misses, but also validates stale data with the server. Use it for repeatable requests.
Moshi: Modern JSON for Kotlin & Android
Moshi is a modern JSON library that maps JSON strings to your Kotlin/Java objects. It's a translator between API text and your app's data classes, built with Kotlin-first features. The footgun: Kotlin classes require Moshi's codegen or reflection adapter.
Handling API State with a Sealed Class Wrapper
Treat API calls as states, not just data. A sealed class wrapper (`Success`, `Error`, `Loading`) models these states for your UI. Use this with Retrofit to show spinners or errors without crashing. The footgun: don't scatter try-catch blocks; centralize them.
OkHttp: A Smarter HTTP Client for Your App
OkHttp is a smart, resilient HTTP client for Android and Java. It automatically handles complexities like connection pooling, caching, and GZIP compression to make requests faster and more reliable.
Kotlinx.serialization: Kotlin's Native Data Converter
Kotlinx.serialization is Kotlin's native way to convert data classes into formats like JSON. It uses a compiler plugin to automate the process, making it ideal for API calls or saving data.
Retrofit: Turn Your HTTP API into an Interface
Retrofit turns your HTTP API into a simple interface, letting you define network calls as annotated methods. It's the standard for most Android network tasks. Footgun: forgetting to run calls off the main thread will crash your app.
Testing Room Databases: On-Device vs. Local JVM
Test your Room database in two ways: on an Android device for realism, or on your local JVM for speed. This is crucial for verifying DAO queries and preventing data bugs. The footgun is assuming local tests behave identically to on-device tests.
Get Kotlin bites daily.
Five a day, five minutes, offline. With quizzes so it sticks.
Open testing — you’ll join as an early tester.