Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Backend Engineering for Android Developers

Backend Engineering for Android Developers

With Kotlin growing in popularity, it's about time we start looking at Kotlin as a language that has superpowers.
When we talk about backend technologies, we have a lot of languages/frameworks like Go, Java, Python, etc.
But when we talk about Kotlin as a backend language, it might raise a lot of eyebrows
But let me try to convince you that Kotlin is definitely a good choice for Backend! We will talk about Ktor (A framework for Kotlin) to build a fast backend that can be re-used on your client-side

Himanshu Singh

October 22, 2021
Tweet

More Decks by Himanshu Singh

Other Decks in Technology

Transcript

  1. Backend for
    Android Engineers

    View Slide

  2. 01.
    About me

    View Slide

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

    View Slide

  4. 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?

    View Slide

  5. 01.
    Introduction

    View Slide

  6. 6
    Introduction
    01.
    Kotlin?

    View Slide

  7. 7
    Introduction
    01.
    Kotlin? Android?

    View Slide

  8. 8
    Introduction
    01.
    Kotlin? Android? Backend

    View Slide

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

    View Slide

  10. 02.
    Frameworks

    View Slide

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

    View Slide

  12. 03.
    Ktor

    View Slide

  13. 13

    Ktor
    Why?

    View Slide

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

    View Slide

  15. 04.
    Tutorial.

    View Slide

  16. 16

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

    View Slide

  17. 17

    Tutorial: Web

    View Slide

  18. 18

    Tutorial: Web
    Create Project

    View Slide

  19. 19

    Tutorial: Web
    Create Project

    View Slide

  20. 20

    Tutorial: Output

    View Slide

  21. Code run through!

    View Slide

  22. 22

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

    View Slide

  23. 23

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

    View Slide

  24. 24

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

    View Slide

  25. 05.
    Routing

    View Slide

  26. 26

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

    View Slide

  27. 27

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

    View Slide

  28. 28

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

    View Slide

  29. 29

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

    View Slide

  30. 30

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

    View Slide

  31. HTTP Verbs

    View Slide

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

    View Slide

  33. 33

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

    View Slide

  34. 06.
    Plugins

    View Slide


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

    View Slide

  36. 36

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

    View Slide

  37. 37

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

    View Slide

  38. 38

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

    View Slide

  39. 07.
    Real world example.

    View Slide

  40. 40

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

    View Slide

  41. 41

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

    View Slide

  42. 42

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

    View Slide

  43. 43

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

    View Slide

  44. 44

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

    View Slide

  45. 45

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

    View Slide

  46. 08.
    Locations

    View Slide


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

    View Slide

  48. 48

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

    View Slide

  49. 49

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

    View Slide

  50. 50

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

    View Slide

  51. 09.
    Status Page

    View Slide


  52. Response to any failure state/Exception.

    View Slide

  53. 53

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

    View Slide

  54. 54

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

    View Slide

  55. 10.
    Libraries Available for
    Awesomeness

    View Slide

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

    View Slide

  57. 10.
    Why use it as android Dev?

    View Slide

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

    View Slide

  59. 11.
    Summary

    View Slide

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

    View Slide

  61. 12.
    Resources

    View Slide

  62. 62

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

    View Slide

  63. Danke Schön! Questions?

    View Slide