Keeping your build tool updated in a multi repository world
Android 101 - Kotlin ( Future of Android Development)
1. Hassan Abid
GDE Android - Singapore
@hassanabidpk
Android 101
Kotlin - Future of Android Development
2. ● Mainly work as Android developer
● Google Developers Expert for Android
● Contributed in apps and SDKs: KineMaster, Rakuten LIVE, BeLive, NexPlayer SDK
(Android, iOS, Unity, Tizen)
About me
Lead Software Engineer
4. ● Started learning on my own in 2011 from developers.android.com
● Joined Android Study meetup with Geeks in 2011
● Tried to contribute in a very difficult project in 2011
● Got first internship for developing Tablet UI in 2012 (Life changing move)
● Mentored and taught my friends about Android dev in 2015 (Udacity course)
● Finished Android Nanodegree in dec 2015
● Became Android GDE In 2016
My Android Journey
5.
6.
7. “10 years and now over 2.5 billion active
devices produced by more than 180
hardware manufacturers”
- Android director Stephanie Cuthbertson
8. ● Android Studio 3.5 released
● Kotlin is Google’s preferred language of Android
● Official documentation : https://developer.android.com
● Official Samples : https://developer.android.com/samples
Android
Latest version of Android (10) was released last week
15. ● Kotlin is a modern statically typed programming language
● It’s pragmatic and concise, and makes coding a satisfying and
efficient experience.
● Kotlin is 100% interoperable with Java
Kotlin 101
Developed by JetBrain
21. // Nullable types and Non-Null Types
// Case 1
var a: String = "abc"
a = null // compilation error
// Case 2
var b: String? = "abc"
b = null // ok
print(b)
22. // Checking for null in conditions
val b: String? = "Kotlin"
if (b != null && b.length > 0) {
print("String of length ${b.length}")
} else {
print("Empty string")
}
23. // Safe Calls
val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length) // Unnecessary safe call
24. // Safe Calls
val a = "Kotlin"
val b: String? = null
println(b?.length)
println(a?.length) // Unnecessary safe call
// The !! Operator
val l = b!!.length
25. // Extension Functions
fun MutableList<Int>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}
// Usage
val list = mutableListOf(1, 2, 3)
list.swap(0, 2) // 'this' inside 'swap()' will hold the value of 'list'
26. // Data Classes
data class User(val name: String = "", val age: Int = 0)
data class Person(val name: String) {
var age: Int = 0
}
val jane = User("Jane", 35)
val (name, age) = jane
println("$name, $age years of age") // prints "Jane, 35 years of age"
27. // Smart Casts
fun demo(x: Any) {
if (x is String) {
print(x.length) // x is automatically cast to String
}
}
// Smart cast for when expression
when (x) {
is Int -> print(x + 1)
is String -> print(x.length + 1)
is IntArray -> print(x.sum())
}
28. ● Read more at https://kotlinlang.org/docs/reference/coroutines-overview.html
● Code Lab : https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#0
Coroutines
Lightweight threads
29. // Async callbacks
networkRequest { result ->
// Successful network request
databaseSave(result) { rows ->
// Result saved
}
}
// The same code with coroutines
val result = networkRequest()
// Successful network request
databaseSave(result)
// Result saved
30. // Android Kotlin Extensions - KTX
// Before KTX
sharedPreferences
.edit() // create an Editor
.putBoolean("key", value)
.apply() // write to disk asynchronously
// After KTX
// Commit a new value asynchronously
sharedPreferences.edit { putBoolean("key", value) }
31.
32. MORE HERE : https://play.kotlinlang.org/hands-on/overview