如何将AndroidKotlin文件添加到Cordova插件项目

是否可以将 Android Kotlin 文件添加到Cordova 插件项目或仅支持 Java?

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

Java 文件会出现,但 Kotlin 文件不会。我也没有看到任何具有它的插件。

回答

在 config.xml 中添加以下内容

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

还要注意放置 kotlin 文件的位置,即 src/main/kotlin//

使用:

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

如果您的项目没有选择 kotlin 文件,请将以下内容添加到您的插件的 gradle.build 中

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

此答案参考自:https : //stackoverflow.com/a/63872580/10999673

我修改了一个示例存储库来测试这个:

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

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/low-quality-posts/27872480)

以上是如何将AndroidKotlin文件添加到Cordova插件项目的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>