0% found this document useful (0 votes)
2 views

Android Kotlin

The document provides an overview of Android development using Kotlin, highlighting its advantages such as being Google's preferred language, null safety, and interoperability with Java. It discusses the history of Android, the evolution of its architecture, and compares Kotlin with Swift, emphasizing their similarities. Additionally, it covers Kotlin's features like coroutines, data classes, and the development tools available for Android programming.

Uploaded by

Arbaz Pirwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Android Kotlin

The document provides an overview of Android development using Kotlin, highlighting its advantages such as being Google's preferred language, null safety, and interoperability with Java. It discusses the history of Android, the evolution of its architecture, and compares Kotlin with Swift, emphasizing their similarities. Additionally, it covers Kotlin's features like coroutines, data classes, and the development tools available for Android programming.

Uploaded by

Arbaz Pirwani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

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

● Coroutines are like very light-weight threads

● Asynchronous programming made easy

● No callbacks

● No complex functionality
History of Android
• Android, Inc. was founded in October 2003 by:
• Andy Rubin (co-founder of Danger)

• Rich Miner (co-founder of Wildfire Communications Inc.)

• Nick Sears (once VP at T-Mobile)

• Chris White (headed design and interface development at WebTV)

• To develop open source software platform for mobile devices that


allows new product out in market in timely fashion

• Initially developed by Android Inc., which Google bought in 2005


and was unveiled in 2007
Need of Mobile Platform

• Can be widely installed on all handheld devices


• Easy application development
• Applications can take full advantage of hardware
• Applications can be easily available to install for larger customer base
• Updates can be easily available to users
Introduction to Android

• Android™ delivers a complete set of software for mobile devices: an


operating system, middleware and key mobile applications.

• Android was built from the ground-up to enable developers to create


compelling mobile applications that take full advantage of all a handset
has to offer

• Based on Linux kernel


• Android has its own virtual machine (Dalvik, ART) that executes
applications written for Android
Introduction to Android

• Dalvik (Android 4.4 and earlier)


• Dalvik is virtual machine with trace-based just-in-time (JIT) compilation that
optimizing the execution of applications by continually profiling applications each
time they run and dynamically compiling frequently executed short segments of their
bytecode into native machine code

• ART (Android 5.0 and later)


• ART employs a new Ahead-Of-Time (AOT) concept contrary to Dalvik’s JIT, which
basically compiles apps before they are even launched. What this means is that
first-time installs will take longer, and apps will occupy more space on internal
storage, but at the same time, since the app will be fully compiled as soon as it’s
installed, launch times will be much faster.
Android MVVM Architecture
Apple Pie 1.0 Cupcake 1.5 Donut 1.6 Eclare 2.0/2.1 Froyo 2.2 Gingerbread 2.3.x

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

2.3.3 - Gingerbread 10 0.3%


2.3.7

4.0.3 - Ice Cream Sandwich 15 0.3%


4.0.4

4.1.x Jelly Bean 16 1.2%

4.2.x 17 1.5%

4.3 18 0.5%

4.4 KitKat 19 6.9%

5.0 Lollipop 21 3.0%

5.1 22 11.5%

6.0 Marshmallow 23 16.9%

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.0 Oreo 26 12.9%

8.1 27 15.4%

9 Pie 28 10.4%
Back To Kotlin
VS
Kotlin & Swift

● The syntax of Swift doesn’t just resemble that of Kotlin: in


small chunks of code there can be up to 77% string
similarity.
● At the very least, it should be noted that while Swift
appeared in 2013, Kotlin originated back in 2011. Hence,
even though comparing Kotlin to Swift (in this exact order)
can be convenient due to Swift’s earlier introduction to a
wide audience, any ‘copycat’ attitudes towards Kotlin
aren’t justified.
Kotlin & Swift

● Available on both mobile-client and back-end server

● Highly performant and statically-typed

● Kotlin also can be used for web front-end via Kotlin.JS


Swift is like Kotlin:
Hello World

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

• Advantages over Java 7 are overwhelming


• Java 8 introduced
• Lambdas
• Null safety (“Optional” class)
• Default methods
• Streams API
• New Date & Time API
But even with Java 8 Kotlin still...

• Is more elegant, concise and safe


• Has more cool stuff
Basic syntax and rules
More elegant?

• Functions - definition in package or in class


• Immutable/mutable variables
• No “new” keyword
• Type inference
• No checked exceptions
• No primitive types
• No static members
Basic syntax and rules cont’d
More elegant?

• Primary constructors
• No fields, just properties
• Bean style classes easy to declare
• By default, all classes are final
Null Safety
More elegant?

• Null reference – Billion dollar mistake


• Kotlin is designed in a way that aims to eliminate NPE from our code
Generics
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

• Default argument values can be defined


• Arguments with default values are optional
• No more need for function overloading (almost)
• Kotlin classes can have only one constructor
• Arguments can be called by name
• When passing arguments by name ordering doesn’t matter
Ranges

• Simpler “is it in range” check


• Can be used for any type that implements Comparable
• “both ends“ included
• “..” operator is translated to “rangeTo” function
• “rangeTo” function is implemented as extension function on
Comparable
• Numerical ranges can be iterated over
• In both directions and in arbitrary steps
Ranges
Pattern matching

• Java’s instaceof not very practical


• No !instanceof
• Meet Kotlin’s "is“
• "is" negation "!is“

• Automatic type cast when “is” evaluates true inside if / when


blocks
AND MANY MORE
Development Tool

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/

You might also like