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

Kotlin for Android

Kotlin for Android

A quick introduction to some of the cool features of Kotlin and the adoption in Android.

This has been presented at Berlin during the yearly ezhome Engineering Meetup.

Spiros Economakis

May 25, 2017
Tweet

More Decks by Spiros Economakis

Other Decks in Technology

Transcript

  1. Kotlin awesome features String templates Null-safety Smart casts Lambda expressions

    + Inline functions Extension functions Properties Primary constructors First-class delegation Type inference Declaration-site variance & Type projections Range expressions Operator overloading Companion objects Data classes Separate interfaces for read-only and mutable collections Coroutines Singletons
  2. Why Kotlin for Android? Stuck on Java 6: - No

    javax.time - No streams - No lambdas, method refs, non-capturing anon classes - No try-with resources
  3. Why Kotlin for Android? Stuck on Java 6: - No

    javax.time Use ThreeTenBP - No streams Use RxJava - No lambdas, method refs, non-capturing anon classes Use Retrolambda - No try-with resources Use Retrolambda or minSDK=19
  4. Why Kotlin for Android? Java language restriction and problems: -

    Unable to add methods to platform types - Nullability problems - Mutability problems - General verbosity for common things
  5. Why Kotlin for Android? Android API design problems: - Too

    much Inheritance - Nullability everywhere - Bunch of APIs
  6. Android boilerplate public class TestActivity extends AppCompatActivity { @Override protected

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); ViewPager viewPager = (ViewPager) findViewById(R.id.welcomeViewPager); CircleIndicator circleIndicator = (CircleIndicator) findViewById(R.id.welcomeCircleIndicator); } }
  7. Android boilerplate public class TestActivity extends AppCompatActivity { @Override protected

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); ViewPager viewPager = (ViewPager) findViewById(R.id.welcomeViewPager); CircleIndicator circleIndicator = (CircleIndicator) findViewById(R.id.welcomeCircleIndicator); } }
  8. Android boilerplate public class TestActivity extends AppCompatActivity { @Override protected

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); ViewPager viewPager = (ViewPager) findViewById(R.id.welcomeViewPager); CircleIndicator circleIndicator = (CircleIndicator) findViewById(R.id.welcomeCircleIndicator); } }
  9. Android boilerplate public class TestActivity extends AppCompatActivity { @Override protected

    void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); ViewPager viewPager = (ViewPager) findViewById(R.id.welcomeViewPager); CircleIndicator circleIndicator = (CircleIndicator) findViewById(R.id.welcomeCircleIndicator); } }
  10. Android boilerplate + Butterknife public class TestActivity extends AppCompatActivity {

    @BindView(R.id.welcomeViewPager) ViewPager welcomeViewPager; @BindView(R.id.welcomeCircleIndicator) CircleIndicator welcomeCircleIndicator; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_welcome); } }
  11. Kotlin Android Extensions plugin What is it? Provides reference to

    all layout views (which have IDs) with single line of code How? Adds a hidden caching function and a field inside each Kotlin Activity
  12. Kotlin Android Extensions plugin import kotlinx.android.synthetic.main.activity_welcome.* class TestActivity : AppCompatActivity()

    { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_welcome) welcomeViewPager.setText("Hello, world!") } }
  13. Kotlin Android Extensions plugin import kotlinx.android.synthetic.main.activity_welcome.* class TestActivity : AppCompatActivity()

    { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_welcome) welcomeViewPager.setText("Hello, world!") } }
  14. Kotlin Android Extensions plugin import kotlinx.android.synthetic.main.activity_welcome.* class TestActivity : AppCompatActivity()

    { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_welcome) welcomeViewPager.setText("Hello, world!") } }
  15. Kotlin & Java MULTIPLE LANGUAGES IN A PROJECT DIFFERENT LANGUAGES

    PARADIGMS COMPLEXITY ONBOARDING MAY LEAD TO INTEROPERABILITY BOILERPLATE ISOLATE CODE PER FEATURE/LAYER