2. What is Kotlin?
...it’s just another language…
● Created by Jetbrains
● Statically typed
● Lightweight
● Running on JVM
3. Old fashion Android Coding
● Android supports only Java6(ish)
● The verbosity of Java is annoying (i.e. a lot of boilerplate code)
● The Billion Dollar Mistake (aka Null Reference)
4. Why Kotlin?
● Concise: Reduce the boilerplate code, no ceremonies, no decorations
● Safe: It’s a Null Safety language
● Versatile: Write Android, Front end and Server Side applications
● Interoperable: Write Kotlin and Java code in the same project
6. The Language
fun getAvg(a: Int, b: Int): Int {
return (a + b) / 2
}
fun getAvg(a: Int, b: Int) = (a + b) / 2
val number: Int = 0
val stringArray = ArrayList<String>()
var average: Float? = null
7. Extension Functions
Simply way to extend a class adding new functions
fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, duration).show()
}
…
toast("Hello Folks!")
8. 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
}
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
val tmp = this[index1] // 'this' corresponds to the list
this[index1] = this[index2]
this[index2] = tmp
}
10. Lambda Expression Syntax
val sum : (Int, Int) -> Int = {x: Int, y: Int -> x + y}
val sum = { x: Int, y: Int -> x + y }
val notEmpty: (String) -> Boolean = { !it.isEmpty() }
val notEmpty = { it: String -> !it.isEmpty() }
11. Higher-Order Functions and Lambdas
A function that takes functions as parameters, or returns a function
fun getId(id: Int, func: (Int) -> String): String {
return func(id)
}
val hereMyID = getId(101478, { x -> "Here My Id: $x" })
12. Lazy Initialization
var mySdk : Sdk? = null
mySdk = Sdk()
Sdk mySdk = null;
mySdk = new Sdk();
Java Kotlin
lateinit var mySdk : Sdk
mySdk = Sdk()
13. Lazy Initialization
Kotlin
class JavaClass {
Sdk mySdk;
final Sdk getSdk() {
if (mySdk == null) {
mySdk = new Sdk();
}
return mySdk;
}
}
val mySdk : Sdk by lazy { getSdk() }
protected fun getSdk() = Sdk()
Java
val mySdk : Sdk by lazy { Sdk() }
14. Kotlin Android Extensions Library
Java:
recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
Kotlin:
recyclerView = findViewById(R.id.recycler_view) as RecyclerView
Kotter Knife:
val recyclerView by bindView<RecyclerView>(R.id.recycler_view)
Extensions Library: just import
import kotlinx.android.synthetic.main.my_layout.*
15. Android Extensions Library
Pros:
● Compile time check
● No casts
● Less error prone.
● No libraries in your build.gradle (less methods!) (Butterknife is 342)
● Can import Views from different flavors
16. Anko
Anko is a DSL (Domain-specific language) written in Kotlin to simplify the
programmatically creation of UI.
Much more:
● Intents
● Fragments
● Services
● Configuration qualifiers
● Dialogs and toasts
● Asynchronous tasks
● Logging
● Using SQLite
● Extending Anko
17. val intent = Intent(this, javaClass<SomeActivity>())
intent.putExtra("id", 5)
intent.putExtra("name", "John")
startActivity(intent)
startActivity(Intent(this, SomeActivity::class.java).
putExtra("id", 5).putExtra("name", "John"))
startActivity<SomeActivity>("id" to 5, "name" to "John")
Anko - Intents
Kotlin i.e. 1
Kotlin i.e. 2
Kotlin with Anko
18. async {
// Long background task
uiThread {
result.text = "Done"
}
}
val executor = Executors.newScheduledThreadPool(4)
async(executor) {
// Some task
}
Anko - AsyncTask
19. class SomeActivity : Activity(), AnkoLogger {
private fun someMethod() {
info("London is the capital of Great Britain")
debug(5) // .toString() method will be executed
warn(null) // "null" will be printed
wtf(“Oh, No!”)
}
}
Anko - Logger