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 full-size slide

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

    View full-size slide

  3. 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 full-size slide

  4. 01.
    Introduction

    View full-size slide

  5. 6
    Introduction
    01.
    Kotlin?

    View full-size slide

  6. 7
    Introduction
    01.
    Kotlin? Android?

    View full-size slide

  7. 8
    Introduction
    01.
    Kotlin? Android? Backend

    View full-size slide

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

    View full-size slide

  9. 02.
    Frameworks

    View full-size slide

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

    View full-size slide

  11. 13

    Ktor
    Why?

    View full-size slide

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

    View full-size slide

  13. 04.
    Tutorial.

    View full-size slide

  14. 16

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

    View full-size slide

  15. 17

    Tutorial: Web

    View full-size slide

  16. 18

    Tutorial: Web
    Create Project

    View full-size slide

  17. 19

    Tutorial: Web
    Create Project

    View full-size slide

  18. 20

    Tutorial: Output

    View full-size slide

  19. Code run through!

    View full-size slide

  20. 22

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

    View full-size slide

  21. 23

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

    View full-size slide

  22. 24

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

    View full-size slide

  23. 26

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

    View full-size slide

  24. 27

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

    View full-size slide

  25. 28

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

    View full-size slide

  26. 29

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

    View full-size slide

  27. 30

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

    View full-size slide

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

    View full-size slide

  29. 33

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

    View full-size slide


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

    View full-size slide

  31. 36

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

    View full-size slide

  32. 37

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

    View full-size slide

  33. 38

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

    View full-size slide

  34. 07.
    Real world example.

    View full-size slide

  35. 40

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

    View full-size slide

  36. 41

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

    View full-size slide

  37. 42

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

    View full-size slide

  38. 43

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

    View full-size slide

  39. 44

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

    View full-size slide

  40. 45

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

    View full-size slide

  41. 08.
    Locations

    View full-size slide


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

    View full-size slide

  43. 48

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

    View full-size slide

  44. 49

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

    View full-size slide

  45. 50

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

    View full-size slide

  46. 09.
    Status Page

    View full-size slide


  47. Response to any failure state/Exception.

    View full-size slide

  48. 53

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

    View full-size slide

  49. 54

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

    View full-size slide

  50. 10.
    Libraries Available for
    Awesomeness

    View full-size slide

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

    View full-size slide

  52. 10.
    Why use it as android Dev?

    View full-size slide

  53. 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 full-size slide

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

    View full-size slide

  55. 12.
    Resources

    View full-size slide

  56. 62

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

    View full-size slide

  57. Danke Schön! Questions?

    View full-size slide