How to add Android Kotlin files to a Cordova Plugin project

add the following in config.xml

<preference name="GradlePluginKotlinEnabled" value="true" />
<preference name="GradlePluginKotlinCodeStyle" value="official" />
<preference name="GradlePluginKotlinVersion" value="1.3.50" />

also note where to put the kotlin files, i.e. src/main/kotlin///

using:

<source-file src="src/android/file.kt" target-dir="app/src/main/kotlin/xx/yy/zz" />

and add the below to your plugin's gradle.build if the kotlin files aren't picked up by your project

android {
    sourceSets {
        main.java {
            srcDirs += 'src/main/kotlin'
        }
    }
}

This answer is referenced from: https://stackoverflow.com/a/63872580/10999673

I modifed an example repository to test this:

https://github.com/kilisio/cordova-plugin-hello-kotlin.git


Kotlin support was added in Cordova 9.0.0, which was released on January 23 2020. Here you can find more information: https://cordova.apache.org/announcements/2020/06/29/cordova-android-9.0.0.html