Slide 1

Slide 1 text

for Android Developers @camachoyury

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Java ● Missing modern features ● NPE ● JDK 6-7 ● No lambdas ● No streams ● Too Much ceremony

Slide 4

Slide 4 text

History ● 2011, released as official project by Jetbrains ● 2012, Jetbrains Open Sourced the project, license Apache 2 ● 2015, released first Kotlin beta version 1.0 ● 2016, Kotlin 1.0 released ● 2016 Gradle announces official support ● 2017, released 1.1 ● and…….

Slide 5

Slide 5 text

History

Slide 6

Slide 6 text

What is Kotlin?

Slide 7

Slide 7 text

What is Kotlin?

Slide 8

Slide 8 text

Where can this run? JVM Android Browser Native

Slide 9

Slide 9 text

Which tools I could use?

Slide 10

Slide 10 text

What is Kotlin? ● JVM ● Seems Java ● Object Oriented ● Functional ● Do more with less ● Complete interop with Java ● Null safety

Slide 11

Slide 11 text

Source: https://goo.gl/KkMPYg What is Kotlin?

Slide 12

Slide 12 text

No classes are needed fun main(args: Array){ println("Hello GDG Lima!! You Rocks") }

Slide 13

Slide 13 text

Variables val name: String = "Kylo Ren" var side = "Force" side = "Dark Side" name = "Kylo" //no compile!!! and mutability

Slide 14

Slide 14 text

Functions fun tellMeYourName(person: Person) :String { return person.name }

Slide 15

Slide 15 text

Null Safety var name: String = null name = "Juan" name?.trim() // does runs name!!.trim() // runs anyway ? val lastName: String = "Perez" lastName.trim() // no need for "?."

Slide 16

Slide 16 text

No Lambdas in Java public List evens(List nums){ List numsCopy = new ArrayList<>(nums); Iterator numsItr = numsCopy . listIterator (); while (numsItr.hasNext()) { Integer num = numsItr . next (); if (num % 2 != 0) numsItr.remove(); } return numsCopy; }

Slide 17

Slide 17 text

Lambdas fun evens(nums: List) = nums.filter { it % 2 == 0 }

Slide 18

Slide 18 text

POJO JAVA public static class Person { private String mName; public Person(String name) { mName = name; } public String getName() { return mName; } public void setName(String name) { mName = name; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Person person = (Person) o; return mName != null ? mName.equals(person.mName) : person.mName == null; } @Override public int hashCode() { return mName != null ? mName.hashCode() : 0; } }

Slide 19

Slide 19 text

Bye Bye POJO data class Person(var name: String)

Slide 20

Slide 20 text

Optional Arguments class Character(name: String, gun: Boolean = false ){ val name = name; var gun = gun } var bruceLee = Character("Bruce Lee") var rambo = Character("Rambo", true)

Slide 21

Slide 21 text

Named Arguments class Character(name: String, gun: Boolean = false ){ val name = name; var gun = gun } var rambo = Character(gun = true, name = "Rambo")

Slide 22

Slide 22 text

Extension Functions fun String.printYourSelf(){ println(this) } "Force be with you!!!".printYourSelf()

Slide 23

Slide 23 text

Extension Functions infix fun Character.fight(character: Character){ println("fighting ${this.name} and ${character.name} ") } rambo fight bruceLee

Slide 24

Slide 24 text

Inheritance open class Person(var name: String){ } class Character(name: String) : Person(name){ }

Slide 25

Slide 25 text

Operator Overload operator fun Character.plus(character: Character){ println( "${this.name} y ${character.name} ") } jhonSmith + janeSmith > Jhon Smith y Jane Smith

Slide 26

Slide 26 text

Kotlin For Android Developers

Slide 27

Slide 27 text

Extension functions in android //JAVA Toast.makeText(getBaseContext(),"#IOLima2017", Toast.LENGTH_SHORT).show(); //Kotlin fun Activity.toast(message: CharSequence, duration: Int = Toast.LENGTH_LONG){ Toast.makeText(this, message, duration).show() } toast("#IOLima2017")

Slide 28

Slide 28 text

Extension functions in android //JAVA public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.main, container, false); } //KOTLIN fun ViewGroup.inflate(layoutId: Int, attachRoot:Boolean = false): View { return LayoutInflater.from(context).inflate(layoutId, this, attachRoot) } return container.inflate(R.layout.main)

Slide 29

Slide 29 text

Android extensions ● Better than Butter Knife ● No need for instance variables

Slide 30

Slide 30 text

how use it ? Add this line in Build.gradle apply plugin: 'kotlin-android-extensions' Import synthetic layout import kotlinx.android.synthetic..*

Slide 31

Slide 31 text

Example with Java public class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main) TextView text = (TextView) findViewById(R.id.hello); } }

Slide 32

Slide 32 text

Example Android Extensions import kotlinx.android.synthetic.main.* public class MyActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.main) this.hello.setText("Hi!") } }

Slide 33

Slide 33 text

Example: Login Facebook - Java //Java loginButton = (LoginButton)findViewById(R.id.login_button); loginButton.registerCallback(callbackManage......

Slide 34

Slide 34 text

Example : Login Facebook - Kotlin //Kotlin loginButton!!.registerCallback(callbackManager, facebookCallback)

Slide 35

Slide 35 text

Anko ● Programmatic layouts ● DSL instead of XML ● Compact and readable ● Ide Integration with previews

Slide 36

Slide 36 text

Anko Source: blog.jetbrains.com

Slide 37

Slide 37 text

Adopters

Slide 38

Slide 38 text

Example code: Kotlin and Vision API https://github.com/camachoyury/FaceTracker

Slide 39

Slide 39 text

Questions?, sure please!

Slide 40

Slide 40 text

Main Reference https://kotlinlang.org https://antonioleiva.com/ Books:

Slide 41

Slide 41 text

Other references http://kotlin.es/ http://devcode.la/cursos/kotlin https://codigofacilito.com/cursos/kotli https://www.udemy.com/curso-kotlin/ https://devexperto.com/

Slide 42

Slide 42 text

Gracias! @camachoyury