Instacart Colin White Oct 16, 2018

Using Kotlin extensions to make the Android SDK friendlier

M2 Related OWASP risk: Inadequate Supply Chain Security Learn more →

Article Summary

The Android SDK's rough edges force developers into verbose workarounds. Kotlin extensions offer a cleaner path forward.

Colin White from Instacart breaks down how Kotlin extension functions improve Android SDK discoverability and developer experience. He shares practical extensions that simplify common pain points, building on concepts introduced in Google's Android KTX.

Key Takeaways

Critical Insight

Kotlin extensions transform clunky Android SDK patterns into fluent, discoverable APIs that autocomplete naturally in Android Studio.

The article includes a full compilation of production-ready extensions you can drop into your project today.

About This Article

Problem

Android developers have to deal with API version fragmentation. Deprecated methods like resources.getDrawable() require conditional SDK_INT checks, and developers need to know about scattered compat utility classes like ResourcesCompat and AppCompatResources.

Solution

Colin White and Instacart used Kotlin extension functions to wrap AndroidX compat functions into fluent APIs. This lets Android Studio autocomplete discover them without needing version-specific implementation details.

Impact

Extensions like tint, toActivity, and openWebPage cut down boilerplate across all API levels. The unsafeLazy extension removes unnecessary thread-safe locks on main-thread variables. The result is cleaner code and better runtime performance.