Using Kotlin in Production

A8aac549f52be897a2098b280acffea0?s=47 Emma Guy
November 11, 2017

Using Kotlin in Production

Kotlin has fast become the new hotness in the Android world - but what is the reality of adding it to an app which is rapidly being built for a fast growing startup? In this talk we'll cover how we went about adding Kotlin to the Monzo app, why we made those choices and how it went.

https://devfest.gdg.london/schedule/2017-11-11?sessionId=204

A8aac549f52be897a2098b280acffea0?s=128

Emma Guy

November 11, 2017
Tweet

Transcript

  1. Using Kotlin in Production @emmaguy

  2. How did we get there? Did it go well? Lessons

    learnt
  3. Convince your team

  4. Have an offsite Nerd out!

  5. Convince your manager(s)

  6. None
  7. Christina Lee @ Google I/O https://www.youtube.com/watch?v=fPzxfeDJDzY

  8. Everyone is sold! Let’s go!

  9. “Let’s just start using it” - me, March 2017

  10. Plan

  11. Decide how to use it

  12. Only tests? A layer of your application’s architecture? ‘Anything new’?

  13. Tests only

  14. @Test public void register_noPhoneNumber_showContactSupport() { when(pinManager.fetchPhoneNumber()).thenReturn(""); presenter.register(view); verify(view).showContactSupport(); }

  15. @Test fun register_noPhoneNumber_showContactSupport() { whenever(pinManager.fetchPhoneNumber()).thenReturn("") presenter.register(view) verify(view).showContactSupport() }

  16. Figure out any potential “incompatibilities” e.g. Realm, Lombok

  17. None
  18. None
  19. Learn & Adapt

  20. Progressed to use in data layer of our app ...then

    in Activities (replacing ButterKnife) ...then anything new!
  21. Expect discussions about nullability

  22. #kotlin-tips

  23. Stick with what you committed to for a while Assess

    Make changes
  24. None
  25. Awesome things

  26. Kotlin = great for hiring

  27. @Deprecated https://hackernoon.com/how-kotlins-deprecated-relieves-pain-of-colossal-refactoring-8577545aaed

  28. Extension methods

  29. @Override public void showLoading() { progressBar.setVisibility(View.VISIBLE); } or override fun

    showLoading() { progressBar.visible() }
  30. fun ImageView.loadUrl(url: String) { Picasso.with(context).load(url).into(this) } imageView.loadUrl(url)

  31. fun Activity.screenWidth(): Int { val metrics = DisplayMetrics() windowManager.defaultDisplay.getMetrics(metrics) return

    metrics.widthPixels }
  32. Named/default arguments

  33. fun card( id: String = UUID.randomUUID().toString(), expires: String = "24/20",

    cardStatus: CardStatus = CardStatus.ACTIVE ): Card { return Card(id, expires, cardStatus) } Card(expires = "03/16", cardStatus = CardStatus.INACTIVE)
  34. setUpgradeBannerStyle(true) or setUpgradeBannerStyle(loudStyle = true)

  35. apply { }

  36. val arguments = Bundle().apply { putString(KEY_ID, id) }

  37. Things we learnt

  38. “Convert any file you touch” doesn’t work

  39. There is no ‘static’

  40. companion object {} https://medium.com/@BladeCoder/exploring-kotlins-hidden-costs-part-1-fbb9935d9b62

  41. Where do I put constants?

  42. None
  43. Auto convert is not always your friend

  44. None
  45. SAM Conversion

  46. public interface PayeeClickListener { void onPayeeClicked(Payee payee); }

  47. adapter.setPayeeClickListener(payee -> /* do something */);

  48. adapter.setPayeeClickListener(PayeeClickListener { /* do something */ })

  49. But, if we convert the interface to Kotlin

  50. adapter.setPayeeClickListener(object : PayeeClickListener { override fun onPayeeClicked(payee: Payee) { //

    do something } })
  51. New thing = less mature ecosystem IDE integration, style guide,

    code coverage tools
  52. Would you do it again?

  53. Absolutely.

  54. None
  55. Thank you! @emmaguy

  56. Questions? @emmaguy