SlideShare a Scribd company logo
LITTLE HELPERS FOR ANDROID
DEVELOPMENT WITH KOTLIN
KAI KOENIG (@AGENTK)
Little Helpers for Android Development with Kotlin
AGENDA
▸ What is Kotlin?
▸ Common idioms and language concepts
▸ Kotlin and Android
▸ Anko
▸ Other libraries and tools for Kotlin and Android
▸ Final thoughts
WHAT IS KOTLIN?
WHAT IS KOTLIN?
SOME FUNDAMENTALS
▸ Statically typed programming language for the
JVM and Android as well as the browser
▸ Started as internal language “Project Kotlin” at
Jetbrains in 2010
▸ Now: Open-Source, Apache License - Kotlin 1.0
released in Feb 2016
▸ Kotlin SDK plus tool support for IntelliJ,
Android Studio, Eclipse
▸ Named after an island in the Gulf of Finland
WHAT IS KOTLIN?
MOTIVATION FOR KOTLIN
▸ The Java platform is awesome, but it has its issues:
▸ Sometimes tied to backwards/legacy compatibility
▸ Can be a very verbose language and produce bloated code
▸ Type system has various flaws
▸ Kotlin aims to fix a lot of those issues, in particular when one has to use Java 6
or 7 (if we’re lucky…) and can’t use all the new, shiny features from Java 8 and
soon Java 9 and 10.
WHAT IS KOTLIN?
HOW DOES A SIMPLE CONVERSION LOOK LIKE?
public String listConvert(Collection<Integer> collection) {
StringBuilder sb = new StringBuilder();
sb.append("{");
Iterator<Integer> iterator = collection.iterator();
while (iterator.hasNext()) {
Integer element = iterator.next();
sb.append(element);
if (iterator.hasNext()) {
sb.append(", ");
}
}
sb.append("}");
return sb.toString();
}
fun listConvert(collection: Collection<Int>): String {
val sb = StringBuilder()
sb.append("{")
val iterator = collection.iterator()
while (iterator.hasNext()) {
val element = iterator.next()
sb.append(element)
if (iterator.hasNext()) {
sb.append(", ")
}
}
sb.append("}")
return sb.toString()
}
fun listConvertKt(collection: Collection<Int>): String {
return collection.joinToString(prefix = "{",postfix = "}")
}
COMMON IDIOMS &
LANGUAGE PATTERNS
https://www.flickr.com/photos/geraldford/6976818221/
COMMON IDIOMS AND LANGUAGE CONCEPTS
OVERVIEW
▸ Immutability
▸ String templates & Enum classes
▸ Null safety
▸ Properties and Fields
▸ Type inference and casts
▸ Data classes
▸ Syntactic sugar (loops, ranges etc)
▸ Extension functions
▸ Lambdas
▸ Collection API
▸ Type-safe builders
▸ Java-Kotlin-Interop
COMMON IDIOMS AND LANGUAGE CONCEPTS
IMMUTABILITY
▸ Built-in support for mutable and immutable
variables, properties and fields
▸ Keywords var and val
▸ val - immutable (recommended)
▸ var - mutable
▸ Similar concept applies for class properties, val
creates getters, var creates getters and setters
(more later)
val a: Int = 1
val b = 1
val c: Int
c = 1
var x = 23
x += 1
COMMON IDIOMS AND LANGUAGE CONCEPTS
STRING TEMPLATES & ENUM CLASSES
▸ Kotlin Strings can contain template expressions
▸ Start with a $ character and
▸ can contain simple references: 

$s
▸ complex expressions in curly braces: 

${s.length}
▸ Kotlin has a dedicated enum class, very similar
to Java
val s = "abc"
val str = "$s.length is ${s.length}"
enum class Locale(val hello: String) {
DE_DE("Hallo"), EN_NZ("Hello"), MI_NZ("Kia Ora")
}
class Customer(val firstName:String,
val lastName:String,
val locale: Locale = Locale.DE_DE) {
fun sayHello() = println("${locale.hello},
$firstName $lastName")
}
fun main(args : Array<String>) {
val myCustomer = Customer("Sandra",
"Musterfrau",
Locale.MI_NZ)
myCustomer.sayHello()
}
COMMON IDIOMS AND LANGUAGE CONCEPTS
NULL SAFETY
▸ Motivation: A better way to deal with NPEs
▸ Kotlin differentiates nullable types from non-
nullable types by adding a ? to the type:
▸ String: no nullable
▸ String?: nullable
▸ Handle manually or use Safe Call operator ?. or
use the !! operator to allow/trigger a NPE.
// Won't compile
var lastName: String = null
// Will compile
var lastNameNullable: String? = null
// Will also not compile
println(lastNameNullable.length)
// Option 1 (-1)
println(if (lastNameNullable != null)
lastNameNullable.length else -1)
// Option 2 (null)
println(lastNameNullable?.length)
// Option 3 (NPE)
println(lastNameNullable!!.length)
COMMON IDIOMS AND LANGUAGE CONCEPTS
PROPERTIES AND FIELDS
▸ Kotlin classes have mutable or immutable
properties
▸ An automated backing field can be provided
by the compiler (if deemed necessary)
▸ Default getter/setters for properties, can be
customised
▸ lateinit modifier to deal with non-nullable
properties that can’t be initialised in the
constructor
var counter = 0
set(value) {
if (value >= 0)
field = value
}
public class MyTest {
lateinit var subject: TestSubject
@SetUp fun setup() {
subject = TestSubject()
}
@Test fun test() {
subject.method()
}
}
COMMON IDIOMS AND LANGUAGE CONCEPTS
TYPE INFERENCE AND CASTS (I)
▸ When possible, Kotlin will infer the type of
variables
▸ Explicit conversions, type widening and
inference
▸ Smaller types are not subtypes of bigger
types, no implicit conversion
▸ Types are often inferred from the context
val b: Byte = 1
// This won't work
val i: Int = b
// This will
val i: Int = b.toInt()
val l = 1L + 3
COMMON IDIOMS AND LANGUAGE CONCEPTS
TYPE INFERENCE AND CASTS (II)
▸ is or !is checks if an object adheres to a certain
type
▸ Smart cast: Compiler tracks is-expressions for
immutable values
▸ works for val local variables and private,
internal or in module performed casts
▸ works for var local variables if the variable
hasn’t been modified between check and
usage, never for var properties
fun whatIs(x: Any) {
when (x) {
is Int -> println(x + 42)
is String -> println(x.length)
is IntArray -> println(x.sum())
}
}
whatIs(4) // 46
whatIs("4") // 1
whatIs(intArrayOf(1,2,3,4,5)) // 15
COMMON IDIOMS AND LANGUAGE CONCEPTS
DATA CLASSES
▸ The POJOs or Beans of other languages…
▸ Data classes implicitly create:
▸ getters/setters (the latter if a property is var)
▸ equals(), hashCode(), toString(), copy() - can
be overwritten by custom implementations
▸ copy() has default parameters and can be
used to alter a copy
▸ parameterless constructors need default
parameters specified
data class ChromeEncryptedPayload(
val encryptedPayload: String,
val encryptionHeader: String,
val cryptoKeyHeader: String)
COMMON IDIOMS AND LANGUAGE CONCEPTS
EXTENSION FUNCTIONS
▸ Allow adding new functionality to a class
without inheritance or Decorators
▸ Kotlin allows extension functions and extension
properties
▸ Resolved statically, do not actually modify
the class (excellent example why this has to
be the case on https://kotlinlang.org/docs/
reference/extensions.html)
fun Int.sum(otherInt: Int): Int = this +
otherInt
3.sum(7)
fun Activity.toast(message: CharSequence,
duration: Int =
TOAST.LENGTH_SHORT) {
Toast.makeText(this, message,
duration).show()
}
// In onCreate of an Activity
override fun onCreate(...) {
...
toast("Hi there")
...
}
COMMON IDIOMS AND LANGUAGE CONCEPTS
JAVA-KOTLIN-INTEROP
▸ Java and Kotlin are fully interoperable from an integration point of view
▸ Your Java code can call and use Kotlin code
▸ Your Kotlin code can call and use Java code
▸ The latter is in particular useful because it means you can continue to use pretty
much any existing Android/Java library
▸ Check out Hendrik Kokocinski’s sample Kotlin app that uses all kinds of well
known Android/Java libs: https://github.com/blob0815/kotlin-android-sample
COMMON IDIOMS AND LANGUAGE CONCEPTS
OVERVIEW
▸ Immutability
▸ String templates & Enum classes
▸ Null safety
▸ Properties and Fields
▸ Type inference and casts
▸ Data classes
▸ Syntactic sugar (loops, ranges etc)
▸ Extension functions
▸ Lambdas
▸ Collection API
▸ Type-safe builders
▸ Java-Kotlin-Interop
KOTLIN & ANDROID
https://realm.io/assets/img/news/tutorial-kotlin-anko-cover.png
KOTLIN AND ANDROID
TOOLCHAIN AND FLOW
Java sourcesKotlin sources
Bytecode
javackotlinc
Dalvik ART JVM
KOTLIN AND ANDROID
PROJECT SETUP
▸ Use Android Studio 1.5.x/2.x or IntelliJ 15/2016
▸ Install Kotlin plugin (comes with “Jetbrains plugins” nowadays)
▸ Gradle dependencies project-level:
▸ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.2"
▸ Gradle dependencies module-level:
▸ compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.2'
▸ apply plugin: 'kotlin-android'
▸ main.java.srcDirs += 'src/main/kotlin'
KOTLIN AND ANDROID
KOTLIN EXTENSIONS FOR ANDROID (I)
▸ Provides of a set of synthetic properties that bind views to those properties
▸ Alternative to Butter Knife-style bindings, no need for additional runtime
library (Kotlin Extensions for Android are a Kotlin compiler plugin)
▸ import kotlinx.android.synthetic.main.<layout>.*
▸ import kotlinx.android.synthetic.main.<layout>.view.*
▸ usage: <componentid>.doSomething()
▸ Integrates nicely with build flavors, too
KOTLIN AND ANDROID
SYNTHETIC PROPERTIES
package ventegocreative.co.nz.kotlindemo
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
helloworld.text = "Hey, I'm dynamically set"
}
}
KOTLIN AND ANDROID
KOTLIN EXTENSIONS FOR ANDROID (II)
▸ Used to be part of a separate plugin (https://plugins.jetbrains.com/plugin?
pluginId=7717)
▸ obsolete since December 2015
▸ functionality rolled into the main Kotlin plugin
▸ still needs a very particular setup (not properly documented but in this place:
https://youtrack.jetbrains.com/issue/KT-10336)



TLDR: move dependencies in app module(s), not top-level Gradle file.
KOTLIN AND ANDROID
KOTLIN EXTENSIONS FOR ANDROID (III)
▸ The current assumption is that Jetbrains plan to add more to the Kotlin
Extensions for Android, but we don’t really know for sure at this stage.
▸ Current feature of view binding:
▸ Fresh take on view binding
▸ Very simple to setup and use
▸ Overall less powerful than Butter Knife, Kotter Knife
ANKO
ANKO
A DSL FOR LAYOUTS
▸ Developed and maintained by Jetbrains, under Apache 2.0 license
▸ The most important element of Anko is the Layout DSL
▸ Idea: Replace XML layout definitions by Kotlin code - without having to build
the layout in a truly programmatic sense
▸ Modular - as we’re talking about UI/Layout, it’s very important to select the
right library for your minSDKVersion
▸ Extensible - you can add your own DSL elements for custom UI controls
ANKO
LAYOUT XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:gravity="center"
android:text="@string/empty_todos_message"
android:layout_weight="7"
android:layout_height="wrap_content" />
<Button
android:layout_width="match_parent"
android:layout_weight="1"
android:text="Say Hello"
android:layout_height="0dp" />
</LinearLayout>
ANKO
PROGRAMMATIC LAYOUT IN KOTLIN
val act = this
val layout = LinearLayout(act)
layout.orientation = LinearLayout.VERTICAL
val name = EditText(act)
val button = Button(act)
button.text = "Say Hello"
button.setOnClickListener {
Toast.makeText(act, "Hello, ${name.text}!", Toast.LENGTH_SHORT).show()
}
layout.addView(name)
layout.addView(button)
ANKO
ANKO DSL
verticalLayout {
val name = editText()
button("Say Hello") {
onClick { toast("Hello, ${name.text}!") }
}
}
ANKO
BUT THERE’S MORE (ANKO “ADVANCED TOPICS”)
▸ Intent Wrappers for various purposes: e.g. sendSMS(number, [text])
▸ Service shortcuts
▸ Configuration qualifiers: configuration(screenSize = ScreenSize.LARGE,
orientation = Orientation.LANDSCAPE) { … }
▸ Asynchronous tasks
▸ SQLLite
▸ Removes all the tragic cursor handling and lot of the try/catch blocks necessary
LIBRARIES AND TOOLS
https://www.flickr.com/photos/sillygwailo/5990089210/
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
OVERVIEW
▸ Kotter Knife
▸ Butter Knife
▸ KAndroid
▸ Kovenant
▸ Quickies: Fuel, Injekt, Spek, Kotson
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KOTTER KNIFE
▸ We don’t like findByViewId(…) - read: https://ragunathjawahar.wordpress.com/
2015/03/23/kotlin-findviewbyid-dead-as-dinosaurs
▸ Kotter Knife provides view binding in a similar way to Butter Knife for Android/
Java
▸ Why Kotter Knife (runtime library) over Kotlin Android Extensions (compiler
plugin)?
▸ Porting code from Java/Butter Knife to Kotlin
▸ Features like listener binding and resources binding that don’t exist in KAE.
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
BUTTER KNIFE
▸ Not much to say about it, probably one of the most famous libraries in the
Android world.
▸ Butter Knife was difficult to use with Kotlin in the beginning
▸ Since Kotlin 1.0 RC you can in fact just use Butter Knife in your Kotlin code
▸ Sample code for Butter Knife/Kotlin in the official Jetbrains Kotlin Examples
repo (https://github.com/JetBrains/kotlin-examples/tree/master/gradle/
android-butterknife)
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KOTTER KNIFE VS BUTTER KNIFE VS KAE (I)
findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("MainActivity", "onClick: send")
}
});
@OnClick(R.id.send)
void clickSend(View v) {
Log.d("MainActivity", "onClick: send")
}
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KOTTER KNIFE VS BUTTER KNIFE VS KAE (II)
findViewById(R.id.send).setOnClickListener { view -> Log.d("MainActivity",
"onClick: send") };
val btnSend: Button by bindView(R.id.send)
btnSend.setOnClickListener({ view -> Log.d("MainActivity", "onClick: send") })
import kotlinx.android.synthetic.activity_main.*
btn_send.setOnClickListener({ view -> Log.d("MainActivity", "onClick: send") })
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KANDROID
▸ KAndroid is an extension library for Kotlin/Android
▸ Contains a variety of absolutely distinct and unconnected functionality
▸ Common theme: let’s get rid of boilerplate code
▸ Apache 2.0 license
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KANDROID FEATURES
▸ View binding (again)
▸ TextWatcher
▸ SeekBar Extension
▸ SearchView Extension
▸ Shortcuts to system services
▸ Logging
▸ Dealing with Intents
▸ SDK Version API (from/to)
▸ Thread management
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KANDROID EXAMPLES
runDelayed(1000) {
// delayed execution
}
runDelayedOnUiThread(5000) {
// delayed UI update
}
toApi(16, inclusive = true) {
// handle devices running older APIs
}
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KOVENANT
▸ In a nutshell: Promises for Kotlin
▸ Very modular built, you can essentially pick and choose the artifacts of
Kovenant that you’d like to use - Kovenant is not an Android-specific library
▸ Good staring set for Android: core, android, combine, jvm, functional
▸ MIT license
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KOVENANT FEATURES
▸ Core, foundations of a Promise
framework
▸ Tasks & Callbacks
▸ Chaining (Then, ThenApply)
▸ Lazy Promises
▸ Cancelling and Voiding
▸ Combine: combines 2-20 promises
▸ Functional: adds map, bind and
apply to support more advanced
HOF constructs in Kovenant
▸ JVM: Executors and Throttles (thread
pools)
▸ Android: UI callbacks and interacting
with UI Thread
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KOVENANT EXAMPLES (I)
task {
// some long-running thing
} success {
println("result: $it")
} fail {
println("problem: ${it.message}")
} always {
// this will always happen
}
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
KOVENANT EXAMPLES (II)
promiseOnUi {
// do some UI preparation
} then {
// the actual work
} successUi {
// update UI
}
OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID
OVERVIEW
▸ Kotter Knife
▸ Butter Knife
▸ KAndroid
▸ Kovenant
▸ Quickies: Fuel, Injekt, Spek, Kotson
FINAL THOUGHTS
https://www.flickr.com/photos/brickset/16099265973/
FINAL THOUGHTS
PERFORMANCE
▸ Runtime is pretty much on-par with Java
▸ Pre Kotlin 1.0.2: Build process is slower than a comparable app in Java - mainly
due to how the Kotlin compiler works (no partial builds/compilation)
▸ Kotlin libraries do add to the size of the application as well as to the method
count
▸ Kotlin runtime + stdlib are similar in method count to support-v4 or play-
services-base and add significantly less than Scala or Groovy
FINAL THOUGHTS
LANGUAGE AND MATURITY
▸ Kotlin 1.0 was a big step for the language
▸ Surprisingly mature for a 1.0 release (but 5+ years in the making)
▸ Full of great concepts and idioms
▸ Refreshing language that makes both Android and JVM development significantly
more pleasant and fun
▸ Issue: Tooling around static analysis is non-existent at the moment (some basic listing
for Android is available since 1.0.2)
▸ Ready for prime-time yet?
FINAL THOUGHTS
WHAT DID WE LEARN?
▸ What is Kotlin?
▸ Common idioms and language concepts
▸ Kotlin and Android
▸ Anko
▸ Other libraries and tools for Kotlin and Android
RESOURCES
RESOURCES
▸ Kotlin: http://kotlinlang.org
▸ Anko: https://github.com/Kotlin/anko
▸ Kotter Knife: https://github.com/JakeWharton/kotterknife
▸ KAndroid: https://github.com/pawegio/KAndroid
▸ Kovenant: https://github.com/mplatvoet/kovenant
▸ Fuel: https://github.com/kittinunf/Fuel
▸ Injekt: https://github.com/kohesive/injekt
▸ Spek: http://jetbrains.github.io/spek/
▸ Kotson: https://github.com/SalomonBrys/Kotson
RESOURCES
GET IN TOUCH
Kai Koenig
Email: kai@ventego-creative.co.nz
Twitter: @AgentK

More Related Content

What's hot (19)

PDF
Kotlin boost yourproductivity
nklmish
 
PDF
No excuses, switch to kotlin
Thijs Suijten
 
PDF
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki
 
PDF
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 
PDF
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
PDF
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
PDF
Kotlin hands on - MorningTech ekito 2017
Arnaud Giuliani
 
PDF
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
PDF
Taking Kotlin to production, Seriously
Haim Yadid
 
PDF
Kotlin cheat sheet by ekito
Arnaud Giuliani
 
PDF
Kotlin - Better Java
Dariusz Lorenc
 
PDF
Swift and Kotlin Presentation
Andrzej Sitek
 
PPTX
Exploring Anko Components, Kotlin, Android
Rakshak R.Hegde
 
PDF
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan s.r.o.
 
PPTX
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
PPTX
K is for Kotlin
TechMagic
 
PDF
Kotlin for Android devs
Adit Lal
 
PDF
Introduction to kotlin
NAVER Engineering
 
PDF
Intro to Kotlin
Magda Miu
 
Kotlin boost yourproductivity
nklmish
 
No excuses, switch to kotlin
Thijs Suijten
 
ADG Poznań - Kotlin for Android developers
Bartosz Kosarzycki
 
Kotlin advanced - language reference for android developers
Bartosz Kosarzycki
 
Kotlin Developer Starter in Android projects
Bartosz Kosarzycki
 
Introduction to kotlin for android app development gdg ahmedabad dev fest 2017
Hardik Trivedi
 
Kotlin hands on - MorningTech ekito 2017
Arnaud Giuliani
 
Kotlin, smarter development for the jvm
Arnaud Giuliani
 
Taking Kotlin to production, Seriously
Haim Yadid
 
Kotlin cheat sheet by ekito
Arnaud Giuliani
 
Kotlin - Better Java
Dariusz Lorenc
 
Swift and Kotlin Presentation
Andrzej Sitek
 
Exploring Anko Components, Kotlin, Android
Rakshak R.Hegde
 
eMan Dev Meetup: Kotlin - A Language we should know it exists (part 02/03) 18...
eMan s.r.o.
 
Introduction to kotlin + spring boot demo
Muhammad Abdullah
 
K is for Kotlin
TechMagic
 
Kotlin for Android devs
Adit Lal
 
Introduction to kotlin
NAVER Engineering
 
Intro to Kotlin
Magda Miu
 

Viewers also liked (16)

PDF
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski
 
PDF
Contextual communications and why you should care - Droidcon DE
Marcos Placona
 
PPTX
Unit testing without Robolectric, Droidcon Berlin 2016
Danny Preussler
 
PDF
Add ClassyShark to your Android toolbox
Boris Farber
 
PPTX
Android & Kotlin - The code awakens #03
Omar Miatello
 
PPTX
Android & Kotlin - The code awakens #01
Omar Miatello
 
PPT
Smoothing Your Java with DSLs
intelliyole
 
PDF
Getting started-kotlin-android
Lucas Albuquerque
 
PDF
A realtime infrastructure for Android apps: Firebase may be what you need..an...
Alessandro Martellucci
 
PPTX
Android Workshop
Rakshak R.Hegde
 
PDF
First few months with Kotlin - Introduction through android examples
Nebojša Vukšić
 
PDF
프로그래밍 대회: C++11 이야기
Jongwook Choi
 
PPTX
MVVM and RxJava – the perfect mix
Florina Muntenescu
 
PDF
2016 FunctionCup 풀이
geunwoo bae
 
PDF
Database design & Normalization (1NF, 2NF, 3NF)
Jargalsaikhan Alyeksandr
 
PDF
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
Tomasz Polanski - Automated mobile testing 2016 - Testing: why, when, how
Tomasz Polanski
 
Contextual communications and why you should care - Droidcon DE
Marcos Placona
 
Unit testing without Robolectric, Droidcon Berlin 2016
Danny Preussler
 
Add ClassyShark to your Android toolbox
Boris Farber
 
Android & Kotlin - The code awakens #03
Omar Miatello
 
Android & Kotlin - The code awakens #01
Omar Miatello
 
Smoothing Your Java with DSLs
intelliyole
 
Getting started-kotlin-android
Lucas Albuquerque
 
A realtime infrastructure for Android apps: Firebase may be what you need..an...
Alessandro Martellucci
 
Android Workshop
Rakshak R.Hegde
 
First few months with Kotlin - Introduction through android examples
Nebojša Vukšić
 
프로그래밍 대회: C++11 이야기
Jongwook Choi
 
MVVM and RxJava – the perfect mix
Florina Muntenescu
 
2016 FunctionCup 풀이
geunwoo bae
 
Database design & Normalization (1NF, 2NF, 3NF)
Jargalsaikhan Alyeksandr
 
Mastering the NDK with Android Studio 2.0 and the gradle-experimental plugin
Xavier Hallade
 
Ad

Similar to Little Helpers for Android Development with Kotlin (20)

PDF
Android 101 - Building a simple app with Kotlin in 90 minutes
Kai Koenig
 
PDF
Practical tips for building apps with kotlin
Adit Lal
 
PDF
A quick and fast intro to Kotlin
XPeppers
 
PDF
Save time with kotlin in android development
Adit Lal
 
PDF
Kotlin what_you_need_to_know-converted event 4 with nigerians
junaidhasan17
 
PDF
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic
 
PPTX
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
PPTX
Kotlin – the future of android
DJ Rausch
 
PPTX
Why kotlininandroid
Phani Kumar Gullapalli
 
PDF
Kotlin for Android
Han Yin
 
PDF
Kotlin a problem solver - gdd extended pune
Hardik Trivedi
 
PDF
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
PDF
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
PDF
Basics of kotlin ASJ
DSCBVRITH
 
PDF
Introduction to Kotlin - Android KTX
Syed Awais Mazhar Bukhari
 
PDF
Android 101 - Kotlin ( Future of Android Development)
Hassan Abid
 
PDF
Be More Productive with Kotlin
Brandon Wever
 
PPTX
Introduction to Kotlin for Android developers
Mohamed Wael
 
PDF
Exploring Kotlin
Johan Haleby
 
PDF
Kotlin for Android Developers - 1
Mohamed Nabil, MSc.
 
Android 101 - Building a simple app with Kotlin in 90 minutes
Kai Koenig
 
Practical tips for building apps with kotlin
Adit Lal
 
A quick and fast intro to Kotlin
XPeppers
 
Save time with kotlin in android development
Adit Lal
 
Kotlin what_you_need_to_know-converted event 4 with nigerians
junaidhasan17
 
9054799 dzone-refcard267-kotlin
Zoran Stanimirovic
 
Introduction to Kotlin Language and its application to Android platform
EastBanc Tachnologies
 
Kotlin – the future of android
DJ Rausch
 
Why kotlininandroid
Phani Kumar Gullapalli
 
Kotlin for Android
Han Yin
 
Kotlin a problem solver - gdd extended pune
Hardik Trivedi
 
Kotlin: A pragmatic language by JetBrains
Jigar Gosar
 
Kotlin for Android Developers - Victor Kropp - Codemotion Rome 2018
Codemotion
 
Basics of kotlin ASJ
DSCBVRITH
 
Introduction to Kotlin - Android KTX
Syed Awais Mazhar Bukhari
 
Android 101 - Kotlin ( Future of Android Development)
Hassan Abid
 
Be More Productive with Kotlin
Brandon Wever
 
Introduction to Kotlin for Android developers
Mohamed Wael
 
Exploring Kotlin
Johan Haleby
 
Kotlin for Android Developers - 1
Mohamed Nabil, MSc.
 
Ad

More from Kai Koenig (17)

PDF
Why a whole country skipped a day - Fun with Timezones
Kai Koenig
 
PDF
Android 103 - Firebase and Architecture Components
Kai Koenig
 
PDF
Android 102 - Flow, Layouts and other things
Kai Koenig
 
PDF
Kotlin Coroutines and Android sitting in a tree - 2018 version
Kai Koenig
 
PDF
Kotlin Coroutines and Android sitting in a tree
Kai Koenig
 
PDF
Improving your CFML code quality
Kai Koenig
 
PDF
Introduction to Data Mining
Kai Koenig
 
PDF
Garbage First and you
Kai Koenig
 
PDF
Real World Lessons in jQuery Mobile
Kai Koenig
 
PDF
The JVM is your friend
Kai Koenig
 
PDF
Regular Expressions 101
Kai Koenig
 
PDF
There's a time and a place
Kai Koenig
 
KEY
Clojure - an introduction (and some CFML)
Kai Koenig
 
KEY
AngularJS for designers and developers
Kai Koenig
 
KEY
Cryptography for developers
Kai Koenig
 
PDF
JVM and Garbage Collection Tuning
Kai Koenig
 
PDF
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
Kai Koenig
 
Why a whole country skipped a day - Fun with Timezones
Kai Koenig
 
Android 103 - Firebase and Architecture Components
Kai Koenig
 
Android 102 - Flow, Layouts and other things
Kai Koenig
 
Kotlin Coroutines and Android sitting in a tree - 2018 version
Kai Koenig
 
Kotlin Coroutines and Android sitting in a tree
Kai Koenig
 
Improving your CFML code quality
Kai Koenig
 
Introduction to Data Mining
Kai Koenig
 
Garbage First and you
Kai Koenig
 
Real World Lessons in jQuery Mobile
Kai Koenig
 
The JVM is your friend
Kai Koenig
 
Regular Expressions 101
Kai Koenig
 
There's a time and a place
Kai Koenig
 
Clojure - an introduction (and some CFML)
Kai Koenig
 
AngularJS for designers and developers
Kai Koenig
 
Cryptography for developers
Kai Koenig
 
JVM and Garbage Collection Tuning
Kai Koenig
 
Apps vs. Sites vs. Content - a vendor-agnostic view on building stuff for the...
Kai Koenig
 

Recently uploaded (20)

PPTX
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
PDF
interacting-with-ai-2023---module-2---session-3---handout.pdf
cniclsh1
 
PPTX
Cubase Pro Crack 2025 – Free Download Full Version with Activation Key
HyperPc soft
 
PPTX
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PDF
Transform Retail with Smart Technology: Power Your Growth with Ginesys
Ginesys
 
PDF
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PDF
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PDF
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
PowerISO Crack 2025 – Free Download Full Version with Serial Key [Latest](1)....
HyperPc soft
 
PDF
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
PPTX
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
PPTX
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
PPTX
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 
NeuroStrata: Harnessing Neuro-Symbolic Paradigms for Improved Testability and...
Ivan Ruchkin
 
interacting-with-ai-2023---module-2---session-3---handout.pdf
cniclsh1
 
Cubase Pro Crack 2025 – Free Download Full Version with Activation Key
HyperPc soft
 
CONCEPT OF PROGRAMMING in language .pptx
tamim41
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
Transform Retail with Smart Technology: Power Your Growth with Ginesys
Ginesys
 
Efficient, Automated Claims Processing Software for Insurers
Insurance Tech Services
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
GridView,Recycler view, API, SQLITE& NetworkRequest.pdf
Nabin Dhakal
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Difference Between Kubernetes and Docker .pdf
Kindlebit Solutions
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PowerISO Crack 2025 – Free Download Full Version with Serial Key [Latest](1)....
HyperPc soft
 
From Chaos to Clarity: Mastering Analytics Governance in the Modern Enterprise
Wiiisdom
 
Perfecting XM Cloud for Multisite Setup.pptx
Ahmed Okour
 
MiniTool Power Data Recovery Full Crack Latest 2025
muhammadgurbazkhan
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Java Native Memory Leaks: The Hidden Villain Behind JVM Performance Issues
Tier1 app
 

Little Helpers for Android Development with Kotlin

  • 1. LITTLE HELPERS FOR ANDROID DEVELOPMENT WITH KOTLIN KAI KOENIG (@AGENTK)
  • 3. AGENDA ▸ What is Kotlin? ▸ Common idioms and language concepts ▸ Kotlin and Android ▸ Anko ▸ Other libraries and tools for Kotlin and Android ▸ Final thoughts
  • 5. WHAT IS KOTLIN? SOME FUNDAMENTALS ▸ Statically typed programming language for the JVM and Android as well as the browser ▸ Started as internal language “Project Kotlin” at Jetbrains in 2010 ▸ Now: Open-Source, Apache License - Kotlin 1.0 released in Feb 2016 ▸ Kotlin SDK plus tool support for IntelliJ, Android Studio, Eclipse ▸ Named after an island in the Gulf of Finland
  • 6. WHAT IS KOTLIN? MOTIVATION FOR KOTLIN ▸ The Java platform is awesome, but it has its issues: ▸ Sometimes tied to backwards/legacy compatibility ▸ Can be a very verbose language and produce bloated code ▸ Type system has various flaws ▸ Kotlin aims to fix a lot of those issues, in particular when one has to use Java 6 or 7 (if we’re lucky…) and can’t use all the new, shiny features from Java 8 and soon Java 9 and 10.
  • 7. WHAT IS KOTLIN? HOW DOES A SIMPLE CONVERSION LOOK LIKE? public String listConvert(Collection<Integer> collection) { StringBuilder sb = new StringBuilder(); sb.append("{"); Iterator<Integer> iterator = collection.iterator(); while (iterator.hasNext()) { Integer element = iterator.next(); sb.append(element); if (iterator.hasNext()) { sb.append(", "); } } sb.append("}"); return sb.toString(); } fun listConvert(collection: Collection<Int>): String { val sb = StringBuilder() sb.append("{") val iterator = collection.iterator() while (iterator.hasNext()) { val element = iterator.next() sb.append(element) if (iterator.hasNext()) { sb.append(", ") } } sb.append("}") return sb.toString() } fun listConvertKt(collection: Collection<Int>): String { return collection.joinToString(prefix = "{",postfix = "}") }
  • 8. COMMON IDIOMS & LANGUAGE PATTERNS https://www.flickr.com/photos/geraldford/6976818221/
  • 9. COMMON IDIOMS AND LANGUAGE CONCEPTS OVERVIEW ▸ Immutability ▸ String templates & Enum classes ▸ Null safety ▸ Properties and Fields ▸ Type inference and casts ▸ Data classes ▸ Syntactic sugar (loops, ranges etc) ▸ Extension functions ▸ Lambdas ▸ Collection API ▸ Type-safe builders ▸ Java-Kotlin-Interop
  • 10. COMMON IDIOMS AND LANGUAGE CONCEPTS IMMUTABILITY ▸ Built-in support for mutable and immutable variables, properties and fields ▸ Keywords var and val ▸ val - immutable (recommended) ▸ var - mutable ▸ Similar concept applies for class properties, val creates getters, var creates getters and setters (more later) val a: Int = 1 val b = 1 val c: Int c = 1 var x = 23 x += 1
  • 11. COMMON IDIOMS AND LANGUAGE CONCEPTS STRING TEMPLATES & ENUM CLASSES ▸ Kotlin Strings can contain template expressions ▸ Start with a $ character and ▸ can contain simple references: 
 $s ▸ complex expressions in curly braces: 
 ${s.length} ▸ Kotlin has a dedicated enum class, very similar to Java val s = "abc" val str = "$s.length is ${s.length}" enum class Locale(val hello: String) { DE_DE("Hallo"), EN_NZ("Hello"), MI_NZ("Kia Ora") } class Customer(val firstName:String, val lastName:String, val locale: Locale = Locale.DE_DE) { fun sayHello() = println("${locale.hello}, $firstName $lastName") } fun main(args : Array<String>) { val myCustomer = Customer("Sandra", "Musterfrau", Locale.MI_NZ) myCustomer.sayHello() }
  • 12. COMMON IDIOMS AND LANGUAGE CONCEPTS NULL SAFETY ▸ Motivation: A better way to deal with NPEs ▸ Kotlin differentiates nullable types from non- nullable types by adding a ? to the type: ▸ String: no nullable ▸ String?: nullable ▸ Handle manually or use Safe Call operator ?. or use the !! operator to allow/trigger a NPE. // Won't compile var lastName: String = null // Will compile var lastNameNullable: String? = null // Will also not compile println(lastNameNullable.length) // Option 1 (-1) println(if (lastNameNullable != null) lastNameNullable.length else -1) // Option 2 (null) println(lastNameNullable?.length) // Option 3 (NPE) println(lastNameNullable!!.length)
  • 13. COMMON IDIOMS AND LANGUAGE CONCEPTS PROPERTIES AND FIELDS ▸ Kotlin classes have mutable or immutable properties ▸ An automated backing field can be provided by the compiler (if deemed necessary) ▸ Default getter/setters for properties, can be customised ▸ lateinit modifier to deal with non-nullable properties that can’t be initialised in the constructor var counter = 0 set(value) { if (value >= 0) field = value } public class MyTest { lateinit var subject: TestSubject @SetUp fun setup() { subject = TestSubject() } @Test fun test() { subject.method() } }
  • 14. COMMON IDIOMS AND LANGUAGE CONCEPTS TYPE INFERENCE AND CASTS (I) ▸ When possible, Kotlin will infer the type of variables ▸ Explicit conversions, type widening and inference ▸ Smaller types are not subtypes of bigger types, no implicit conversion ▸ Types are often inferred from the context val b: Byte = 1 // This won't work val i: Int = b // This will val i: Int = b.toInt() val l = 1L + 3
  • 15. COMMON IDIOMS AND LANGUAGE CONCEPTS TYPE INFERENCE AND CASTS (II) ▸ is or !is checks if an object adheres to a certain type ▸ Smart cast: Compiler tracks is-expressions for immutable values ▸ works for val local variables and private, internal or in module performed casts ▸ works for var local variables if the variable hasn’t been modified between check and usage, never for var properties fun whatIs(x: Any) { when (x) { is Int -> println(x + 42) is String -> println(x.length) is IntArray -> println(x.sum()) } } whatIs(4) // 46 whatIs("4") // 1 whatIs(intArrayOf(1,2,3,4,5)) // 15
  • 16. COMMON IDIOMS AND LANGUAGE CONCEPTS DATA CLASSES ▸ The POJOs or Beans of other languages… ▸ Data classes implicitly create: ▸ getters/setters (the latter if a property is var) ▸ equals(), hashCode(), toString(), copy() - can be overwritten by custom implementations ▸ copy() has default parameters and can be used to alter a copy ▸ parameterless constructors need default parameters specified data class ChromeEncryptedPayload( val encryptedPayload: String, val encryptionHeader: String, val cryptoKeyHeader: String)
  • 17. COMMON IDIOMS AND LANGUAGE CONCEPTS EXTENSION FUNCTIONS ▸ Allow adding new functionality to a class without inheritance or Decorators ▸ Kotlin allows extension functions and extension properties ▸ Resolved statically, do not actually modify the class (excellent example why this has to be the case on https://kotlinlang.org/docs/ reference/extensions.html) fun Int.sum(otherInt: Int): Int = this + otherInt 3.sum(7) fun Activity.toast(message: CharSequence, duration: Int = TOAST.LENGTH_SHORT) { Toast.makeText(this, message, duration).show() } // In onCreate of an Activity override fun onCreate(...) { ... toast("Hi there") ... }
  • 18. COMMON IDIOMS AND LANGUAGE CONCEPTS JAVA-KOTLIN-INTEROP ▸ Java and Kotlin are fully interoperable from an integration point of view ▸ Your Java code can call and use Kotlin code ▸ Your Kotlin code can call and use Java code ▸ The latter is in particular useful because it means you can continue to use pretty much any existing Android/Java library ▸ Check out Hendrik Kokocinski’s sample Kotlin app that uses all kinds of well known Android/Java libs: https://github.com/blob0815/kotlin-android-sample
  • 19. COMMON IDIOMS AND LANGUAGE CONCEPTS OVERVIEW ▸ Immutability ▸ String templates & Enum classes ▸ Null safety ▸ Properties and Fields ▸ Type inference and casts ▸ Data classes ▸ Syntactic sugar (loops, ranges etc) ▸ Extension functions ▸ Lambdas ▸ Collection API ▸ Type-safe builders ▸ Java-Kotlin-Interop
  • 21. KOTLIN AND ANDROID TOOLCHAIN AND FLOW Java sourcesKotlin sources Bytecode javackotlinc Dalvik ART JVM
  • 22. KOTLIN AND ANDROID PROJECT SETUP ▸ Use Android Studio 1.5.x/2.x or IntelliJ 15/2016 ▸ Install Kotlin plugin (comes with “Jetbrains plugins” nowadays) ▸ Gradle dependencies project-level: ▸ classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.0.2" ▸ Gradle dependencies module-level: ▸ compile 'org.jetbrains.kotlin:kotlin-stdlib:1.0.2' ▸ apply plugin: 'kotlin-android' ▸ main.java.srcDirs += 'src/main/kotlin'
  • 23. KOTLIN AND ANDROID KOTLIN EXTENSIONS FOR ANDROID (I) ▸ Provides of a set of synthetic properties that bind views to those properties ▸ Alternative to Butter Knife-style bindings, no need for additional runtime library (Kotlin Extensions for Android are a Kotlin compiler plugin) ▸ import kotlinx.android.synthetic.main.<layout>.* ▸ import kotlinx.android.synthetic.main.<layout>.view.* ▸ usage: <componentid>.doSomething() ▸ Integrates nicely with build flavors, too
  • 24. KOTLIN AND ANDROID SYNTHETIC PROPERTIES package ventegocreative.co.nz.kotlindemo import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) helloworld.text = "Hey, I'm dynamically set" } }
  • 25. KOTLIN AND ANDROID KOTLIN EXTENSIONS FOR ANDROID (II) ▸ Used to be part of a separate plugin (https://plugins.jetbrains.com/plugin? pluginId=7717) ▸ obsolete since December 2015 ▸ functionality rolled into the main Kotlin plugin ▸ still needs a very particular setup (not properly documented but in this place: https://youtrack.jetbrains.com/issue/KT-10336)
 
 TLDR: move dependencies in app module(s), not top-level Gradle file.
  • 26. KOTLIN AND ANDROID KOTLIN EXTENSIONS FOR ANDROID (III) ▸ The current assumption is that Jetbrains plan to add more to the Kotlin Extensions for Android, but we don’t really know for sure at this stage. ▸ Current feature of view binding: ▸ Fresh take on view binding ▸ Very simple to setup and use ▸ Overall less powerful than Butter Knife, Kotter Knife
  • 27. ANKO
  • 28. ANKO A DSL FOR LAYOUTS ▸ Developed and maintained by Jetbrains, under Apache 2.0 license ▸ The most important element of Anko is the Layout DSL ▸ Idea: Replace XML layout definitions by Kotlin code - without having to build the layout in a truly programmatic sense ▸ Modular - as we’re talking about UI/Layout, it’s very important to select the right library for your minSDKVersion ▸ Extensible - you can add your own DSL elements for custom UI controls
  • 30. ANKO PROGRAMMATIC LAYOUT IN KOTLIN val act = this val layout = LinearLayout(act) layout.orientation = LinearLayout.VERTICAL val name = EditText(act) val button = Button(act) button.text = "Say Hello" button.setOnClickListener { Toast.makeText(act, "Hello, ${name.text}!", Toast.LENGTH_SHORT).show() } layout.addView(name) layout.addView(button)
  • 31. ANKO ANKO DSL verticalLayout { val name = editText() button("Say Hello") { onClick { toast("Hello, ${name.text}!") } } }
  • 32. ANKO BUT THERE’S MORE (ANKO “ADVANCED TOPICS”) ▸ Intent Wrappers for various purposes: e.g. sendSMS(number, [text]) ▸ Service shortcuts ▸ Configuration qualifiers: configuration(screenSize = ScreenSize.LARGE, orientation = Orientation.LANDSCAPE) { … } ▸ Asynchronous tasks ▸ SQLLite ▸ Removes all the tragic cursor handling and lot of the try/catch blocks necessary
  • 34. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID OVERVIEW ▸ Kotter Knife ▸ Butter Knife ▸ KAndroid ▸ Kovenant ▸ Quickies: Fuel, Injekt, Spek, Kotson
  • 35. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KOTTER KNIFE ▸ We don’t like findByViewId(…) - read: https://ragunathjawahar.wordpress.com/ 2015/03/23/kotlin-findviewbyid-dead-as-dinosaurs ▸ Kotter Knife provides view binding in a similar way to Butter Knife for Android/ Java ▸ Why Kotter Knife (runtime library) over Kotlin Android Extensions (compiler plugin)? ▸ Porting code from Java/Butter Knife to Kotlin ▸ Features like listener binding and resources binding that don’t exist in KAE.
  • 36. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID BUTTER KNIFE ▸ Not much to say about it, probably one of the most famous libraries in the Android world. ▸ Butter Knife was difficult to use with Kotlin in the beginning ▸ Since Kotlin 1.0 RC you can in fact just use Butter Knife in your Kotlin code ▸ Sample code for Butter Knife/Kotlin in the official Jetbrains Kotlin Examples repo (https://github.com/JetBrains/kotlin-examples/tree/master/gradle/ android-butterknife)
  • 37. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KOTTER KNIFE VS BUTTER KNIFE VS KAE (I) findViewById(R.id.send).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("MainActivity", "onClick: send") } }); @OnClick(R.id.send) void clickSend(View v) { Log.d("MainActivity", "onClick: send") }
  • 38. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KOTTER KNIFE VS BUTTER KNIFE VS KAE (II) findViewById(R.id.send).setOnClickListener { view -> Log.d("MainActivity", "onClick: send") }; val btnSend: Button by bindView(R.id.send) btnSend.setOnClickListener({ view -> Log.d("MainActivity", "onClick: send") }) import kotlinx.android.synthetic.activity_main.* btn_send.setOnClickListener({ view -> Log.d("MainActivity", "onClick: send") })
  • 39. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KANDROID ▸ KAndroid is an extension library for Kotlin/Android ▸ Contains a variety of absolutely distinct and unconnected functionality ▸ Common theme: let’s get rid of boilerplate code ▸ Apache 2.0 license
  • 40. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KANDROID FEATURES ▸ View binding (again) ▸ TextWatcher ▸ SeekBar Extension ▸ SearchView Extension ▸ Shortcuts to system services ▸ Logging ▸ Dealing with Intents ▸ SDK Version API (from/to) ▸ Thread management
  • 41. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KANDROID EXAMPLES runDelayed(1000) { // delayed execution } runDelayedOnUiThread(5000) { // delayed UI update } toApi(16, inclusive = true) { // handle devices running older APIs }
  • 42. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KOVENANT ▸ In a nutshell: Promises for Kotlin ▸ Very modular built, you can essentially pick and choose the artifacts of Kovenant that you’d like to use - Kovenant is not an Android-specific library ▸ Good staring set for Android: core, android, combine, jvm, functional ▸ MIT license
  • 43. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KOVENANT FEATURES ▸ Core, foundations of a Promise framework ▸ Tasks & Callbacks ▸ Chaining (Then, ThenApply) ▸ Lazy Promises ▸ Cancelling and Voiding ▸ Combine: combines 2-20 promises ▸ Functional: adds map, bind and apply to support more advanced HOF constructs in Kovenant ▸ JVM: Executors and Throttles (thread pools) ▸ Android: UI callbacks and interacting with UI Thread
  • 44. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KOVENANT EXAMPLES (I) task { // some long-running thing } success { println("result: $it") } fail { println("problem: ${it.message}") } always { // this will always happen }
  • 45. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID KOVENANT EXAMPLES (II) promiseOnUi { // do some UI preparation } then { // the actual work } successUi { // update UI }
  • 46. OTHER LIBRARIES AND TOOLS FOR KOTLIN AND ANDROID OVERVIEW ▸ Kotter Knife ▸ Butter Knife ▸ KAndroid ▸ Kovenant ▸ Quickies: Fuel, Injekt, Spek, Kotson
  • 48. FINAL THOUGHTS PERFORMANCE ▸ Runtime is pretty much on-par with Java ▸ Pre Kotlin 1.0.2: Build process is slower than a comparable app in Java - mainly due to how the Kotlin compiler works (no partial builds/compilation) ▸ Kotlin libraries do add to the size of the application as well as to the method count ▸ Kotlin runtime + stdlib are similar in method count to support-v4 or play- services-base and add significantly less than Scala or Groovy
  • 49. FINAL THOUGHTS LANGUAGE AND MATURITY ▸ Kotlin 1.0 was a big step for the language ▸ Surprisingly mature for a 1.0 release (but 5+ years in the making) ▸ Full of great concepts and idioms ▸ Refreshing language that makes both Android and JVM development significantly more pleasant and fun ▸ Issue: Tooling around static analysis is non-existent at the moment (some basic listing for Android is available since 1.0.2) ▸ Ready for prime-time yet?
  • 50. FINAL THOUGHTS WHAT DID WE LEARN? ▸ What is Kotlin? ▸ Common idioms and language concepts ▸ Kotlin and Android ▸ Anko ▸ Other libraries and tools for Kotlin and Android
  • 51. RESOURCES RESOURCES ▸ Kotlin: http://kotlinlang.org ▸ Anko: https://github.com/Kotlin/anko ▸ Kotter Knife: https://github.com/JakeWharton/kotterknife ▸ KAndroid: https://github.com/pawegio/KAndroid ▸ Kovenant: https://github.com/mplatvoet/kovenant ▸ Fuel: https://github.com/kittinunf/Fuel ▸ Injekt: https://github.com/kohesive/injekt ▸ Spek: http://jetbrains.github.io/spek/ ▸ Kotson: https://github.com/SalomonBrys/Kotson
  • 52. RESOURCES GET IN TOUCH Kai Koenig Email: [email protected] Twitter: @AgentK