Slide 1

Slide 1 text

Kotlin 101: The Basics for Android Development Wellington Costa Pereira Android Lead @ Aegro

Slide 2

Slide 2 text

What is Kotlin?

Slide 3

Slide 3 text

Kotlin was developed internally by Jetbrains, and it was revealed in July 2011 as a new programming language for the JVM. In February 2012, Jetbrains open sourced the project under the Apache 2 license. The main goal of the project was to write Jetbrains products like IntelliJ IDEA. The first stable version was released in February 2016. During Google I/O 2017, Google announced the support for Kotlin on Android. Since then, Kotlin has been the official recommended language for Android development. What is Kotlin?

Slide 4

Slide 4 text

Kotlin is an open source programming language, maintained by Jetbrains, Google and the community, and it's a modern language with powerful features like null-safety and asynchronous programming. What is Kotlin?

Slide 5

Slide 5 text

Expressive and Concise Express your ideas writing less boilerplate code

Slide 6

Slide 6 text

Safe code Forget NullPointerException in your code

Slide 7

Slide 7 text

Interoperable You can migrate your Java project with low efforts

Slide 8

Slide 8 text

Structured concurrency You can write asynchronous code with no complications using Coroutines

Slide 9

Slide 9 text

Kotlin has a great ecosystem, such as first-class support for Android development, backend development powered by Ktor and multiplaform development (JVM, Javascript, Native). Kotlin also has a great community, counting +30k stars on main Github repository, +30k members on official Slack and +50k questions on Stackoverflow. In 2018, Stackoverflow published a survey that introduced Kotlin at the second position about the most loved programming languages. Kotlin ecosystem and community

Slide 10

Slide 10 text

A lot of big companies are adopting Kotlin in their projects, such as Evernote, Coursera and Atlassian. And here at Brazil, we have a lot of companies using Kotlin, such as Aegro, Creditas, C6 Bank, Hash Payments, Itaú and so on. And a lot of popular open source projects are adding support for Kotlin, such as Gradle and Spring. Kotlin adoption

Slide 11

Slide 11 text

So, let’s getting started

Slide 12

Slide 12 text

Agenda • Basic syntax • Properties and functions • Classes, interfaces and objects • Java interoperability

Slide 13

Slide 13 text

Basic syntax Declaring a main function: fun main(args: Array) { val greeting: String = "Developer Student Club" println("Hello, " + greeting) }

Slide 14

Slide 14 text

Basic syntax Declaring a main function with no args: fun main() { val greeting: String = "Developer Student Club" println("Hello, " + greeting) } * In Kotlin, the args parameter in main function isn’t mandatory.

Slide 15

Slide 15 text

Basic syntax Join strings using String Templates: fun main() { val greeting: String = "Developer Student Club" println("Hello, $greeting”) } In terms of syntax, there is a notable difference, since using templates, you compose the string in a more fashion way. In terms of runtime, there is no big difference, since the Kotlin compiler produces the same code for both string concatenation using + operator and string templates.

Slide 16

Slide 16 text

Basic syntax Control flow using if/else statement: fun someBusinessLogic() { if (myCondition) { !// execute some action } else if (anotherCondition) { !// execute another action } else { !// alternative flow goes here } }

Slide 17

Slide 17 text

Basic syntax Assignment using if/else statement: fun isLoading(value: Boolean) { val visibility = if (value) View.VISIBLE else View.GONE progressBar.visibility = visibility } * Kotlin does not have ternary operator.

Slide 18

Slide 18 text

Basic syntax Control flow using when statement: enum class State { START, IN_PROGRESS, DONE, ERROR } fun processState(state: State) { when (state) { START !-> TODO() IN_PROGRESS !-> TODO() DONE !-> TODO() ERROR !-> TODO() else !-> TODO() } }

Slide 19

Slide 19 text

Basic syntax Assignment using when statement: fun getCodeFrom(state: State): Int { val code = when (state) { START !-> 1 IN_PROGRESS !-> 2 DONE !-> 3 ERROR !-> 4 else !-> 0 } return code }

Slide 20

Slide 20 text

Basic syntax Inline return using when statement: fun getCodeFrom(state: State): Int { return when (state) { START !-> 1 IN_PROGRESS !-> 2 DONE !-> 3 ERROR !-> 4 else !-> 0 } }

Slide 21

Slide 21 text

Basic syntax Declaring a for loop: fun printAllGuests(guests: List) { for (guest in guests) { println("Hello, $guest") } }

Slide 22

Slide 22 text

Basic syntax Declaring an indexed for loop: fun printAllGuests(guests: List) { for ((index, guest) in guests.withIndex()) { println("Hello, #$index - $guest") } }

Slide 23

Slide 23 text

Basic syntax Iteration over a collection using Collection API: fun printAllGuests(guests: List) { guests.forEach { guest !-> println("Hello, $guest") } }

Slide 24

Slide 24 text

Properties and functions Declaring a mutable property: var name: String = "Wellington" name = "Chico" !// it works well since var properties can be reassigned

Slide 25

Slide 25 text

Properties and functions Declaring an immutable property: val age: Int = 25 age = 20 !// it produces a compiler error because val properties cannot be reassigned Immutable states are a good way to keep a consistent state during program execution. If you want a new value, you need to produce a new state instead of changing the current one.

Slide 26

Slide 26 text

Properties and functions Declaring a function: fun greeting() { println("Hello!") }

Slide 27

Slide 27 text

Properties and functions Declaring a function with a return: fun getGreetingMessage(): String { return "Hello!" }

Slide 28

Slide 28 text

Properties and functions Declaring a function with a return and parameter: fun greetingFor(guest: String): String { return "Hello, $guest!" }

Slide 29

Slide 29 text

Properties and functions Declaring a function with a return and parameter with default value: fun greetingFor(guest: String = "Chico"): String { return "Hello, $guest!" }

Slide 30

Slide 30 text

Properties and functions Inferring the return type of a function: fun greetingFor(guest: String = "Chico") = "Hello, $guest!"

Slide 31

Slide 31 text

Properties and functions Declaring an extension function: fun Double.pow(exponent: Double): Double { return java.lang.Math.pow(this, exponent) } fun main() { 2.0.pow(10.0) }

Slide 32

Slide 32 text

Properties and functions Making an extension function to use infix notation: infix fun Double.pow(exponent: Double): Double { return java.lang.Math.pow(this, exponent) } fun main() { 2.0 pow 10.0 }

Slide 33

Slide 33 text

Classes, interfaces and objects Declaring a regular class: class AuthService { private var attempts: Int = 0 fun auth(username: String, password: String): Boolean { while (attempts < 3) { val response = authApi.call(username, password) if (response.isFailure) { attempts!++ continue } return true } return false } }

Slide 34

Slide 34 text

Classes, interfaces and objects Declaring a data class: data class Person(var username: String, var age: Int) Data classes are a good way to represent some kind of data in your program with less boilerplate. By default, data classes implements equals(), hashCode() and toString() functions.

Slide 35

Slide 35 text

Classes, interfaces and objects Representing a Kotlin data class in Java code: public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public boolean equals(Object o) { if (this !== o) return true; if (o !== null !|| getClass() !!= o.getClass()) return false; Person person = (Person) o; return name.equals(person.name) !&& age.equals(person.age); } @Override public int hashCode() { return Objects.hash(name, age); } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

Slide 36

Slide 36 text

Classes, interfaces and objects Declaring an interface: interface PersonRepository { fun findAll(): List fun findBy(name: String): List }

Slide 37

Slide 37 text

Classes, interfaces and objects Implementing an interface: interface PersonRepository { fun findAll(): List fun findBy(name: String): List } class PersonRepositoryImpl : PersonRepository { override fun findAll(): List { TODO("Not yet implemented") } override fun findBy(name: String): List { TODO("Not yet implemented") } }

Slide 38

Slide 38 text

Classes, interfaces and objects Declaring a Kotlin object: object DateUtils { private val brazilianDateFormat = SimpleDateFormat("dd/MM/yyyy") private val unitedStatesDateFormat = SimpleDateFormat("mm-dd-yyyy") fun createDateInBrazilianFormat(value: String): Date { return brazilianDateFormat.parse(value) } fun createDateInUnitedStatesFormat(value: String): Date { return unitedStatesDateFormat.parse(value) } } fun main() { DateUtils.createDateInBrazilianFormat("16/01/2021") DateUtils.createDateInBrazilianFormat("01-16-2021") }

Slide 39

Slide 39 text

Java interoperability Calling Kotlin code from Java code: !// Kotlin code class PersonKotlin { var name: String = "" var age: Int = 0 } !// Java code public class MainJava { public static void main(String[] args) { PersonKotlin personKotlin = new PersonKotlin(); personKotlin.setName("Wellington"); personKotlin.setAge(25); System.out.println(personKotlin.getName()); System.out.println(personKotlin.getAge()); } }

Slide 40

Slide 40 text

Java interoperability Calling Java code from Kotlin code: !// Java code public class PersonJava { private String name; private Integer age; !// getters and settes } !// Kotlin Code fun main() { val personJava = PersonJava() personJava.name = "Wellington" personJava.age = 25 println(personJava.name) println(personJava.age) }

Slide 41

Slide 41 text

Asynchronous programming?

Slide 42

Slide 42 text

What’s next?

Slide 43

Slide 43 text

• Platform and its ecosystem What’s next?

Slide 44

Slide 44 text

• Platform and its ecosystem • Android components and lifecycles What’s next?

Slide 45

Slide 45 text

• Platform and its ecosystem • Android components and lifecycles • UI components (Activities, Fragments, Views) What’s next?

Slide 46

Slide 46 text

• Platform and its ecosystem • Android components and lifecycles • UI components (Activities, Fragments, Views) • App navigation What’s next?

Slide 47

Slide 47 text

• Platform and its ecosystem • Android components and lifecycles • UI components (Activities, Fragments, Views) • App navigation • Presentation layer patterns (MVP, MVVM, MVx) What’s next?

Slide 48

Slide 48 text

• Platform and its ecosystem • Android components and lifecycles • UI components (Activities, Fragments, Views) • App navigation • Presentation layer patterns (MVP, MVVM, MVx) • Architectural patterns (Clean Architecture) What’s next?

Slide 49

Slide 49 text

Questions?

Slide 50

Slide 50 text

• https://kotlinlang.org • https://developer.android.com/kotlin • https://kotlinlang.org/docs/reference References

Slide 51

Slide 51 text

Aegro’s intern hiring program

Slide 52

Slide 52 text

http://bit.ly/aegroestagio

Slide 53

Slide 53 text

Thank you! ;) /wellingtoncosta /wellingtoncosta128 [email protected]