This document summarizes a presentation about Kotlin features for Android development. It discusses Kotlin's concise syntax, null safety, lambdas, extension functions, and higher-order functions. It also covers Kotlin collections, coroutines, Anko for layouts and SQLite, and migrating existing Android projects to Kotlin. The presentation provides code examples for many of these features and recommends additional Kotlin learning resources.
7. Example : Concise
public class Event { //Java
private long id;
private String name;
private String url;
public long getId() {...}
public String getName() {...}
public String getUrl() {...}
public void setId(long id) {...}
public void setName(String name) {...}
public void setUrl(String url) {...}
@override public String toString(){...}
}
//Kotlin
data class Event(
var id: Long,
var name: String,
var url: String)
#Kotlin #Android
15. Example : Extension Functions
//Normal Kotlin Function
fun triple(): Int {
return this * 3
}
//Extension Kotlin Function
fun Int.triple(): Int {
return this * 3
}
//Using Extension Function
var result = 3.triple()
#Kotlin #Android
16. Example : Extension Functions
//Extension Function for Glide
fun ImageView.loadImage(url: String) {
Glide.with(context).load(url).into(this)
}
//Usage
imageView.loadImage(url)
#Kotlin #Android
24. Coroutines
● Light-weight threads
● Stackless coroutine - No mapping on main thread
● No context switching on processor
● Multitasking and managed by user
#Kotlin #Android
25. Definition : Coroutines
One can think of a coroutine as a light-weight thread. Like threads,
coroutines can run in parallel, wait for each other and communicate.
The biggest difference is that coroutines are very cheap, almost free:
we can create thousands of them, and pay very little in terms of
performance. True threads, on the other hand, are expensive to start
and keep around. A thousand threads can be a serious challenge for a
modern machine.
- kotlinlang.org
#Kotlin #Android
26. Example : Launch
//Coroutine Launch
fun main(args: Array<String>) {
println("Kotlin Start")
launch(CommonPool) {
delay(2000)
println("Kotlin Inside")
}
println("Kotlin End")
}
// The output will be
// Kotlin Start
// Kotlin End
// Kotlin Inside
#Kotlin #Android
27. Example : Async
// Parallel execution
private fun doWorksInParallel() {
val one = async(CommonPool) {
doWorkFor1Seconds() }
val two = async(CommonPool) {
doWorkFor2Seconds()}
launch(CommonPool) {
val combined = one.await() + "_" + two.await()
println("Kotlin Combined : " + combined)
}
}
suspend fun doWorkFor1Seconds(): String {
delay(1000)
return “doWorkFor1Seconds” }
suspend fun doWorkFor2Seconds(): String {
delay(2000)
return “doWorkFor2Seconds” }
#Kotlin #Android
// The output is
// Kotlin Combined :
//doWorkFor1Seconds_doWorkFor2Seconds
29. Anko
● Commons: a lightweight library full of helpers for intents, dialogs, logging etc.
● Layouts: a fast and type-safe way to write dynamic Android layouts
● SQLite: a query DSL and parser collection for Android SQLite
● Coroutines: utilities based on the kotlinx.coroutines library
#Kotlin #Android
30. Anko Commons
● Intents
● Dialogs and toasts
● Logging
● Resources and dimensions
#Kotlin #Android
31. Example : Commons
//Calling Intent using Kotlin
val intent = Intent(this,
SomeOtherActivity::class.java)
intent.putExtra("id", 5)
intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_
TOP)
startActivity(intent)
//Calling Intent using Anko Commons
startActivity(intentFor<SomeOtherActivity>("id"
to 5).singleTop())
//Calling Intent using Anko Commons
startActivity(intentFor<SomeOtherActivity>("id"
to 5,"name" to "Hari"))
#Kotlin #Android
32. Example : Layouts
//using Anko to create layouts
verticalLayout {
val name = editText()
button("Say Hello") {
onClick { toast("Hello, ${name.text}!") }
}
}
//Output
#Kotlin #Android
33. Example : SQLite
//using Anko to create table
database.use {
createTable("User", true,
"id" to INTEGER + PRIMARY_KEY,
"name" to TEXT,
"photo" to BLOB)
}
//using Anko to query
db.select("User", "name")
.whereArgs("(_id > {userId}) and (name =
{userName})",
"userName" to "John",
"userId" to 42)
#Kotlin #Android
34. Example : Coroutines
//using Anko to create async task
fun authorizeUser(username: String, password: String) {
doAsync {
val authorized = signInBL.checkUserCredentials(
AuthCredentials(username = username, password = password))
activityUiThread {
if (authorized) {
toast("Signed!!!")
} else {
view.showAccessDeniedAlertDialog()
}
}
}
#Kotlin #Android
40. Content Credits
● Mindorks - http://blog.mindorks.com/
● Kotlin Official - https://kotlinlang.org
● Word Cloud - https://worditout.com/
● Kotlin for Android Developers - https://antonioleiva.com
#Kotlin #Android
Hinweis der Redaktion
Also, supports native LLVM. Kotlin is a new programming language from JetBrains, the maker of the world’s best IDEs.
Drastically reduce the amount of boilerplate code you need to write.
Also get hashCode and equals for free
Use Elvis operator to give an alternative in case the object is null
2. Thus, if you want an NPE, you can have it, but you have to ask for it explicitly, and it does not appear out of the blue.
Kotlin is basically an object oriented language, not a pure functional language. However, as many other modern languages, it uses many concepts from functional programming, such as lambda expressions, to solve some problems in a much easier way.
A higher-order function is a function that takes functions as parameters, or returns a function
Coroutines are based on the idea of suspending functions: functions that can stop the execution when they are called and make it continue once it has finished running their background task.
Suspending functions are marked with the reserved word suspend, and can only be called inside other suspending functions or inside a coroutine.
Coroutines are a new way of writing asynchronous, non-blocking code (and much more)
Also get hashCode and equals for free
This starts a new coroutine on a given thread pool. In this case, we are using CommonPool that uses ForkJoinPool.commonPool(). Threads still exist in a program based on coroutines, but one thread can run many coroutines, so there’s no need for too many threads.
If you directly do this in the main function, it says this with error:
Suspend functions are only allowed to be called from a coroutine or another suspend function.
Here, the delay function is suspend function, so we can only call it from a coroutine or an another suspend function.
Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.
Talk about Rx Kotlin
Talk about interoperability
Talk about Kotlin plugin
Convincing Management
How to start learning? Start with test case
Method count
Code converter
Build speed
Not purely functional, internal conversion is more
Learning curve
Smaller community and less experts