Slide 1

Slide 1 text

Develop your next app with otlin

Slide 2

Slide 2 text

@arnogiu Work @ ekito Gear Maker Mobile & Cloud Arnaud GIULIANI ekito.fr/people

Slide 3

Slide 3 text

Yet another JVM Language ?

Slide 4

Slide 4 text

The new « Swift » for Android ? https://goo.gl/W5g6Do - mid 2014

Slide 5

Slide 5 text

Limits of java - Verbose - Type inference - No properties / Lazy / Delegate - Checked exception - NullPointerException - Extensibility - End of lines with ; @ sdeleuze

Slide 6

Slide 6 text

But Java is great … - Fast - Optimized bytecode - Static typing - Simple to learn - Amazing ecosystem

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

« production ready » 15.02.2016

Slide 9

Slide 9 text

1.1.2 @ 25.04.2017

Slide 10

Slide 10 text

- Conciseness & Smarter API - Null Safety & Immutability protection - Type Inference, Static Typing & Smart Casts - Open programming styles - Java Interop Why Kotlin ?

Slide 11

Slide 11 text

- Fully open source (built by Jetbrains) - Based on Java & run on Java 6+ - Modern language features - 1st grade tooling What’s Kotlin ?

Slide 12

Slide 12 text

- String templates - Properties - Lambdas - Data class - Smart cast - Null safety - Lazy property - Default values for function parameters - Extension Functions - No more ; - Single-expression functions - When expression - let, apply, use, with - Collections - Android Extensions Plugin About Kotlin …

Slide 13

Slide 13 text

Compare with Same conciseness and expressive code, but Kotlin static typing and null-safety make a big difference. "Some people say Kotlin has 80% the power of Scala, with 20% of the features" * 
 "Kotlin is a software engineering language in contrast to Scala which is a computing science language." * Swift and Kotlin are VERY similar. Swift is LLVM based and has C interop while Kotlin is JVM based and has Java interop. * Quotes from Kotlin: The Ying and Yang of Programming Languages @ sdeleuze

Slide 14

Slide 14 text

Open to functional programming !

Slide 15

Slide 15 text

is Kotlin Android friendly ? https://github.com/SidneyXu/AndroidDemoIn4Languages Method counts by Language

Slide 16

Slide 16 text

Java 8 on Android ? https://android-developers.googleblog.com/2017/03/future-of-java-8-language-feature.html

Slide 17

Slide 17 text

KOTLIN is not just about writing your app with lesser lines. It’s all about writing SAFER & BETTER APPS !

Slide 18

Slide 18 text

Clearly considered by all the Java community ! https://spring.io/blog/2017/01/04/introducing-kotlin-support-in- spring-framework-5-0 http://www.javamagazine.mozaicreader.com/ #&pageSet=5&page=0&contentItem=0 (March/April 2017) https://www.thoughtworks.com/radar/languages-and-frameworks/kotlin

Slide 19

Slide 19 text

https://antonioleiva.com/google-kotlin/

Slide 20

Slide 20 text

Ready to use

Slide 21

Slide 21 text

IntelliJ

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Getting started with gradle buildscript { ext.kotlin_version = '' dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } apply plugin: "kotlin" // or apply plugin: "kotlin-android" android { sourceSets {
 main.java.srcDirs += 'src/main/kotlin'
 test.java.srcDirs += 'src/test/kotlin'
 } } dependencies { compile « org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version" } * You can now use Kotlin in your grade scripts !

Slide 24

Slide 24 text

Gradle tips for Kotlin # Gradle
 org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX: +HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
 org.gradle.parallel=true org.gradle.deamon=true
 
 # Kotlin
 kotlin.incremental=true
 
 # Android Studio 2.2+
 android.enableBuildCache=true In your gradle.properties https://medium.com/keepsafe-engineering/kotlin-vs-java-compilation-speed-e6c174b39b5d#.k44v7axsk

Slide 25

Slide 25 text

Conversion Assistant

Slide 26

Slide 26 text

Let’s go !

Slide 27

Slide 27 text

Basics

Slide 28

Slide 28 text

VAR & VAL val : constant value - IMMUTABLE var : variable value - MUTABLE val icon1 : String = "sunny"
 icon1 = "cloudy" // error - val cannot be reassigned
 
 var icon2 = "sunny"
 icon2 = "cloudy" USE VAL AS MUCH AS POSSIBLE ! TYPE INFERENCE

Slide 29

Slide 29 text

Class class DailyForecastModel(val forecastString: String, 
 val icon: String, 
 val temperatureLow: String, 
 val temperatureHigh: String) Optional body Public/Closed by default val forecast = DailyForecastModel("description ...","sunny-icon","5°C","18°C") POJO + - Getter - Setter - Constructors

Slide 30

Slide 30 text

Properties class DailyForecastModel(val forecastString: String,
 val icon: String,
 val temperatureLow: String,
 val temperatureHigh: String) {
 
 val temperatureString = "$temperatureLow°C - $temperatureHigh°C"
 
 } val forecast = ...
 println("temperature is ${forecast.temperatureString}") Property access & string template val property = read only property !

Slide 31

Slide 31 text

Data Class data class DailyForecastModel(val forecastString: String,
 val icon: String,
 val temperatureLow: String,
 val temperatureHigh: String) POJO + - Getter - Setter - Constructors - toString - hashcode - equals - copy

Slide 32

Slide 32 text

data class DailyForecastModel(val forecastString: String, 
 val icon: String, 
 val temperatureLow: String, 
 val temperatureHigh: String) Kotlin public class DailyForecastModel {
 
 private String forecast;
 private String icon;
 private String temperatureLow;
 private String temperatureHigh;
 
 /**
 * Business Getter
 */
 public String getTemperatureString() {
 return temperatureLow + "°C - " + temperatureHigh + "°C";
 }
 
 /**
 * Business Getter
 */
 public String getForecastString() {
 return forecast;
 }
 
 
 public DailyForecastModel() {
 }
 
 public DailyForecastModel(String forecast, String icon, String temperatureLow, String temperatureHigh) {
 this.forecast = forecast;
 this.icon = icon;
 this.temperatureLow = temperatureLow;
 this.temperatureHigh = temperatureHigh;
 }
 
 
 public String getForecast() {
 return forecast;
 }
 
 public void setForecast(String forecast) {
 this.forecast = forecast;
 }
 
 public String getIcon() {
 return icon;
 }
 
 public void setIcon(String icon) {
 this.icon = icon;
 }
 
 public String getTemperatureLow() {
 return temperatureLow;
 }
 
 public String getTemperatureHigh() {
 return temperatureHigh;
 }
 
 @Override
 public boolean equals(Object o) {
 if (this == o) return true;
 if (o == null || getClass() != o.getClass()) return false;
 
 DailyForecastModel that = (DailyForecastModel) o;
 
 if (forecast != null ? !forecast.equals(that.forecast) : that.forecast != null)
 return false;
 if (icon != null ? !icon.equals(that.icon) : that.icon != null) return false;
 if (temperatureLow != null ? !temperatureLow.equals(that.temperatureLow) : that.temperatureLow != null)
 return false;
 return temperatureHigh != null ? temperatureHigh.equals(that.temperatureHigh) : that.temperatureHigh == null;
 
 }
 
 @Override
 public int hashCode() {
 int result = forecast != null ? forecast.hashCode() : 0;
 result = 31 * result + (icon != null ? icon.hashCode() : 0);
 result = 31 * result + (temperatureLow != null ? temperatureLow.hashCode() : 0);
 result = 31 * result + (temperatureHigh != null ? temperatureHigh.hashCode() : 0);
 return result;
 }
 
 @Override
 public String toString() {
 return "DailyForecastModel{" +
 "forecast='" + forecast + '\'' +
 ", icon='" + icon + '\'' +
 ", temperatureLow='" + temperatureLow + '\'' +
 ", temperatureHigh='" + temperatureHigh + '\'' +
 '}';
 }
 }
 Java

Slide 33

Slide 33 text

VAR & VAL val forecast1 = DailyForecastModel("description … »,"sunny-icon","5°C","18°C") var forecast2 = DailyForecastModel("description … »,"sunny-icon","5°C","18°C") forecast1 = DailyForecastModel("description … »,"sunny-icon","5°C","18°C") // error : val cannot be reassigned 
 forecast2 = DailyForecastModel("description …","sunny-icon","5°C","18°C")
 Use Immutable Value Objects !

Slide 34

Slide 34 text

Methods & Functions fun reformat(str: String,
 normalizeCase: Boolean = true,
 upperCaseFirstLetter: Boolean = true,
 wordSeparator: Char = ' ') : String {
 
 } - Parameters are IMMUTABLE (considered as VAL) - Parameter has a NAME & can have DEFAULT VALUE - Return type can be optional (single expression) reformat(str, true, true, '_') // old way to call
 reformat(str, wordSeparator = '_') // using default values & named params

Slide 35

Slide 35 text

No static, use Object object WeatherSDKUtil {
 
 fun extractLocation(geocode: Geocode): Location = ...
 
 fun getDailyForecasts(weather: Weather): List {
 ...
 }
 } Singleton Class data class DailyForecastModel(val forecastString: String, val icon: String, val temperatureLow: String, val temperatureHigh: String) {
 
 val temperatureString = "$temperatureLow°C - $temperatureHigh°C"
 
 companion object{
 
 fun sunnyDay() = DailyForecastModel("It's a good day today !","sunny","16°C","25°C")
 
 }
 } Companion Object

Slide 36

Slide 36 text

Null Safety val forecast1 : DailyForecastModel? = DailyForecastModel("description ...","sunny-icon","5°C","18°C")
 val forecast2 : DailyForecastModel? = null 
 val forecast : DailyForecastModel = null // Error because type is a non- null type val forecast : DailyForecastModel? = … - Nullable type : accept null value - Type is nullable when followed by ? You must use proper operators with nullable types !

Slide 37

Slide 37 text

Safe Calls with ?. val forecast : DailyForecastModel? = DailyForecastModel("description ...","sunny- icon","5°C","18°C")
 val myIcon: String? = forecast?.icon Nullable Type Operators fun extractLocation(geocode: Geocode): Location? = geocode?.geometry?.location Exploring safely objects val forecast : DailyForecastModel? = DailyForecastModel("description ...","sunny-icon","5°C","18°C")
 
 if (forecast != null){
 // forecast is now type DailyForecastModel
 val icon : String = forecast.icon
 } if-non-null checks & smart casts

Slide 38

Slide 38 text

Collections val list = listOf("a", "b", « c","aa") val map = mapOf("a" to 1, "b" to 2, "c" to 3) list.filter { it.startsWith("a") } Collections operators with lambdas : map, filter, take, flatMap, forEach, firstOrNull, last … - Based on Java Collections, but add some smart API - Mutable / Immutable Collections // range
 for (i in 1..100) { //... } Range expression for for-loop map["a"] = "my value" // direct map access with array style 
 for ((k, v) in map) {
 println("$k -> $v")
 } Smart accessors

Slide 39

Slide 39 text

Kotlin Collection Java 7 Collection Extraction Filter & Select

Slide 40

Slide 40 text

InterOp Kotlin/Java Java Kotlin

Slide 41

Slide 41 text

InterOp Java/Kotlin data class DailyForecastModel (...){
 companion object{
 fun sunnyDay(){ //…
 }
 }
 } DailyForecastModel u = new DailyForecastModel(…);
 DailyForecastModel.Companion.sunnyDay(); fun getDailyForecasts(weather : Weather?){
 …
 } WeatherSDKUtilKt.getDailyForecasts(weather) object WeatherSDKUtil{
 fun extractLocation(geocode : Geocode) …
 } WeatherSDKUtil.INSTANCE.extractLocation(geocode)

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

Advanced

Slide 44

Slide 44 text

Default values with Elvis operator ?: val icon: String = if (forecast != null) forecast.icon else "sunny"
 val icon: String = forecast?.icon ?: "sunny" Others Nullable Type Operators val icon: String = forecast!!.icon Explicit (dangerous) non-null call with !!. Safe cast with as? val icon: String? = forecast?.icon as? String fun extractLocation(geocode: Geocode): Location? = geocode.results.firstOrNull()?.geometry?.location Collection with nullable types

Slide 45

Slide 45 text

Advanced Properties Getter & Setter var temperatureString : String
 get() = "temperature is $tempData"
 set(value){
 tempData = value
 } Late Initialization class MainApplication : Application() {
 
 override fun onCreate() {
 ...
 instance = this
 }
 
 companion object {
 
 private lateinit var instance: MainApplication
 
 fun get(): Application {
 return instance
 }
 }
 }

Slide 46

Slide 46 text

When when (x) {
 1 -> print("x == 1")
 2 -> print("x == 2")
 else -> { // Note the block
 print("x is neither 1 nor 2")
 }
 } Flow Control (replace your switch and if-blocks) when (x) {
 in 1..10 -> print("x is in the range")
 in validNumbers -> print("x is valid")
 !in 10..20 -> print("x is outside the range")
 else -> print("none of the above")
 } Pattern Matching

Slide 47

Slide 47 text

Lambdas val fab = findViewById(R.id.fab) as FloatingActionButton
 fab.setOnClickListener { view -> popLocationDialog(view) } A lambda expression or an anonymous function is a “function literal”, i.e. a function that is not declared, but passed immediately as an expression - A lambda expression is always surrounded by curly braces - parameters are declared before -> (parameter types may be omitted) - body goes after -> (when present) myNumber.split("\n").flatMap { it.split(separator) }
 .map(Integer::parseInt)
 .map(::checkPositiveNumber)
 .filter { it <= 1000 }
 .sum() Method references are not surrounded by parenthesis !

Slide 48

Slide 48 text

Extensions Functions Extension declaration package fr.ekito.myweatherlibrary.json.weather
 fun Weather.getDailyForecasts(): List = forecast?.simpleforecast?.forecastday.orEmpty()
 .map { f -> DailyForecastModel(f.conditions!!, getWeatherCode(f.icon!!), f.low!!.celsius!!, f.high!!.celsius!!) }
 .filter { f -> !f.icon.startsWith(PREFIX) }
 .take(4) Old style … val forecasts = WeatherSDKUtil.getDailyForecasts(weather) val forecasts = weather.getDailyForecasts() Using extension

Slide 49

Slide 49 text

Destructuring Declarations In lambdas, Since Kotlin 1.1 map.mapValues { (key, value) -> "$value!" } Destructured variables declaration val (_, _, low, high) = DailyForecastModel("description ...","sunny- icon","5°C","18°C")
 
 println("Today we have $low < $high") Works with Map, Data Class, Pair, Triple …

Slide 50

Slide 50 text

Kotlin’s Android Extensions apply plugin: 'com.android.application' apply plugin: ‘kotlin-android’ apply plugin: ‘kotlin-android-extensions’ // the kotlin-android extension ! In your build.gradle

Slide 51

Slide 51 text

Kotlin Java

Slide 52

Slide 52 text

Reactive Programming

Slide 53

Slide 53 text

Lamba expressions Functional Programming Reactive Streams http://reactivex.io/documentation/operators.html http://www.reactive-streams.org/ https://spring.io/blog/2016/06/07/notes-on-reactive- programming-part-i-the-reactive-landscape

Slide 54

Slide 54 text

Kotlin Java 7 RxJava

Slide 55

Slide 55 text

DSL https://github.com/Kotlin/anko verticalLayout { val name = editText() button("Say Hello") { onClick { toast("Hello, ${name.text}!") } } } { ("/blog" and accept(TEXT_HTML)).route { GET("/", this@BlogController::findAllView) GET("/{slug}", this@BlogController::findOneView) } ("/api/blog" and accept(APPLICATION_JSON)).route { GET("/", this@BlogController::findAll) GET("/{id}", this@BlogController::findOne) } } https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0

Slide 56

Slide 56 text

Ready to go ?

Slide 57

Slide 57 text

Where to start ?

Slide 58

Slide 58 text

58 Production ready ?

Slide 59

Slide 59 text

It’s always hard to start !

Slide 60

Slide 60 text

IF YOU DON'T LOOK AT JAVA AND THINK, "THIS COULD BE BETTER", DON'T SWITCH.

Slide 61

Slide 61 text

Our feedback - Easy to start on existing project - Great learning curve - Good doc & tools support - Don’t use anymore Butterknife, Dagger … - ⚠ Tests Mocks, Realm DB … - Hard to come back to Java https://www.ekito.fr/people/kotlin-in-production-one-year-later/

Slide 62

Slide 62 text

62 Next in Kotlin

Slide 63

Slide 63 text

- Coroutines - JS Support - New language features (type aliases …) - Kotlin Lib update - Java 8 Optimization

Slide 64

Slide 64 text

- Tech Preview ;) - LLVM Compile (no more JVM) - Native library integration - MacOS, Linux, Raspberry Pi

Slide 65

Slide 65 text

Many well-known companies are using Kotlin: Pinterest, Coursera, Netflix, Uber, Square, Trello, Basecamp, amongst others well-known banks (such as Goldman Sachs, Wells Fargo, J.P. Morgan, Deutsche Bank, UBS, HSBC, BNP Paribas, Société Générale)

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

Try Kotlin online http://try.kotlinlang.org/#/Examples/ Kotlin Reference https://kotlinlang.org/docs/reference/ Kotlin Cheat Sheet (by ekito) https://speakerdeck.com/agiuliani/kotlin-cheat- sheet

Slide 68

Slide 68 text

Just try it !

Slide 69

Slide 69 text

Liiiiiiive coding !

Slide 70

Slide 70 text

70% less ! Develop a weather app with K

Slide 71

Slide 71 text

Thank you :)