2. Me, Myself and I
Blah, blah, blah…
@nicolas_frankel #kotlin #android
3. My experience in Android
Back-end Java developer
Developing a To Do list application with
some improvements
Images
Alarm
Multiple lists
@nicolas_frankel #kotlin #android
4. My personal view
Developing Android app is a pain
Backend Java is very mature compared to
Android
High-level libraries required to cope up with
low-level APIs
@nicolas_frankel #kotlin #android
6. Outline
Kotlin - the language
Libraries
Stdlib
Kotlin extensions for Android
Anko
@nicolas_frankel #kotlin #android
7. Kotlin
Language developed by JetBrains
Open Source
Compiles to
JVM bytecode
JavaScript (experimental)
A "simpler Scala"
@nicolas_frankel #kotlin #android
8. Kotlin
Functional and object-oriented
Statically typed
Null safety
No checked exceptions
Named & optional arguments
Lambdas
Extension functions
Java compatibility
(And more...)
@nicolas_frankel #kotlin #android
9. Hello world!
package hello // optional semicolons
// namespace-level functions
// types on the right
// no special syntax for arrays
// optional return type
fun main(args: Array<String>) {
println("Hello, world!")
}
@nicolas_frankel #kotlin #android
12. Sample code
public class AddTaskEvent extends AbstractTaskEvent {
private final List list;
public AddTaskEvent(Task task, List list) {
super(task);
this.list = list;
}
public List getList() {
return list;
}
} @nicolas_frankel #kotlin #android
17. Kotlin solution
fun show(a: Activity) {
val imm =
a.getSystemService(INPUT_METHOD_SERVICE)
as InputMethodManager
imm.toggleSoftInput(SHOW_FORCED, 0)
}
fun hide(a: Activity) {
val imm =
a.getSystemService(INPUT_METHOD_SERVICE)
as InputMethodManager
imm.toggleSoftInput(HIDE_IMPLICIT_ONLY, 0)
}
28. Kotlin solution
fun getFrom(c: Context): DisplayMetrics {
val wm =
c.getSystemService(Context.WINDOW_SERVICE)
as WindowManager
val metrics = DisplayMetrics()
wm.defaultDisplay.getMetrics(metrics)
return metrics
}
@nicolas_frankel #kotlin #android
29. Kotlin solution
fun getFrom(c: Context): DisplayMetrics {
val wm =
c.getSystemService(Context.WINDOW_SERVICE)
as WindowManager
return DisplayMetrics().apply {
wm.defaultDisplay.getMetrics(this)
}
}
@nicolas_frankel #kotlin #android
30. Kotlin extensions for Android
Plugin for the Kotlin compiler
Provide a "synthetic property" for each
widget in a layout
@nicolas_frankel #kotlin #android
39. Anko solution
import org.jetbrains.anko.*
class AddAlarmClickListener:
View.OnClickListener {
override fun onClick(view: View) {
val parent:View = view.rootView
val flip =
parent.find<ViewFlipper>(R.id.new_task_flip)
flip.displayedChild = 1
}
}
@nicolas_frankel #kotlin #android
40. Code sample
fun show(a: Activity) {
val imm =
a.getSystemService(INPUT_METHOD_SERVICE)
as InputMethodManager
imm.toggleSoftInput(SHOW_FORCED, 0)
}
fun hide(a: Activity) {
val imm =
a.getSystemService(INPUT_METHOD_SERVICE)
as InputMethodManager
imm.toggleSoftInput(HIDE_IMPLICIT_ONLY, 0)
}
47. Migration tactics
Start with standard classes
Then with Android-dependent classes
Use Android Studio provided migration tool
Take a class
Migrate to Kotlin extension
Migrate to Anko
After each single change, test!
Repeat
@nicolas_frankel #kotlin #android
49. Null-able vs. non null-able types
Different type whether value can be null or
not
T: cannot be null
T?: can be null
@nicolas_frankel #kotlin #android
50. Null-able vs. non null-able types
Parameter types should use the right kind
Know your API
Or read the docs
Or be conservative
@nicolas_frankel #kotlin #android