Function Properties in Data Classes are Code Smells

To me, using functions as properties in the primary constructor of a data class is a code smell. Here’s why: Data classes represent data. Data is a value. Data is never executed. Functions are not data. They produce values when executed. Note: By the book, a function returns a value, while a procedure executes commands. In both cases, neither is data. Why It Matters Kotlin generates key methods for data classes based on the properties in the primary constructor, such as:...

November 29, 2024

Robolectric in commonTest

Sharing tests across Kotlin Multiplatform (KMP) projects can be tricky when dealing with platform-specific APIs like Android’s Bundle. commonTest on Android relies on the androidTest source set, which uses an empty android.jar, leading to test failures. The Solution The ideal solution is to avoid platform-specific APIs in commonTest. If that’s not an option, you can use Robolectric in your commonTest source set to access functional Android classes. This approach: Allows testing of platform-specific code in commonTest....

November 28, 2024

Extension Shadowing for Actual Declarations in KMP

Heads-up: this article assumes familiarity with Kotlin’s extension functions and expect and actual declarations in Kotlin Multiplatform (KMP). My work has recently focused on “commonizing”1 APIs, and I came across KT-70012, which I believe merits attention. In Kotlin JVM development, the EXTENSION_SHADOWED_BY_MEMBER warning indicates that an extension function is redundant, as it will always be overshadowed by a member function with the same name when invoked. However, in KMP, this behaviour can be useful, as shadowing may occur on some platforms but not all....

November 7, 2024

No Mocks Allowed

Disclaimer: It has been brought to my attention the title can be seen as a click bait. That wasn’t my intention and I’m sorry. I wanted to reference the Fallout game series and the No Mutants Allowed community. Testable code plays a crucial role in app development. When we neglect designing code for testability, we often resort to using a mock library (such as Mockito Kotlin, also know as “auto-mockers”) as a mean to achieve test coverage....

June 28, 2023

Injection Points

Android has made significant progress in becoming a DI-Friendly Framework. Throughout the years, new APIs like AppComponentFactory and FragmentFactory have been introduced, allowing apps to incorporate their own custom constructors and facilitating the development of testable code. The purpose of this article is to highlight some of the Android APIs that are utilised behind the scenes. By exploring these APIs, developers can gain a better understanding of the underlying mechanisms employed by popular libraries (such as Dagger Hilt and Koin Android)....

June 2, 2023

Trampoline Activities

Today, I came across the term “Trampoline Activities”. Although it’s not an official name (or is it?), I’ve noticed it is a typical pattern and have decided to make a “How To” guide. What are Trampoline Activities? A Trampoline Activity is an Activity that launches another activity and finishes itself. It may include conditional logic to determine which activity to launch or transforming the parameters before sending it to the next Activity....

March 6, 2023

Using Compose Beta on AS 4.1

Jetpack Compose hit Beta! Many teams are excited to experiment with Compose, but as you might know, since 1.0.0-alpha04, the compiler has been refactored to a new group and became incompatible with the current Android Studio (AS) 4.1 stable: Compose Version 1.0.0-alpha04 is only compatible with Android Studio 4.2 Canary 13 and later. Been forced to use a Canary version of AS is a real bummer. There are cases in which you want to explore Compose in a real-world application (e....

March 30, 2021

N26 Path to Anvil

This post represents my personal experience while working at N26. I do not speak for the company nor by other employees. N26 Android App current codebase has a million lines of code, 280+ modules, and 30+ engineers working in 4 different countries and different timezones in a mono repository. Our modules are divided into features and libraries, and we have been using “Sample Apps” for years now as our full app build time might take up to 20 minutes....

February 5, 2021

Humble Views, Proud ViewModels

The Android Community has long advocated that Activities and Fragments were views - but this perception has changed over time. For good. Let’s dive deep into how to design views and view models, how they wire to a LifecycleOwner, and how this can positively impact your’s app testability. To better describe how to build humble views we will be developing an elementary Sign-Up form with an email, a password text field and two buttons: a cancel that pops the user’s back stack and a sign up that creates an account and moves the user to the home screen....

February 1, 2021

Naming Factory Methods

When discussing Factory Methods, extension functions are often preferred in Kotlin. However, naming these functions in a discoverable way without cluttering your project’s namespace can be challenging. A great source of inspiration is Kotlin’s Standard Library, which offers numerous examples to guide function design. Wrapping an Instance If you want to take an existing instance and adapt it to a different object to meet another contract—such as creating a ViewModelProvider.Factory that internally uses a javax....

February 1, 2020