How to add Android Kotlin files to a Cordova Plugin project
Asked Answered
P

2

10

Is it possible to add Android Kotlin files to a Cordova Plugin project or is only Java supported?

<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
        version="0.0.1">

  <!-- android -->
  <platform name="android">

    <source-file src="src/android/nl/companyname/kotlin/ScanMode.kt" target-dir="src/nl/companyname/kotlin"/>

  </platform>

</plugin>

The Java files show up, but the Kotlin files don't. I also don't see any plugin that has it.

Precambrian answered 14/12, 2020 at 20:4 Comment(1)
i think this link may be useful for you github.com/dpa99c/cordova-plugin-hello-kotlinTenney
V
9

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://mcmap.net/q/1164438/-kotlin-for-a-cordova-ionic-based-plugin

I modifed an example repository to test this:

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

Vegetarian answered 17/12, 2020 at 18:31 Comment(0)
R
4

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

Riffle answered 17/12, 2020 at 21:28 Comment(1)
yeah but the question was specific about Cordova Plugin.Precambrian

© 2022 - 2024 — McMap. All rights reserved.