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

Yo Dawg! I heard you like Kotlin

Antonio Leiva
November 17, 2017

Yo Dawg! I heard you like Kotlin

Kotlin is the new official language to develop Android Apps, and it can be used almost everywhere.

But did you know you could write a complete App without using other language rather than Kotlin? It can substitute Java in your code, XML in your views and Groovy in your Gradle files.

Antonio Leiva

November 17, 2017
Tweet

More Decks by Antonio Leiva

Other Decks in Programming

Transcript

  1. Menu (hope you’re hungry!) 1. Kotlin instead of Java 2.

    Kotlin for Gradle files 3. Kotlin for views 4. KOTLIN EVERYWHEEAAAR!
  2. override fun showAlbum(albumDetail: AlbumDetail?) { if (albumDetail != null) {

    updateAlbumDetail(albumDetail) } else { postponeTransitions() } }
  3. inline fun ViewPropertyAnimator.onAnimationEnd( crossinline continuation: (Animator) -> Unit) { setListener(object

    : AnimatorListenerAdapter() { override fun onAnimationEnd(animation: Animator) { continuation(animation) } }) }
  4. Kotlin DSL - Cons • Not easy to configure •

    Knowledge about plugin structure • Not much documentation or examples
  5. Kotlin DSL - Steps 1. Rename Gradle files 2. Write

    the config file 3. Convert and compile little by little
  6. 2. Write the config file class ProjectConfiguration { val buildPlugins

    = BuildPlugins() val android = Android() val libs = Libs() val testLibs = TestLibs() }
  7. 2. Write the config file class Android { val buildToolsVersion

    = "26.0.2" val minSdkVersion = 19 val targetSdkVersion = 26 val compileSdkVersion = 26 val applicationId = "com.antonioleiva.bandhookkotlin" val versionCode = 1 val versionName = "0.1" }
  8. 3. Convert and compile little by little val config =

    ProjectConfiguration() ... dependencies { classpath(config.buildPlugins.androidGradle) classpath(config.buildPlugins.kotlinGradlePlugin) }
  9. 3. Convert and compile little by little defaultConfig { 1.

    applicationId = config.android.applicationId 2. minSdkVersion(config.android.minSdkVersion) 3. targetSdkVersion(config.android.targetSdkVersion) 4. versionCode = config.android.versionCode 5. versionName = config.android.versionName }
  10. Anko Layouts verticalLayout { val user = editText { hint

    = "Username" } val pass = editText { hint = "Password" } button("Login") { setOnClickListener { longToast("${user.text}, ${pass.text}") } } }
  11. Anko Layouts - Pros • 4x faster • Better composability

    • Easier data binding, complex calculations
  12. Anko Layouts - Cons • Lack of designer • Learn

    again from scratch • Some things are more complex (like styles)
  13. fun ViewManager.autoFitRecycler(theme: Int = 0) = autoFitRecycler(theme) {} inline fun

    ViewManager.autoFitRecycler(theme: Int = 0, init: AutofitRecyclerView.() -> Unit) = ankoView(::AutofitRecyclerView, theme, init) Anko Layouts - Custom views
  14. Anko Layouts - styling fun AutofitRecyclerView.style() { clipToPadding = false

    columnWidth = dimen(R.dimen.column_width) scrollBarStyle = View.SCROLLBARS_OUTSIDE_OVERLAY horizontalPadding = dimen(R.dimen.recycler_spacing) verticalPadding = dip(2) addItemDecoration(PaddingItemDecoration(dip(2))) }
  15. Anko Layouts - styling .applyRecursively { view -> when (view)

    { is EditText -> view.textSize = 18f } }
  16. Multi-platform projects (Kotlin 1.2) expect class MyClass { fun foo():

    String fun bar(): Int } actual class MyClass { fun foo(): String = “” fun bar(): Int = 20 }