Android Kotlin
Android Kotlin
#PakAcademy
@ArbazPirwani
Android Development In Kotlin
Arbaz Pirwani
@ArbazPirwani
WHY KOTLIN?
- Google Preferred Language
- Statistically typed
- Type Inference
- Developed by Jetbrains
- Concise
- 100% interoperable with Java
- Null Safety
- And many more.
Java vs Kotlin
Java to Kotlin
Server
Kotlin/JVM
Android
Kotlin/JVM
iOS Browser
Kotlin/Native Kotlin/JS
● Data Classes ● Type Inference
● Extension Methods ● DSL Support
● Elvis Operator ● Type-Safe Builders
● Properties ● Coroutines
Default Parameters String Interpolation
Coroutines
● ●
● Type Aliases ● Ranges
● Named Parameters ● Map Array Syntax
● Spread Operator ● Inline Functions
● Auto Casts ● Sealed Classes
● Covariance ● Delegation
● Null Safety ● Destructuring
● Operator Overloading ● KDoc
Coroutines
● No callbacks
● No complex functionality
History of Android
• Android, Inc. was founded in October 2003 by:
• Andy Rubin (co-founder of Danger)
Honeycomb 3.x Ice Cream Sandwich 4.0.x Jelly Bean 4.1/4.2/4.3 KitKat 4.4
Lollipop 5.0 Marshmallow 6.0 Nougat 7.0 Oreo 8.0 Pie 9.0
Version Codename API Distribution
4.2.x 17 1.5%
4.3 18 0.5%
5.1 22 11.5%
7.0 Nougat 24 11.4% Data collected during a 7-day period ending on May 7, 2019.
7.1 25 7.8% Any versions with less than 0.1% distribution are not shown.
8.1 27 15.4%
9 Pie 28 10.4%
Back To Kotlin
VS
Kotlin & Swift
Swift
print("Hello, world!")
Kotlin
println("Hello, world!")
Source: http://nilhcem.com/swift-is-like-kotlin/
Swift is like Kotlin:
Variables And Constants
Swift
var myVariable = 42
myVariable = 50
let myConstant = 42
Kotlin
var myVariable = 42
myVariable = 50
val myConstant = 42
Source: http://nilhcem.com/swift-is-like-kotlin/
Swift is like Kotlin:
Explicit Types
Swift
let explicitDouble: Double = 42
Kotlin
val explicitDouble: Double = 42.0
Source: http://nilhcem.com/swift-is-like-kotlin/
Swift is like Kotlin:
Arrays
Swift
var shoppingList = ["catfish", "water","tulips", "blue paint"]
shoppingList[1] = "bottle of water"
Kotlin
val shoppingList = arrayOf("catfish", "water","tulips", "blue paint")
shoppingList[1] = "bottle of water"
Source: http://nilhcem.com/swift-is-like-kotlin/
Swift is like Kotlin:
Functions
Swift
func greet(_ name: String,_ day: String) -> String {
return "Hello \(name), today is \(day)."
}
greet("Bob", "Tuesday")
Kotlin
fun greet(name: String, day: String): String {
return "Hello $name, today is $day."
}
greet("Bob", "Tuesday")
Source: http://nilhcem.com/swift-is-like-kotlin/
Swift is like Kotlin:
Classes
Swift
class Shape {
var numberOfSides = 0
func simpleDescription() -> String {
return "A shape with \(numberOfSides) sides."
}
}
Kotlin
class Shape {
var numberOfSides = 0
fun simpleDescription() = "A shape with $numberOfSides sides."
}
Source: http://nilhcem.com/swift-is-like-kotlin/
Java strikes back with Java 8
• Primary constructors
• No fields, just properties
• Bean style classes easy to declare
• By default, all classes are final
Null Safety
More elegant?
• Declaration-site variance
• out / in generics modifiers
• Supports use-site variance also
• No wildcard syntax, out / in used
MORE COOL STUFF
Default arguments and
named arguments
https://developer.android.com/studio/#downloads
Android Studio
• Instant Run
• Intelligent code editor
• Fast and feature-rich emulator
• Code templates and GitHub integration
• Multi-screen app development
• Virtual devices for all shapes and sizes
• Android builds evolved, with Gradle
• Performance profilers
https://developer.android.com/studio/#downloads
Resources
• https://www.android.com/
• https://developer.android.com/
• https://developer.android.com/guide/index.html
• https://developer.android.com/studio/#downloads
• https://kotlinlang.org/
• https://play.kotlinlang.org/
• http://nilhcem.com/swift-is-like-kotlin/