Slide 1

Slide 1 text

KOTLIN GOTCHAS AND RANDOM RANTS GDG TORONTO ANDROID Jigar Brahmbhatt (@jabbar_jigariyo)

Slide 2

Slide 2 text

SOME CONTEXT ▸ Feb 2016, Kotlin v1.0 stable released ▸ May 2017, Google announced first-class support for Kotlin on Android ▸ May 2019, Google announced Kotlin as preferred language for Android ▸ Intersect moved to Kotlin little more than a year ago ▸ Delivered 2 largely Kotlin based apps in production ▸ Today, I’ll talk about gotchas we observed here and there

Slide 3

Slide 3 text

USING vs LEARNING

Slide 4

Slide 4 text

Nullable : lateinit : lazy USING vs LEARNING

Slide 5

Slide 5 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” }

Slide 6

Slide 6 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” }

Slide 7

Slide 7 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” } lateinit var currentAccountData: AccountData if (this::currentAccountData.isInitialized) { fetchData(currentAccountData.id) }

Slide 8

Slide 8 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” } // full screen view to show when // we get API error lateinit var tryAgainView: View fun onApiError() { tryAgainView.show() }

Slide 9

Slide 9 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” } // full screen view to show when // we get API error lateinit var tryAgainView: View tryAgainView = findViewById(R.id.try_again_view) fun onApiError() { tryAgainView.show() }

Slide 10

Slide 10 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” } lateinit var incomingData: String fun getIntent(context: Context, data: String? = null): Intent { return Intent(context, MainActivity::class.java).apply { putExtra(DATA, data) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) incomingData = intent.getStringExtra(DATA) }

Slide 11

Slide 11 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” } lateinit var incomingData: String fun getIntent(context: Context, data: String? = null): Intent { return Intent(context, MainActivity::class.java).apply { putExtra(DATA, data) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) incomingData = intent.getStringExtra(DATA) } // Intent.java public String getStringExtra(String name) { return mExtras == null ? null : mExtras.getString(name); } ✗

Slide 12

Slide 12 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { “lazy” } lateinit var incomingData: String fun getIntent(context: Context, data: String? = null): Intent { return Intent(context, MainActivity::class.java).apply { putExtra(DATA, data) } } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) incomingData = intent.getStringExtra(DATA) } // Intent.java @Nullable public String getStringExtra(String name) { return mExtras == null ? null : mExtras.getString(name); } ✓

Slide 13

Slide 13 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { "lazy" }

Slide 14

Slide 14 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { "lazy" } private val userListAdapter by lazy { UserListAdapter(this) } private val bulletPath: Path by lazy { Path() } ✓ ✗

Slide 15

Slide 15 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { "lazy" } private val userListAdapter by lazy { UserListAdapter(this) } private val bulletPath: Path by lazy { Path() }

Slide 16

Slide 16 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { "lazy" } private val userListAdapter by lazy { UserListAdapter(this) } private val bulletPath: Path by lazy { Path() } Airbnb/MvRx --> lifecycleAwareLazy

Slide 17

Slide 17 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { "lazy" } public actual fun lazy(initializer: () -> T): Lazy = SynchronizedLazyImpl(initializer) public actual fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = when (mode) { LazyThreadSafetyMode.SYNCHRONIZED -> SynchronizedLazyImpl(initializer) LazyThreadSafetyMode.PUBLICATION -> SafePublicationLazyImpl(initializer) LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer) }

Slide 18

Slide 18 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { "lazy" } public actual fun lazy(initializer: () -> T): Lazy = SynchronizedLazyImpl(initializer) public actual fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = when (mode) { LazyThreadSafetyMode.SYNCHRONIZED -> SynchronizedLazyImpl(initializer) LazyThreadSafetyMode.PUBLICATION -> SafePublicationLazyImpl(initializer) LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer) } val bulletPath: Path by lazy(LazyThreadSafetyMode.NONE) { Path() }

Slide 19

Slide 19 text

USING vs LEARNING private val iAmNullable: String? = null private lateinit var iAmLateInit: String private val iAmLazy by lazy { "lazy" } public actual fun lazy(initializer: () -> T): Lazy = SynchronizedLazyImpl(initializer) public actual fun lazy(mode: LazyThreadSafetyMode, initializer: () -> T): Lazy = when (mode) { LazyThreadSafetyMode.SYNCHRONIZED -> SynchronizedLazyImpl(initializer) LazyThreadSafetyMode.PUBLICATION -> SafePublicationLazyImpl(initializer) LazyThreadSafetyMode.NONE -> UnsafeLazyImpl(initializer) } val bulletPath: Path by lazy(LazyThreadSafetyMode.NONE) { Path() }

Slide 20

Slide 20 text

KOTLIN <> JAVA <> TOOLING

Slide 21

Slide 21 text

Android Studio yet not Kotlin first! KOTLIN <> JAVA <> TOOLING

Slide 22

Slide 22 text

KOTLIN <> JAVA <> TOOLING

Slide 23

Slide 23 text

KOTLIN <> JAVA <> TOOLING override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(id) parcel.writeString(name) parcel.writeParcelable(customObj, flags) }

Slide 24

Slide 24 text

KOTLIN <> JAVA <> TOOLING override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.apply { writeString(id) writeString(name) writeParcelable(customObj, flags) } }

Slide 25

Slide 25 text

Android Studio default code style still does wildcard imports (Kotlin & Java guidelines do not allowed it) In fact, Ktlint throws warnings about it! KOTLIN <> JAVA <> TOOLING

Slide 26

Slide 26 text

Swagger / open-api codegen support issue KOTLIN <> JAVA <> TOOLING

Slide 27

Slide 27 text

KOTLIN <> JAVA <> TOOLING public class Transaction implements Serializable { private static final long serialVersionUID = 1L; @SerializedName("postedTimestamp") private OffsetDateTime postedTimestamp = null; public Transaction() { } @ApiModelProperty( example = "2017-10-27T03:17:06.837-05:00", value = "Timestamp of posted transaction" ) public OffsetDateTime getPostedTimestamp() { return this.postedTimestamp; } public void setPostedTimestamp(OffsetDateTime postedTimestamp) { this.postedTimestamp = postedTimestamp; } }

Slide 28

Slide 28 text

KOTLIN <> JAVA <> TOOLING fun getTimeLabel(): String { return postedTimestamp.formatTimeAsLabel() } public class Transaction implements Serializable { private static final long serialVersionUID = 1L; @SerializedName("postedTimestamp") private OffsetDateTime postedTimestamp = null; public Transaction() { } @ApiModelProperty( example = "2017-10-27T03:17:06.837-05:00", value = "Timestamp of posted transaction" ) public OffsetDateTime getPostedTimestamp() { return this.postedTimestamp; } public void setPostedTimestamp(OffsetDateTime postedTimestamp) { this.postedTimestamp = postedTimestamp; } }

Slide 29

Slide 29 text

KOTLIN <> JAVA <> TOOLING fun getTimeLabel(): String { return postedTimestamp.formatTimeAsLabel() } public class Transaction implements Serializable { private static final long serialVersionUID = 1L; @SerializedName("postedTimestamp") private OffsetDateTime postedTimestamp = null; public Transaction() { } @ApiModelProperty( example = "2017-10-27T03:17:06.837-05:00", value = "Timestamp of posted transaction" ) public OffsetDateTime getPostedTimestamp() { return this.postedTimestamp; } public void setPostedTimestamp(OffsetDateTime postedTimestamp) { this.postedTimestamp = postedTimestamp; } } Swagger Codegen or openapi-generator for kotlin doesn't have retrofit & other required dependencies yet! So we’re kinda screwed ¯\_(ツ)_/¯

Slide 30

Slide 30 text

Third party enterprise tools not Kotlin friendly yet KOTLIN <> JAVA <> TOOLING

Slide 31

Slide 31 text

KOTLIN <> JAVA <> TOOLING We couldn’t run security analysis and pen testing tools on our codebase because it wasn’t yet supporting Kotlin ¯\_(ツ)_/¯ Third party enterprise tools not Kotlin friendly yet

Slide 32

Slide 32 text

NO checked exceptions on Kotlin! KOTLIN <> JAVA <> TOOLING Understood! But it’s difficult to get used to it after years of Java

Slide 33

Slide 33 text

KOTLIN <> ANDROID

Slide 34

Slide 34 text

KOTLIN <> ANDROID Ugly data classes with Parcelable

Slide 35

Slide 35 text

KOTLIN <> ANDROID Ugly data classes with Parcelable data class MyParcelableData(val id: String, val name: String, val customObj: MyCustomObj)

Slide 36

Slide 36 text

KOTLIN <> ANDROID Ugly data classes with Parcelable data class MyParcelableData(val id: String, val name: String, val customObj: MyCustomObj) : Parcelable { constructor(parcel: Parcel) : this( parcel.readString(), parcel.readString(), parcel.readParcelable(MyCustomObj::class.java.classLoader)) override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(id) parcel.writeString(name) parcel.writeParcelable(customObj, flags) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator { // …… } }

Slide 37

Slide 37 text

KOTLIN <> ANDROID Ugly data classes with Parcelable data class MyParcelableData(val id: String?, val name: String?, val customObj: MyCustomObj?) : Parcelable { constructor(parcel: Parcel) : this( parcel.readString(), // error -compileSdk 29 parcel.readString(), // error parcel.readParcelable(MyCustomObj::class.java.classLoader)) // error override fun writeToParcel(parcel: Parcel, flags: Int) { parcel.writeString(id) parcel.writeString(name) parcel.writeParcelable(customObj, flags) } override fun describeContents(): Int { return 0 } companion object CREATOR : Parcelable.Creator { // …… } }

Slide 38

Slide 38 text

KOTLIN <> ANDROID kotlinx synthetic imports

Slide 39

Slide 39 text

KOTLIN <> ANDROID kotlinx synthetic imports

Slide 40

Slide 40 text

KOTLIN <> ANDROID

Slide 41

Slide 41 text

KOTLIN <> ANDROID

Slide 42

Slide 42 text

THAT ENDS MY BLAH BLAH!

Slide 43

Slide 43 text

RESOURCES ‣ https://www.youtube.com/watch?v=Ta5wBJsC39s (Kotlin Under the Hood: Understand the Internals) ‣ https://www.bignerdranch.com/blog/kotlin-when-to-use-lazy-or-lateinit/ ‣ https://github.com/airbnb/MvRx/blob/master/mvrx/src/main/kotlin/com/airbnb/mvrx/ lifecycleAwareLazy.kt ‣ https://www.reddit.com/r/androiddev/comments/ala9p2/ why_kotlinx_synthetic_is_no_longer_a_recommended/ ‣ https://old.reddit.com/r/androiddev/comments/ala9p2/ why_kotlinx_synthetic_is_no_longer_a_recommended/efdvpkg/ ‣ https://android-review.googlesource.com/c/platform/frameworks/support/+/882241 ‣ https://twitter.com/chrisbanes/status/897075635142754305 ‣ https://github.com/pinterest/ktlint ‣ https://github.com/pinterest/ktlint/issues/48 (wildcard discussion) ‣ https://kotlinlang.org/docs/tutorials/android-plugin.html