76
Views
1
Comments
Solved
Integrating Kotlin code into a cordova based android plugin
Question
Application Type
Mobile

I'm designing a cordova based plugin that uses swift (ios) and kotlin (android) scripts, but there is some issue with the kotlin implementation, so before going through the code itself I wanted to make sure it's properly implemented in the xml file. What is the correct way to define the android section in plugin.xml? Currently I have the following:


<platform name="android">
<config-file parent="/*" target="res/xml/config.xml">
<preference name="GradlePluginVersion" value="8.0.0" />
<preference name="GradlePluginKotlinEnabled" value="true" />
<preference name="GradlePluginKotlinCodeStyle" value="official" />
<preference name="GradlePluginKotlinVersion" value="1.8.10" />
<feature name="PluginName">
<param name="android-package" value="com.mycompany.myplugin.pluginName"/>
</feature>
</config-file>
<config-file parent="/*" target="AndroidManifest.xml"/>
<framework src="myframework:version" />
<source-file src="src/android/PluginName.kt" target-dir="src/com/mycompany/myplugin" />
<source-file src="src/android/File2.kt" target-dir="src/com/mycompany/myplugin" />
<source-file src="src/android/File3.kt" target-dir="src/com/mycompany/myplugin" />
</platform>

where "com.mycompany.myplugin" is referenced as package inside each of the 3 kotlin files.
The 3 files are located in src/android relative to plugin.xml within the repository.
The Extensibility Configuration in outsystems is set up as follows:

{    
"plugin" :{        
           "url": "my git repository",       
           "pluginName":"PluginName" },
"preferences": {
            "android": [
                    {
                    "name": "GradlePluginVersion",
                    "value": "8.0.0"
                    },
                    {
                    "name": "GradlepluginKotlinVersion",
                    "value": "1.8.10"
                    },
                    {
                    "name": "GradlePluginKotlinEnabled",
                    "value": "true"
                    }
                ],
            "ios": [
                    {
                    "name": "UseSwiftLanguageVersion",
                    "value": "5"
                    },
                    {
                    "name": "SwiftVersion",
                    "value": "5.0"
                    }
                ]
            }
}
The application generates successfully but when triggering the exec function (inside an emulator with error logs) we get the following errors:

2023-05-31 15:29:08.041 32336-32466 System.err              myenvironment.myapp  W  java.lang.ClassNotFoundException: PluginName

2023-05-31 15:29:08.042 32336-32466 System.err              myenvironment.myapp   W      at java.lang.Class.classForName(Native Method)

2023-05-31 15:29:08.042 32336-32466 System.err              myenvironment.myapp   W      at java.lang.Class.forName(Class.java:454)

2023-05-31 15:29:08.042 32336-32466 System.err              myenvironment.myapp   W      at java.lang.Class.forName(Class.java:379)

2023-05-31 15:29:08.042 32336-32466 System.err              myenvironment.myapp   W      at org.apache.cordova.PluginManager.instantiatePlugin(PluginManager.java:564)

2023-05-31 15:29:08.042 32336-32466 System.err              myenvironment.myapp   W      at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:183)

2023-05-31 15:29:08.043 32336-32466 System.err              myenvironment.myapp   W      at org.apache.cordova.PluginManager.exec(PluginManager.java:136)

2023-05-31 15:29:08.043 32336-32466 System.err              myenvironment.myapp   W      at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)

2023-05-31 15:29:08.043 32336-32466 System.err              myenvironment.myapp   W      at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)

2023-05-31 15:29:08.044 32336-32466 System.err              myenvironment.myapp   W      at android.os.MessageQueue.nativePollOnce(Native Method)

2023-05-31 15:29:08.044 32336-32466 System.err              myenvironment.myapp   W      at android.os.MessageQueue.next(MessageQueue.java:335)

2023-05-31 15:29:08.044 32336-32466 System.err              myenvironment.myapp   W      at android.os.Looper.loopOnce(Looper.java:161)

2023-05-31 15:29:08.044 32336-32466 System.err              myenvironment.myapp   W      at android.os.Looper.loop(Looper.java:288)

And below that a very similar error set starting with the following instead:

"......  Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mycompany.myplugin.PluginName" on path: DexPathList[[zip file "/data/app/~~h36V_lchCw-ecKFBp2ZB6A==/myenvironment.myapp -VZQmUUbdgrNo6S0n18mOuw==/base.apk"],nativeLibraryDirectories=[/data/app/~~h36V_lchCw-ecKFBp2ZB6A==/myenvironment.myapp 
-VZQmUUbdgrNo6S0n18mOuw==/lib/x86_64, /data/app/~~h36V_lchCw-ecKFBp2ZB6A==/myenvironment.myapp 
-VZQmUUbdgrNo6S0n18mOuw==/base.apk!/lib/x86_64, /system/lib64, /system_ext/lib64]]"


Is there something set up incorrectly above? Thanks for the help!

2021-02-16 20-34-58
Remco Snijders
 
MVP
Solution

The kotlin files need to be in target directory src/main/kotlin according to the release notes of Cordova Android 9.0.0. The default build.gradle refers to this location. Your source files will look like this:

<source-file src="src/android/PluginName.kt" target-dir="src/main/kotlin" />
<source-file src="src/android/File2.kt" target-dir="src/main/kotlin " />
<source-file src="src/android/File3.kt" target-dir="src/main/kotlin " /> 

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.