Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use kotlin for firebase cloud functions?

Is there a way I can use kotlin for firebase cloud functions?? I'm currently working on a project like uber and running out of time seriously, I'm familiar with kotlin but know nothing about JavaScript or type script.

I really would love it if it's possible to do it with kotlin

like image 249
Kossy Avatar asked Dec 18 '25 05:12

Kossy


1 Answers

A quick search on Google reveals this article. https://medium.com/@ishaqhassan/firebase-cloud-functions-using-kotlin-55631dd43f67

You need to start a Kotlin project and set it to compile in JavaScript. Then in build.gradle set this

compileKotlin2Js.kotlinOptions {
    moduleKind = "commonjs"
    outputFile = "functions/index.js"
}

This will tell the gradle to output the transpiled code in functions/index.js path where firebase is storing its functions by default.

Some code example from the link

external fun require(module:String) : dynamic
external var exports: dynamic

fun main(args: Array<String>) {
    val fireFunctions = require("firebase-functions")
    exports.myTestFun = fireFunctions.https.onRequest { request , response ->
        response.send("Hi from kotlin!")
    }
}

So it's possible, as with this approach, Kotlin is compiled in JavaScript, but i am not sure how easy it is to use/deploy.

like image 117
Ionut Avatar answered Dec 19 '25 19:12

Ionut