Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Using Kotlin in Production

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

Emma Guy

November 11, 2017
Tweet

More Decks by Emma Guy

Other Decks in Technology

Transcript

  1. Using Kotlin in Production
    @emmaguy

    View Slide

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

    View Slide

  3. Convince your team

    View Slide

  4. Have an offsite
    Nerd out!

    View Slide

  5. Convince your manager(s)

    View Slide

  6. View Slide

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

    View Slide

  8. Everyone is sold! Let’s go!

    View Slide

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

    View Slide

  10. Plan

    View Slide

  11. Decide how to use it

    View Slide

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

    View Slide

  13. Tests only

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. View Slide

  18. View Slide

  19. Learn & Adapt

    View Slide

  20. Progressed to use in data layer of our app
    ...then in Activities (replacing ButterKnife)
    ...then anything new!

    View Slide

  21. Expect discussions about nullability

    View Slide

  22. #kotlin-tips

    View Slide

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

    View Slide

  24. View Slide

  25. Awesome things

    View Slide

  26. Kotlin = great for hiring

    View Slide

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

    View Slide

  28. Extension methods

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  32. Named/default arguments

    View Slide

  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)

    View Slide

  34. setUpgradeBannerStyle(true)
    or
    setUpgradeBannerStyle(loudStyle = true)

    View Slide

  35. apply { }

    View Slide

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

    View Slide

  37. Things we learnt

    View Slide

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

    View Slide

  39. There is no ‘static’

    View Slide

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

    View Slide

  41. Where do I put constants?

    View Slide

  42. View Slide

  43. Auto convert is not always your friend

    View Slide

  44. View Slide

  45. SAM Conversion

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  49. But, if we convert the interface to Kotlin

    View Slide

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

    View Slide

  51. New thing = less mature ecosystem
    IDE integration, style guide, code coverage tools

    View Slide

  52. Would you do it again?

    View Slide

  53. Absolutely.

    View Slide

  54. View Slide

  55. Thank you!
    @emmaguy

    View Slide

  56. Questions?
    @emmaguy

    View Slide