I created a Windows universal project with cordova cmd line. (create a new project, added platform windows
)
Built the project and run it from cmd line. Works fine.
My requirement is to create my own custom plugin and install it via npm cmd line.
My project also need to access native code with Windows Runtime Component.
I opened the above project CordovaApp in Visual Studio 2015.
Then I added a Windows Runtime Component also I added Newtonsoft library (link here why I added Newtonsoft manually). I added a following Service class:
public sealed class Service
{
public IAsyncOperation<string> Open(string param)
{
return OpenHelper(param).AsAsyncOperation();
}
private async Task<string> OpenHelper(string param)
{
try
{
JObject jObj = JObject.Parse(JArray.Parse(param)[0].ToString());
string message = jObj["message"].ToString();
return message;
}
catch (Exception ex)
{
return "fail";
}
}
}
I wanted to add its dll or winmd file when installing my custom plugin via cmd line. So I copied my .winmd file from bin\x86\MyRuntime.winmd and place it inside my custom plugin folder (\src\windows\lib).
My custom plugin.xml is below:
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
id="cordova-test-plugin"
version="0.1.0">
<name>Launcher</name>
<description>PhoneGap Plugin</description>
<license>MIT</license>
<keywords>phonegap,launcher</keywords>
<js-module src="www/launcher.js" name="Launcher">
<clobbers target="window.launcher" />
</js-module>
<!-- windows -->
<platform name="windows">
<js-module src="src/windows/Launcher.js" name="LauncherProxy">
<merges target="" />
</js-module>
<framework src="src/windows/lib/MyRuntime.winmd" custom="true" />
</platform>
</plugin>
I have launcher.js file in my cordova plugin from where native runtime component is called like below:
Launcher.js
cordova.commandProxy.add("LauncherProxy", {
open: function(successCallback, errorCallback, pluginParam) {
var service = MyRuntime.Service();
service.open(JSON.stringify(pluginParam)).then(function(data) {
successCallback(data);
});
}
});
require("cordova/exec/proxy").add("Launcher", module.exports);
In my index.js, I have this method call on device ready event:
launcher.openFile(function (message) {
Windows.UI.Popups.MessageDialog(message).showAsync();
console.log(message);
}, function () {
});
FYI: Also launcher.js inside the **\www** of my custom plugin has following code structure:
cordova.define("cordova-test-plugin.Launcher", function(require, exports, module) {
var objWindowsLauncher = {
openFile: function(successCallback, errorCallback) {
cordova.exec(
successCallback,
errorCallback,
'LauncherProxy',
'open',
[{
"message": "Hello test message"
}]
);
}
}
module.exports = objWindowsLauncher;
});
Till now it works well. It pop up's Hello test message
when app is run.
Now I added a new Class library project for SQLite. Installed sqlite-net (include SQLite.cs and SQLiteAsync.cs files from old project), added Newtonsoft.json dll, SQlite for Universal Windows Platform, Visual C++ 2015 Runtime for Universal Windows Platfrom.
Added this SQLite reference (Class library) to my previous Runtime Component project by right clicking on the Reference and selecting SQLite project. By doing this, now I can do sqlite operation in runtime component. I build this runtime component and hence its new .winmd file is generated inside bin\x86\Debug. I copied this winmd file and place it inside my custom plugin folder like above.
I removed the plugin and re-added the plugin (cmd line installation) just like above. This time also, the project should work same as previous, but doesn't work.
It throws an exception.
System.IO.FileNotFoundException: Could not load file or assembly 'SQLite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified.
at MyRuntime.Service.<OpenHelper>d__1.MoveNext()
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder 1.Start[TStateMachine](TStateMachine& stateMachine)
Another case:
Sometimes, I get this exception with the exact above process:
Could not load file or assembly 'Newtonsoft.Json, Version=9.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies
INFO
All the project build configuration is under x86.
Newtonsoft version is: 9.0.1, 10.x.x
Cordova Windows 10 project, Runtime Component and Class Library has: 10.0.14393.0 (Target Version) and 10.0.10240.0 (Min Version)
Cordova version: 7.0.1 Cordova Windows version: 5.0.0
Plugin installation and remove cmd:
cordova plugin remove cordova-test-plugin
cordova plugin add ..\plugins\cordova-test-plugin
I have shared the complete project with custom plugin here in onedrive.
P.S. I can add the Runtime component reference manually and make it work but I want to do with cmd line interface, install pluigns, build and generate packages from cmd line interface without interacting with Visual Studio.
Please let me know your views, I remember when I started to work few months back, it used to work. (adding reference from cmd line by defining .winmd file in plugin.xml (<framework src="src/windows/lib/MyRuntime.winmd" custom="true" />
)). It used to work fine. But recently I'm facing above problem.
UPDATE 1
I tried as @Elvis Xia suggested. This seems to load the runtime component as expected. I included all these:
<framework src="src/windows/lib/SQLite.dll" custom="true"/>
<framework src="src/windows/lib/MyRuntime.winmd" custom="true" />
<framework src="src/windows/lib/Newtonsoft.Json.dll" custom="true"/>
in my plugin.xml and installed the plugin via cmd line. There is still some bug while creating SQLite database. I faced system.dllnotfoundexception unable to load dll 'sqlite3'
error as: