Slide 1

Slide 1 text

Backend for Android Engineers

Slide 2

Slide 2 text

01. About me

Slide 3

Slide 3 text

3 Himanshu Singh Android @Clue, GDE - Android,Kotlin

Slide 4

Slide 4 text

4 Agenda: Backend for Android Engineers 01. Intro Kotlin Android? Backend Benefits 02. Kotlin Frameworks Ktor Spring Boot Vertx. Etc. 03. Ktor Why? How? Benefits 04. Tutorial Setting up Restful APIs Routing HTTP Verbs Features Location Status Pages 05. Why to use as Android Developer?

Slide 5

Slide 5 text

01. Introduction

Slide 6

Slide 6 text

6 Introduction 01. Kotlin?

Slide 7

Slide 7 text

7 Introduction 01. Kotlin? Android?

Slide 8

Slide 8 text

8 Introduction 01. Kotlin? Android? Backend

Slide 9

Slide 9 text

Benefits 1. 2. 3. 4. 5. Expressive Tooling Migration 6. Scalable! Community Support Ease of Learning

Slide 10

Slide 10 text

02. Frameworks

Slide 11

Slide 11 text

Frameworks 1. 2. 3. 4. 5. Ktor Micronauts Vertx Spring Boot A lot more!

Slide 12

Slide 12 text

03. Ktor

Slide 13

Slide 13 text

13  Ktor Why?

Slide 14

Slide 14 text

Ktor 1. 2. 3. 4. 5. Light weighted Backed by Jetbrains Coroutine support Open sourced Scalable -> ∞

Slide 15

Slide 15 text

04. Tutorial.

Slide 16

Slide 16 text

16  Tutorial: Web https://start.ktor.io/

Slide 17

Slide 17 text

17  Tutorial: Web

Slide 18

Slide 18 text

18  Tutorial: Web Create Project

Slide 19

Slide 19 text

19  Tutorial: Web Create Project

Slide 20

Slide 20 text

20  Tutorial: Output

Slide 21

Slide 21 text

Code run through!

Slide 22

Slide 22 text

22  fun main() { embeddedServer(Netty, port = 8080, host = "0.0.0.0") { configureRouting() }.start(wait = true) }

Slide 23

Slide 23 text

23  fun main() { embeddedServer(Netty, port = 8080, host = "0.0.0.0") { configureRouting() }.start(wait = true) }

Slide 24

Slide 24 text

24  fun main() { embeddedServer(Netty, port = 8080, host = "0.0.0.0") { configureRouting() }.start(wait = true) }

Slide 25

Slide 25 text

05. Routing

Slide 26

Slide 26 text

26  fun Application.configureRouting() { // Starting point for a Ktor app: routing { get("/") { call.respondText("Hello World!") } } }

Slide 27

Slide 27 text

27  fun Application.configureRouting() { // Starting point for a Ktor app: routing { get("/") { call.respondText("Hello World!") } } }

Slide 28

Slide 28 text

28  fun Application.configureRouting() { // Starting point for a Ktor app: routing { get("/") { call.respondText("Hello World!") } } }

Slide 29

Slide 29 text

29  fun Application.configureRouting() { // Starting point for a Ktor app: routing { get("/") { call.respondText("Hello World!") } } }

Slide 30

Slide 30 text

30  fun Application.configureRouting() { // Starting point for a Ktor app: routing { get("/") { call.respondText("Hello World!") } } }

Slide 31

Slide 31 text

HTTP Verbs

Slide 32

Slide 32 text

HTTP Verbs 1. 2. 3. 4. GET PUT/PATCH DELETE POST

Slide 33

Slide 33 text

33  routing { get("/") { //code } post("/") { //code } delete("/") { //code } put("/") { //code } }

Slide 34

Slide 34 text

06. Plugins

Slide 35

Slide 35 text

“ Set of pre-coded packages to help you ship/build faster

Slide 36

Slide 36 text

36  fun Application.dummyPlugin(){ install(//the plugin) }

Slide 37

Slide 37 text

37  fun main() { embeddedServer(Netty, port = 8080, host = "0.0.0.0") { configureRouting() dummyPlugin() }.start(wait = true) }

Slide 38

Slide 38 text

38  fun Application.contentNegotiation(){ install(ConentNegotiation){ gson() } }

Slide 39

Slide 39 text

07. Real world example.

Slide 40

Slide 40 text

40  fun Application.configureRouting(){ post("/create-user"){ val user = call.receive() //create user call.respond(user) } }

Slide 41

Slide 41 text

41  fun Application.configureRouting(){ post("/create-user"){ val user = call.receive() //create user call.respond(user) } }

Slide 42

Slide 42 text

42  { "name":"Himanshu", "age":26, "job":"Clue" } data class User( val name:String, val age:Int, val job:String )

Slide 43

Slide 43 text

43  https://www.google.com/search?q=droidcon+berlin

Slide 44

Slide 44 text

44  https://www.google.com/search?q=droidcon+berlin

Slide 45

Slide 45 text

45  fun Application.configureRouting(){ get("/search"){ val param = call.request.queryParameters[“q”] //code call.respond(result) } }

Slide 46

Slide 46 text

08. Locations

Slide 47

Slide 47 text

“ A mechanism to create routes in a typed way, for both: constructing URLs and reading the parameters.

Slide 48

Slide 48 text

48  fun Application.module() { install(Locations) // ... }

Slide 49

Slide 49 text

49  @Location("/list/{name}/page/{page}") data class Listing(val name: String, val page: Int)

Slide 50

Slide 50 text

50  routing { get { listing -> call.respondText("Listing ${listing.name}, page ${listing.page}") } }

Slide 51

Slide 51 text

09. Status Page

Slide 52

Slide 52 text

“ Response to any failure state/Exception.

Slide 53

Slide 53 text

53  fun Application.module() { install(StatusPages) // ... }

Slide 54

Slide 54 text

54  install(StatusPages) { exception { cause -> call.respond(HttpStatusCode.Unauthorized) } exception { cause -> call.respond(HttpStatusCode.Forbidden) } }

Slide 55

Slide 55 text

10. Libraries Available for Awesomeness

Slide 56

Slide 56 text

Why? 1. 2. 3. 4. 5. KMongo/Expose In build Authentication/Auth Velocity / Kotlin HTML Gson/Jackson Swagger, ORM etc.

Slide 57

Slide 57 text

10. Why use it as android Dev?

Slide 58

Slide 58 text

Why? 1. 2. 3. 4. 5. Uses our fav. Kotlin KMM support Easy learning curve. Supports JVM Libraries Makes you a full stack developer.

Slide 59

Slide 59 text

11. Summary

Slide 60

Slide 60 text

Summary 1. 2. Use Kotlin to do anything Build your own backend

Slide 61

Slide 61 text

12. Resources

Slide 62

Slide 62 text

62  Resources https://ktor.io/ https://himanshoe.com

Slide 63

Slide 63 text

Danke Schön! Questions?