SlideShare a Scribd company logo
POWER-UP YOUR
ANDROID-FU
WITH KOTLIN
@NICOLAS_FRANKEL
ME, MYSELF AND I
Blah, blah, blah…
@nicolas_frankel - Improve your Android-Fu with Kotlin
2
MY EXPERIENCE IN ANDROID
Back-end Java developer
Developing a To Do list
application with some
improvements
• Images
• Alarm
• Multiple lists
@nicolas_frankel - Improve your Android-Fu with Kotlin
3
MY PERSONAL CONCLUSION
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 - Improve your Android-Fu with Kotlin
4
ORIGINAL STACK
Dagger 2
Butterknife
Retro-lambda
Streams
Green Robot’s EventBus
Picasso
@nicolas_frankel - Improve your Android-Fu with Kotlin
5
OUTLINE
Kotlin - the language
Libraries
• Stdlib
• Kotlin extensions for Android
• Anko
@nicolas_frankel - Improve your Android-Fu with Kotlin
6
KOTLIN
Language developed by
JetBrains
Open Source
Compiles to
• JVM bytecode
• JavaScript (experimental)
A "simpler Scala"
@nicolas_frankel - Improve your Android-Fu with Kotlin
7
KOTLIN
Functional and object-oriented
Statically typed
Null safety
No checked exceptions
Named & optional arguments
Lambdas
Extension functions
Java compatibility
(And more...)
@nicolas_frankel - Improve your Android-Fu with Kotlin
8
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 - Improve your Android-Fu with Kotlin
9
SETUP - BUILD.GRADLE
buildscript {
ext.kotlin_version = '1.0.0-beta-1038'
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-
plugin:$kotlin_version"
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
10
SETUP – APP/BUILD.GRADLE
apply plugin: 'kotlin-android'
android {
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
test.java.srcDirs += 'src/test/kotlin'
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
11
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 - Improve your Android-Fu with Kotlin
12
PAIN POINTS
Verbose!
• Not specific to Android
• Unfortunately Java
@nicolas_frankel - Improve your Android-Fu with Kotlin
13
KOTLIN SOLUTION
class AddTaskEvent(task: Task, val list: List)
: AbstractTaskEvent(task)
@nicolas_frankel - Improve your Android-Fu with Kotlin
14
SAMPLE CODE
public class KeyboardDisplay {
public void show(Activity a) {
InputMethodManager imm =
(InputMethodManager) a.getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(SHOW_FORCED, 0);
}
public void hide(Activity a) {
InputMethodManager imm =
(InputMethodManager) a.getSystemService(INPUT_METHOD_SERVICE);
imm.toggleSoftInput(HIDE_IMPLICIT_ONLY, 0);
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
15
PAIN POINTS
Artificial grouping
Utility class
• No real Object, nor state
@nicolas_frankel - Improve your Android-Fu with Kotlin
16
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)
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
17
SAMPLE CODE
SQLiteDatabase db = getReadableDatabase();
db.query(TASK_TABLE,
new String[] { T_ID_COL, T_NAME_COL },
null, null, null, null,
T_PRIO_COL);
@nicolas_frankel - Improve your Android-Fu with Kotlin
18
PAIN POINTS
@nicolas_frankel - Improve your Android-Fu with Kotlin
19
Really, pass all those null
values?
Create local variable for
ease of use
KOTLIN SOLUTION – PART 1
fun SQLiteDatabase.query(table:String,
columns:Array<String>,
selection:String? = null,
selectionArgs:Array<String>? = null,
groupBy:String? = null,
having:String? = null,
orderBy:String? = null): Cursor {
return query(table, columns, selection,
selectionArgs, groupBy, having, orderBy)
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
20
KOTLIN SOLUTION – PART 2
readableDatabase.query(TASK_TABLE,
arrayOf(T_ID_COL, T_NAME_COL),
orderBy = T_PRIO_COL)
@nicolas_frankel - Improve your Android-Fu with Kotlin
21
KOTLIN LIBRARY
Standard API for the
language
@nicolas_frankel - Improve your Android-Fu with Kotlin
22
SETUP – APP/BUILD.GRADLE
dependencies {
compile "org.jetbrains.kotlin:kotlin-
stdlib:$kotlin_version"
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
23
SAMPLE CODE
public class DisplayMetricsGetter {
public DisplayMetrics getFrom(Context c) {
WindowManager wm = (WindowManager)
c.getSystemService(WINDOW_SERVICE);
DisplayMetrics metrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(metrics);
return metrics;
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
24
PAIN POINTS
Utility class
Cannot return the object
directly
• Have to create a local variable
@nicolas_frankel - Improve your Android-Fu with Kotlin
25
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 - Improve your Android-Fu with Kotlin
26
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 - Improve your Android-Fu with Kotlin
27
KOTLIN EXTENSIONS FOR ANDROID
Plugin for the Kotlin
compiler
Provide a "synthetic
property" for each widget in a
layout
@nicolas_frankel - Improve your Android-Fu with Kotlin
28
SETUP – APP/BUILD.GRADLE
buildscript {
repositories {
jcenter()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-
android-extensions:$kotlin_version"
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
29
SAMPLE CODE
public class AddActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_add_item);
View imageView = findViewById(R.id.new_task_img);
imageView.setOnClickListener(...);
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
30
SAMPLE CODE – WITH BUTTERKNIFE
public class AddActivity extends AppCompatActivity {
@Bind(R.id.new_task_img)
View imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.task_add_item);
ButterKnife.bind(this);
imageView.setOnClickListener(...);
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
31
PAIN POINTS
Do I have to detail???
@nicolas_frankel - Improve your Android-Fu with Kotlin
32
KOTLINX SOLUTION
import
kotlinx.android.synthetic.task_add_item.new_task_img
class AddTaskActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.task_add_item)
new_task_img.setOnClickListener(...)
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
33
ANKO
Kotlin library for Android
Mostly about replacing XML
with fluent DSL for GUI
But a lot of other niceties
@nicolas_frankel - Improve your Android-Fu with Kotlin
34
CODE SAMPLE
public class AddAlarmClickListener implements
View.OnClickListener {
@Override
public void onClick(View view) {
View rootView = view.getRootView();
ViewFlipper flip = (ViewFlipper)
rootView.findViewById(R.id.new_task_alarm_flip);
flip.setDisplayedChild(1);
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
35
PAIN POINTS
Cast
Setter (?)
@nicolas_frankel - Improve your Android-Fu with Kotlin
36
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_alarm_flip)
flip.displayedChild = 1
}
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
37
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)
}
@nicolas_frankel - Improve your Android-Fu with Kotlin
38
PAIN POINTS
Map-based API
Cast
@nicolas_frankel - Improve your Android-Fu with Kotlin
39
ANKO SOLUTION
a.inputMethodManager.toggleSoftInput(
SHOW_FORCED, 0)
a.inputMethodManager.toggleSoftInput(
HIDE_IMPLICIT_ONLY, 0)
@nicolas_frankel - Improve your Android-Fu with Kotlin
40
CODE SAMPLE
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage("New list name");
EditText editText = new EditText(activity);
editText.setId(
android.support.v7.appcompat.R.id.select_dialog_listview);
String listName = global.getList().getName();
editText.setText(listName, NORMAL);
editText.selectAll();
builder.setView(editText);
builder.setPositiveButton("OK", editListNameListener);
builder.setNegativeButton("Cancel", (dialog, which) -> {
dialog.cancel();
});
AlertDialog dialog = builder.create();
dialog.show();
@nicolas_frankel - Improve your Android-Fu with Kotlin
41
PAIN POINTS
Verbose +++
Not structured
@nicolas_frankel - Improve your Android-Fu with Kotlin
42
ANKO SOLUTION
view.context.alert('New list name') {
builder.setPositiveButton('OK', editListNameListener)
negativeButton('Cancel') {
cancel()
}
customView {
editText(global.list.name) {
id = an.sup.v7.appcompat.R.id.select_dialog_listview
}.selectAll()
}
}.show()
@nicolas_frankel - Improve your Android-Fu with Kotlin
43
FINAL STACK
Dagger 2
Butterknife  Kotlin ext.
Retro-lambda  Kotlin
Streams  Kotlin
Green Robot’s EventBus
Picasso
@nicolas_frankel - Improve your Android-Fu with Kotlin
44
MIGRATION TACTICS
@nicolas_frankel - Improve your Android-Fu with Kotlin
45
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
PITFALL
@nicolas_frankel - Improve your Android-Fu with Kotlin
46
Null-able types
NULL-ABLE VS. NON NULL-ABLE TYPES
@nicolas_frankel - Improve your Android-Fu with Kotlin
47
Different type whether
value can be null or not
• T: cannot be null
• T?: can be null
NULL-ABLE VS. NON NULL-ABLE TYPES
@nicolas_frankel - Improve your Android-Fu with Kotlin
48
Parameter types should use
the right kind
• Know your API
• Or read the docs
• Or be conservative
MY EXPERIENCE
@nicolas_frankel - Improve your Android-Fu with Kotlin
49
More expressive
Higher-level abstractions
One single class I couldn’t
migrate
• Dagger 2 module
Q&A
@nicolas_frankel - Improve your Android-Fu with Kotlin
50
http://blog.frankel.ch/
@nicolas_frankel
http://frankel.in/

More Related Content

Similar to Improve your Android-Fu with Kotlin

You Don't Need Dependency Injection
You Don't Need Dependency InjectionYou Don't Need Dependency Injection
You Don't Need Dependency Injection
intive
 
Diving into VS 2015 Day4
Diving into VS 2015 Day4Diving into VS 2015 Day4
Diving into VS 2015 Day4
Akhil Mittal
 
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveWriting Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
André Oriani
 
Kotlin workshop
Kotlin workshopKotlin workshop
Kotlin workshop
Jedsada Tiwongvokul
 
Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++
Daniele Pallastrelli
 
Survey_Paper
Survey_PaperSurvey_Paper
Survey_Paper
Abhra Koley
 
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Hammad Tariq
 
Compose camp 2.pptx
Compose camp 2.pptxCompose camp 2.pptx
Compose camp 2.pptx
bcedsc
 
pluginandplay-UtrechtJUG.pptx
pluginandplay-UtrechtJUG.pptxpluginandplay-UtrechtJUG.pptx
pluginandplay-UtrechtJUG.pptx
SimonedeGijt
 
Kotlin で android アプリを作ってみた
Kotlin で android アプリを作ってみたKotlin で android アプリを作ってみた
Kotlin で android アプリを作ってみた
bina1204 Hozuki
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
Kurt Renzo Acosta
 
Android Study Jam 1 Day 1 | December 2021 | GDSC BVCOENM
Android Study Jam 1 Day 1 | December 2021 | GDSC BVCOENMAndroid Study Jam 1 Day 1 | December 2021 | GDSC BVCOENM
Android Study Jam 1 Day 1 | December 2021 | GDSC BVCOENM
GDSCBVCOENM
 
PluginAndPlay-AmsterdamJUG.pptx
PluginAndPlay-AmsterdamJUG.pptxPluginAndPlay-AmsterdamJUG.pptx
PluginAndPlay-AmsterdamJUG.pptx
SimonedeGijt
 
Google DSC Android Study Jams Session 1
Google DSC Android Study Jams Session 1Google DSC Android Study Jams Session 1
Google DSC Android Study Jams Session 1
BeauWilliams7
 
從零開始學 Android
從零開始學 Android從零開始學 Android
從零開始學 Android
秀吉(Hsiu-Chi) 蔡(Tsai)
 
Android things introduction - Development for IoT
Android things introduction - Development for IoTAndroid things introduction - Development for IoT
Android things introduction - Development for IoT
Bartosz Kosarzycki
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
intelliyole
 
Android Wear CodeLab - GDG Firenze
Android Wear CodeLab - GDG FirenzeAndroid Wear CodeLab - GDG Firenze
Android Wear CodeLab - GDG Firenze
Fabio Collini
 
Cheat-sheet_Koin_2023.pdf
Cheat-sheet_Koin_2023.pdfCheat-sheet_Koin_2023.pdf
Cheat-sheet_Koin_2023.pdf
AkbarHamaminatu1
 
Kotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDev
Kotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDevKotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDev
Kotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDev
DroidConTLV
 

Similar to Improve your Android-Fu with Kotlin (20)

You Don't Need Dependency Injection
You Don't Need Dependency InjectionYou Don't Need Dependency Injection
You Don't Need Dependency Injection
 
Diving into VS 2015 Day4
Diving into VS 2015 Day4Diving into VS 2015 Day4
Diving into VS 2015 Day4
 
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna loveWriting Kotlin Multiplatform libraries that your iOS teammates are gonna love
Writing Kotlin Multiplatform libraries that your iOS teammates are gonna love
 
Kotlin workshop
Kotlin workshopKotlin workshop
Kotlin workshop
 
Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++Going native with less coupling: Dependency Injection in C++
Going native with less coupling: Dependency Injection in C++
 
Survey_Paper
Survey_PaperSurvey_Paper
Survey_Paper
 
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...Building  android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
Building android apps with MVP, Dagger, Retrofit, Gson, JSON, Kotlin Data Cl...
 
Compose camp 2.pptx
Compose camp 2.pptxCompose camp 2.pptx
Compose camp 2.pptx
 
pluginandplay-UtrechtJUG.pptx
pluginandplay-UtrechtJUG.pptxpluginandplay-UtrechtJUG.pptx
pluginandplay-UtrechtJUG.pptx
 
Kotlin で android アプリを作ってみた
Kotlin で android アプリを作ってみたKotlin で android アプリを作ってみた
Kotlin で android アプリを作ってみた
 
Building Mobile Apps with Android
Building Mobile Apps with AndroidBuilding Mobile Apps with Android
Building Mobile Apps with Android
 
Android Study Jam 1 Day 1 | December 2021 | GDSC BVCOENM
Android Study Jam 1 Day 1 | December 2021 | GDSC BVCOENMAndroid Study Jam 1 Day 1 | December 2021 | GDSC BVCOENM
Android Study Jam 1 Day 1 | December 2021 | GDSC BVCOENM
 
PluginAndPlay-AmsterdamJUG.pptx
PluginAndPlay-AmsterdamJUG.pptxPluginAndPlay-AmsterdamJUG.pptx
PluginAndPlay-AmsterdamJUG.pptx
 
Google DSC Android Study Jams Session 1
Google DSC Android Study Jams Session 1Google DSC Android Study Jams Session 1
Google DSC Android Study Jams Session 1
 
從零開始學 Android
從零開始學 Android從零開始學 Android
從零開始學 Android
 
Android things introduction - Development for IoT
Android things introduction - Development for IoTAndroid things introduction - Development for IoT
Android things introduction - Development for IoT
 
Kotlin: Why Do You Care?
Kotlin: Why Do You Care?Kotlin: Why Do You Care?
Kotlin: Why Do You Care?
 
Android Wear CodeLab - GDG Firenze
Android Wear CodeLab - GDG FirenzeAndroid Wear CodeLab - GDG Firenze
Android Wear CodeLab - GDG Firenze
 
Cheat-sheet_Koin_2023.pdf
Cheat-sheet_Koin_2023.pdfCheat-sheet_Koin_2023.pdf
Cheat-sheet_Koin_2023.pdf
 
Kotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDev
Kotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDevKotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDev
Kotlin Multiplatform in Action - Alexandr Pogrebnyak - IceRockDev
 

More from Nicolas Fränkel

SnowCamp - Adding search to a legacy application
SnowCamp - Adding search to a legacy applicationSnowCamp - Adding search to a legacy application
SnowCamp - Adding search to a legacy application
Nicolas Fränkel
 
Un CV de dévelopeur toujours a jour
Un CV de dévelopeur toujours a jourUn CV de dévelopeur toujours a jour
Un CV de dévelopeur toujours a jour
Nicolas Fränkel
 
Zero-downtime deployment on Kubernetes with Hazelcast
Zero-downtime deployment on Kubernetes with HazelcastZero-downtime deployment on Kubernetes with Hazelcast
Zero-downtime deployment on Kubernetes with Hazelcast
Nicolas Fränkel
 
jLove - A Change-Data-Capture use-case: designing an evergreen cache
jLove - A Change-Data-Capture use-case: designing an evergreen cachejLove - A Change-Data-Capture use-case: designing an evergreen cache
jLove - A Change-Data-Capture use-case: designing an evergreen cache
Nicolas Fränkel
 
BigData conference - Introduction to stream processing
BigData conference - Introduction to stream processingBigData conference - Introduction to stream processing
BigData conference - Introduction to stream processing
Nicolas Fränkel
 
ADDO - Your own Kubernetes controller, not only in Go
ADDO - Your own Kubernetes controller, not only in GoADDO - Your own Kubernetes controller, not only in Go
ADDO - Your own Kubernetes controller, not only in Go
Nicolas Fränkel
 
TestCon Europe - Mutation Testing to the Rescue of Your Tests
TestCon Europe - Mutation Testing to the Rescue of Your TestsTestCon Europe - Mutation Testing to the Rescue of Your Tests
TestCon Europe - Mutation Testing to the Rescue of Your Tests
Nicolas Fränkel
 
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java applicationOSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
Nicolas Fränkel
 
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cacheGeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
Nicolas Fränkel
 
JavaDay Istanbul - 3 improvements in your microservices architecture
JavaDay Istanbul - 3 improvements in your microservices architectureJavaDay Istanbul - 3 improvements in your microservices architecture
JavaDay Istanbul - 3 improvements in your microservices architecture
Nicolas Fränkel
 
OSCONF Hyderabad - Shorten all URLs!
OSCONF Hyderabad - Shorten all URLs!OSCONF Hyderabad - Shorten all URLs!
OSCONF Hyderabad - Shorten all URLs!
Nicolas Fränkel
 
Devclub.lv - Introduction to stream processing
Devclub.lv - Introduction to stream processingDevclub.lv - Introduction to stream processing
Devclub.lv - Introduction to stream processing
Nicolas Fränkel
 
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring BootOSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
Nicolas Fränkel
 
JOnConf - A CDC use-case: designing an Evergreen Cache
JOnConf - A CDC use-case: designing an Evergreen CacheJOnConf - A CDC use-case: designing an Evergreen Cache
JOnConf - A CDC use-case: designing an Evergreen Cache
Nicolas Fränkel
 
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
Nicolas Fränkel
 
JUG Tirana - Introduction to data streaming
JUG Tirana - Introduction to data streamingJUG Tirana - Introduction to data streaming
JUG Tirana - Introduction to data streaming
Nicolas Fränkel
 
Java.IL - Your own Kubernetes controller, not only in Go!
Java.IL - Your own Kubernetes controller, not only in Go!Java.IL - Your own Kubernetes controller, not only in Go!
Java.IL - Your own Kubernetes controller, not only in Go!
Nicolas Fränkel
 
vJUG - Introduction to data streaming
vJUG - Introduction to data streamingvJUG - Introduction to data streaming
vJUG - Introduction to data streaming
Nicolas Fränkel
 
London Java Community - An Experiment in Continuous Deployment of JVM applica...
London Java Community - An Experiment in Continuous Deployment of JVM applica...London Java Community - An Experiment in Continuous Deployment of JVM applica...
London Java Community - An Experiment in Continuous Deployment of JVM applica...
Nicolas Fränkel
 
OSCONF - Your own Kubernetes controller: not only in Go
OSCONF - Your own Kubernetes controller: not only in GoOSCONF - Your own Kubernetes controller: not only in Go
OSCONF - Your own Kubernetes controller: not only in Go
Nicolas Fränkel
 

More from Nicolas Fränkel (20)

SnowCamp - Adding search to a legacy application
SnowCamp - Adding search to a legacy applicationSnowCamp - Adding search to a legacy application
SnowCamp - Adding search to a legacy application
 
Un CV de dévelopeur toujours a jour
Un CV de dévelopeur toujours a jourUn CV de dévelopeur toujours a jour
Un CV de dévelopeur toujours a jour
 
Zero-downtime deployment on Kubernetes with Hazelcast
Zero-downtime deployment on Kubernetes with HazelcastZero-downtime deployment on Kubernetes with Hazelcast
Zero-downtime deployment on Kubernetes with Hazelcast
 
jLove - A Change-Data-Capture use-case: designing an evergreen cache
jLove - A Change-Data-Capture use-case: designing an evergreen cachejLove - A Change-Data-Capture use-case: designing an evergreen cache
jLove - A Change-Data-Capture use-case: designing an evergreen cache
 
BigData conference - Introduction to stream processing
BigData conference - Introduction to stream processingBigData conference - Introduction to stream processing
BigData conference - Introduction to stream processing
 
ADDO - Your own Kubernetes controller, not only in Go
ADDO - Your own Kubernetes controller, not only in GoADDO - Your own Kubernetes controller, not only in Go
ADDO - Your own Kubernetes controller, not only in Go
 
TestCon Europe - Mutation Testing to the Rescue of Your Tests
TestCon Europe - Mutation Testing to the Rescue of Your TestsTestCon Europe - Mutation Testing to the Rescue of Your Tests
TestCon Europe - Mutation Testing to the Rescue of Your Tests
 
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java applicationOSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
OSCONF Jaipur - A Hitchhiker's Tour to Containerizing a Java application
 
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cacheGeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
GeekcampSG 2020 - A Change-Data-Capture use-case: designing an evergreen cache
 
JavaDay Istanbul - 3 improvements in your microservices architecture
JavaDay Istanbul - 3 improvements in your microservices architectureJavaDay Istanbul - 3 improvements in your microservices architecture
JavaDay Istanbul - 3 improvements in your microservices architecture
 
OSCONF Hyderabad - Shorten all URLs!
OSCONF Hyderabad - Shorten all URLs!OSCONF Hyderabad - Shorten all URLs!
OSCONF Hyderabad - Shorten all URLs!
 
Devclub.lv - Introduction to stream processing
Devclub.lv - Introduction to stream processingDevclub.lv - Introduction to stream processing
Devclub.lv - Introduction to stream processing
 
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring BootOSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
OSCONF Koshi - Zero downtime deployment with Kubernetes, Flyway and Spring Boot
 
JOnConf - A CDC use-case: designing an Evergreen Cache
JOnConf - A CDC use-case: designing an Evergreen CacheJOnConf - A CDC use-case: designing an Evergreen Cache
JOnConf - A CDC use-case: designing an Evergreen Cache
 
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
London In-Memory Computing Meetup - A Change-Data-Capture use-case: designing...
 
JUG Tirana - Introduction to data streaming
JUG Tirana - Introduction to data streamingJUG Tirana - Introduction to data streaming
JUG Tirana - Introduction to data streaming
 
Java.IL - Your own Kubernetes controller, not only in Go!
Java.IL - Your own Kubernetes controller, not only in Go!Java.IL - Your own Kubernetes controller, not only in Go!
Java.IL - Your own Kubernetes controller, not only in Go!
 
vJUG - Introduction to data streaming
vJUG - Introduction to data streamingvJUG - Introduction to data streaming
vJUG - Introduction to data streaming
 
London Java Community - An Experiment in Continuous Deployment of JVM applica...
London Java Community - An Experiment in Continuous Deployment of JVM applica...London Java Community - An Experiment in Continuous Deployment of JVM applica...
London Java Community - An Experiment in Continuous Deployment of JVM applica...
 
OSCONF - Your own Kubernetes controller: not only in Go
OSCONF - Your own Kubernetes controller: not only in GoOSCONF - Your own Kubernetes controller: not only in Go
OSCONF - Your own Kubernetes controller: not only in Go
 

Recently uploaded

Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
safelyiotech
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
dakas1
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
Severalnines
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
Jhone kinadey
 
TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
aeeva
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
Alberto Brandolini
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
Marcin Chrost
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
vaishalijagtap12
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
Paul Brebner
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Paul Brebner
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
dakas1
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
brainerhub1
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
kgyxske
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
gapen1
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
Tier1 app
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
Bert Jan Schrijver
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
widenerjobeyrl638
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Peter Caitens
 

Recently uploaded (20)

Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
Safelyio Toolbox Talk Softwate & App (How To Digitize Safety Meetings)
 
一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理一比一原版(USF毕业证)旧金山大学毕业证如何办理
一比一原版(USF毕业证)旧金山大学毕业证如何办理
 
Kubernetes at Scale: Going Multi-Cluster with Istio
Kubernetes at Scale:  Going Multi-Cluster  with IstioKubernetes at Scale:  Going Multi-Cluster  with Istio
Kubernetes at Scale: Going Multi-Cluster with Istio
 
Boost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management AppsBoost Your Savings with These Money Management Apps
Boost Your Savings with These Money Management Apps
 
TMU毕业证书精仿办理
TMU毕业证书精仿办理TMU毕业证书精仿办理
TMU毕业证书精仿办理
 
Modelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - AmsterdamModelling Up - DDDEurope 2024 - Amsterdam
Modelling Up - DDDEurope 2024 - Amsterdam
 
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdfBaha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
Baha Majid WCA4Z IBM Z Customer Council Boston June 2024.pdf
 
Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !Enums On Steroids - let's look at sealed classes !
Enums On Steroids - let's look at sealed classes !
 
42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert42 Ways to Generate Real Estate Leads - Sellxpert
42 Ways to Generate Real Estate Leads - Sellxpert
 
Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...Superpower Your Apache Kafka Applications Development with Complementary Open...
Superpower Your Apache Kafka Applications Development with Complementary Open...
 
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
Why Apache Kafka Clusters Are Like Galaxies (And Other Cosmic Kafka Quandarie...
 
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
一比一原版(UMN毕业证)明尼苏达大学毕业证如何办理
 
Unveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdfUnveiling the Advantages of Agile Software Development.pdf
Unveiling the Advantages of Agile Software Development.pdf
 
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
一比一原版(sdsu毕业证书)圣地亚哥州立大学毕业证如何办理
 
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
如何办理(hull学位证书)英国赫尔大学毕业证硕士文凭原版一模一样
 
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSISDECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
DECODING JAVA THREAD DUMPS: MASTER THE ART OF ANALYSIS
 
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
J-Spring 2024 - Going serverless with Quarkus, GraalVM native images and AWS ...
 
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
美洲杯赔率投注网【​网址​🎉3977·EE​🎉】
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom KittEnhanced Screen Flows UI/UX using SLDS with Tom Kitt
Enhanced Screen Flows UI/UX using SLDS with Tom Kitt
 

Improve your Android-Fu with Kotlin

  • 2. ME, MYSELF AND I Blah, blah, blah… @nicolas_frankel - Improve your Android-Fu with Kotlin 2
  • 3. MY EXPERIENCE IN ANDROID Back-end Java developer Developing a To Do list application with some improvements • Images • Alarm • Multiple lists @nicolas_frankel - Improve your Android-Fu with Kotlin 3
  • 4. MY PERSONAL CONCLUSION 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 - Improve your Android-Fu with Kotlin 4
  • 5. ORIGINAL STACK Dagger 2 Butterknife Retro-lambda Streams Green Robot’s EventBus Picasso @nicolas_frankel - Improve your Android-Fu with Kotlin 5
  • 6. OUTLINE Kotlin - the language Libraries • Stdlib • Kotlin extensions for Android • Anko @nicolas_frankel - Improve your Android-Fu with Kotlin 6
  • 7. KOTLIN Language developed by JetBrains Open Source Compiles to • JVM bytecode • JavaScript (experimental) A "simpler Scala" @nicolas_frankel - Improve your Android-Fu with Kotlin 7
  • 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 - Improve your Android-Fu with Kotlin 8
  • 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 - Improve your Android-Fu with Kotlin 9
  • 10. SETUP - BUILD.GRADLE buildscript { ext.kotlin_version = '1.0.0-beta-1038' repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin-gradle- plugin:$kotlin_version" } } @nicolas_frankel - Improve your Android-Fu with Kotlin 10
  • 11. SETUP – APP/BUILD.GRADLE apply plugin: 'kotlin-android' android { sourceSets { main.java.srcDirs += 'src/main/kotlin' test.java.srcDirs += 'src/test/kotlin' } } @nicolas_frankel - Improve your Android-Fu with Kotlin 11
  • 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 - Improve your Android-Fu with Kotlin 12
  • 13. PAIN POINTS Verbose! • Not specific to Android • Unfortunately Java @nicolas_frankel - Improve your Android-Fu with Kotlin 13
  • 14. KOTLIN SOLUTION class AddTaskEvent(task: Task, val list: List) : AbstractTaskEvent(task) @nicolas_frankel - Improve your Android-Fu with Kotlin 14
  • 15. SAMPLE CODE public class KeyboardDisplay { public void show(Activity a) { InputMethodManager imm = (InputMethodManager) a.getSystemService(INPUT_METHOD_SERVICE); imm.toggleSoftInput(SHOW_FORCED, 0); } public void hide(Activity a) { InputMethodManager imm = (InputMethodManager) a.getSystemService(INPUT_METHOD_SERVICE); imm.toggleSoftInput(HIDE_IMPLICIT_ONLY, 0); } } @nicolas_frankel - Improve your Android-Fu with Kotlin 15
  • 16. PAIN POINTS Artificial grouping Utility class • No real Object, nor state @nicolas_frankel - Improve your Android-Fu with Kotlin 16
  • 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) } @nicolas_frankel - Improve your Android-Fu with Kotlin 17
  • 18. SAMPLE CODE SQLiteDatabase db = getReadableDatabase(); db.query(TASK_TABLE, new String[] { T_ID_COL, T_NAME_COL }, null, null, null, null, T_PRIO_COL); @nicolas_frankel - Improve your Android-Fu with Kotlin 18
  • 19. PAIN POINTS @nicolas_frankel - Improve your Android-Fu with Kotlin 19 Really, pass all those null values? Create local variable for ease of use
  • 20. KOTLIN SOLUTION – PART 1 fun SQLiteDatabase.query(table:String, columns:Array<String>, selection:String? = null, selectionArgs:Array<String>? = null, groupBy:String? = null, having:String? = null, orderBy:String? = null): Cursor { return query(table, columns, selection, selectionArgs, groupBy, having, orderBy) } @nicolas_frankel - Improve your Android-Fu with Kotlin 20
  • 21. KOTLIN SOLUTION – PART 2 readableDatabase.query(TASK_TABLE, arrayOf(T_ID_COL, T_NAME_COL), orderBy = T_PRIO_COL) @nicolas_frankel - Improve your Android-Fu with Kotlin 21
  • 22. KOTLIN LIBRARY Standard API for the language @nicolas_frankel - Improve your Android-Fu with Kotlin 22
  • 23. SETUP – APP/BUILD.GRADLE dependencies { compile "org.jetbrains.kotlin:kotlin- stdlib:$kotlin_version" } @nicolas_frankel - Improve your Android-Fu with Kotlin 23
  • 24. SAMPLE CODE public class DisplayMetricsGetter { public DisplayMetrics getFrom(Context c) { WindowManager wm = (WindowManager) c.getSystemService(WINDOW_SERVICE); DisplayMetrics metrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(metrics); return metrics; } } @nicolas_frankel - Improve your Android-Fu with Kotlin 24
  • 25. PAIN POINTS Utility class Cannot return the object directly • Have to create a local variable @nicolas_frankel - Improve your Android-Fu with Kotlin 25
  • 26. 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 - Improve your Android-Fu with Kotlin 26
  • 27. 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 - Improve your Android-Fu with Kotlin 27
  • 28. KOTLIN EXTENSIONS FOR ANDROID Plugin for the Kotlin compiler Provide a "synthetic property" for each widget in a layout @nicolas_frankel - Improve your Android-Fu with Kotlin 28
  • 29. SETUP – APP/BUILD.GRADLE buildscript { repositories { jcenter() } dependencies { classpath "org.jetbrains.kotlin:kotlin- android-extensions:$kotlin_version" } } @nicolas_frankel - Improve your Android-Fu with Kotlin 29
  • 30. SAMPLE CODE public class AddActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.task_add_item); View imageView = findViewById(R.id.new_task_img); imageView.setOnClickListener(...); } } @nicolas_frankel - Improve your Android-Fu with Kotlin 30
  • 31. SAMPLE CODE – WITH BUTTERKNIFE public class AddActivity extends AppCompatActivity { @Bind(R.id.new_task_img) View imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.task_add_item); ButterKnife.bind(this); imageView.setOnClickListener(...); } } @nicolas_frankel - Improve your Android-Fu with Kotlin 31
  • 32. PAIN POINTS Do I have to detail??? @nicolas_frankel - Improve your Android-Fu with Kotlin 32
  • 33. KOTLINX SOLUTION import kotlinx.android.synthetic.task_add_item.new_task_img class AddTaskActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.task_add_item) new_task_img.setOnClickListener(...) } } @nicolas_frankel - Improve your Android-Fu with Kotlin 33
  • 34. ANKO Kotlin library for Android Mostly about replacing XML with fluent DSL for GUI But a lot of other niceties @nicolas_frankel - Improve your Android-Fu with Kotlin 34
  • 35. CODE SAMPLE public class AddAlarmClickListener implements View.OnClickListener { @Override public void onClick(View view) { View rootView = view.getRootView(); ViewFlipper flip = (ViewFlipper) rootView.findViewById(R.id.new_task_alarm_flip); flip.setDisplayedChild(1); } } @nicolas_frankel - Improve your Android-Fu with Kotlin 35
  • 36. PAIN POINTS Cast Setter (?) @nicolas_frankel - Improve your Android-Fu with Kotlin 36
  • 37. 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_alarm_flip) flip.displayedChild = 1 } } @nicolas_frankel - Improve your Android-Fu with Kotlin 37
  • 38. 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) } @nicolas_frankel - Improve your Android-Fu with Kotlin 38
  • 39. PAIN POINTS Map-based API Cast @nicolas_frankel - Improve your Android-Fu with Kotlin 39
  • 41. CODE SAMPLE AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setMessage("New list name"); EditText editText = new EditText(activity); editText.setId( android.support.v7.appcompat.R.id.select_dialog_listview); String listName = global.getList().getName(); editText.setText(listName, NORMAL); editText.selectAll(); builder.setView(editText); builder.setPositiveButton("OK", editListNameListener); builder.setNegativeButton("Cancel", (dialog, which) -> { dialog.cancel(); }); AlertDialog dialog = builder.create(); dialog.show(); @nicolas_frankel - Improve your Android-Fu with Kotlin 41
  • 42. PAIN POINTS Verbose +++ Not structured @nicolas_frankel - Improve your Android-Fu with Kotlin 42
  • 43. ANKO SOLUTION view.context.alert('New list name') { builder.setPositiveButton('OK', editListNameListener) negativeButton('Cancel') { cancel() } customView { editText(global.list.name) { id = an.sup.v7.appcompat.R.id.select_dialog_listview }.selectAll() } }.show() @nicolas_frankel - Improve your Android-Fu with Kotlin 43
  • 44. FINAL STACK Dagger 2 Butterknife  Kotlin ext. Retro-lambda  Kotlin Streams  Kotlin Green Robot’s EventBus Picasso @nicolas_frankel - Improve your Android-Fu with Kotlin 44
  • 45. MIGRATION TACTICS @nicolas_frankel - Improve your Android-Fu with Kotlin 45 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
  • 46. PITFALL @nicolas_frankel - Improve your Android-Fu with Kotlin 46 Null-able types
  • 47. NULL-ABLE VS. NON NULL-ABLE TYPES @nicolas_frankel - Improve your Android-Fu with Kotlin 47 Different type whether value can be null or not • T: cannot be null • T?: can be null
  • 48. NULL-ABLE VS. NON NULL-ABLE TYPES @nicolas_frankel - Improve your Android-Fu with Kotlin 48 Parameter types should use the right kind • Know your API • Or read the docs • Or be conservative
  • 49. MY EXPERIENCE @nicolas_frankel - Improve your Android-Fu with Kotlin 49 More expressive Higher-level abstractions One single class I couldn’t migrate • Dagger 2 module
  • 50. Q&A @nicolas_frankel - Improve your Android-Fu with Kotlin 50 http://blog.frankel.ch/ @nicolas_frankel http://frankel.in/