I'm developping a NativeScript application and want to use the WonderPush SDK. I've already used this SDK into a native Android application, so I tried to create a new plugin to wrap the SDK.
I imported the library in the gradle file, and I tried to call the library from the NativeScript plugin. But the library is empty, if I tried to log it with console.dir, the only result is an quasi-empty object:
export class NativescriptWonderpush extends Common {
init() {
console.dir(com.wonderpush);
// com.wonderpush.sdk.Wonderpush.initialize(app.android.context);
}
}
JS: ==== object dump start ====
JS: sdk: {}
JS: ==== object dump end ====
(The second line com.wonderpush.sdk.Wonderpush.initialize() crash because com.wonderpush.sdk is empty)
Thanks for your help
Native objects may not always be traceable in console like JS objects.
Though you could refer their docs and call the methods you like Or even generate typings if you are using TypeeScript by following the steps given here. The typings ensure that the classes, methods, properties etc., are public and accessible to the JS runtime.
Related
While upgrading a Xamarin app that leverages Splat's bitmap functionality we are getting errors around the use of the methods mentioned in the title:
'IBitmap' does not contain a definition for 'ToNative' and no accessible extension method 'ToNative' accepting a first argument of type 'IBitmap' could be found (are you missing a using directive or an assembly reference?)
In an effort to troubleshoot the problem we have created a new sample Android-only Xamarin app and referenced Splat and Splat.Drawing via NuGet. We then added calls to these methods and have reproduced the error:
public void Bar(IBitmap source)
{
Bitmap native = source.ToNative();
IBitmap bitmap = native.FromNative();
}
It seems as though the Andriod app may not be referencing the proper build of the Splat libraries. When I monitor paths containing %USERPROFILE%\.nuget\packages\splat in ProcMon I don't see any attempt by Visual Studio to open/read anything other than files in the .\netstandard2.0 directories during the build process.
When I open the netstandard2.0 libraries in DotPeek I can see that the class BitmapMixins is not included, but it is included in the monoandroid90 builds.
How do I get the reference to Splat in the Android project to pull in the proper build of Splat?
Example project can be found here: https://github.com/jctlp/SplatBitmap
The error caused by that you did not have the extension methods to assist with dealing with Bitmaps of ToNative and FromNative.
Please do not add reference by installing the Splat from NuGet directly. Add reference to the whole project of Splat would be okay.
Download the whole Splat project from the link below.
https://github.com/reactiveui/splat.git
I create a Xamarin.forms app named App1. And create a class Class2.cs in App1.Android. When I add reference to Splat from the project. It works well with ToNative and FromNative extension method.
Please note: Do not use the Bitmap, use Android.Graphics.Drawables.Drawable or var instead.
public void Bar(IBitmap source)
{
Android.Graphics.Drawables.Drawable native = source.ToNative();//var
IBitmap bitmap = native.FromNative();
}
I am written a very simple plugin which get all the messages from android device and pack it to response. When I am using npm install ionic-capacitor-sms-access and trying to access messages it just call web method but not the android method. So its not working can someone help me here and tell me what went wrong?
Here is my plugin https://www.npmjs.com/package/ionic-capacitor-sms-access
(Note: only the android folder)
It works, there are two ways to achieve this.
Method 1 (from the official tutorial)
What you need to do is just to keep following that tutorial to the section of 'Custom JavaScript', where shows demonstrates how to use your plugin within your ionic/typescripts codes.
such as:
import { Plugins } from '#capacitor/core';
const { SuperGreatPlugin } = Plugins;
export class CustomSuperPlugin {
constructor() {
}
customAwesomeness() {
SuperGreatPlugin.awesome();
}
}
Notes:
the name of the plugin class, i.e. 'SuperGreatPlugin' must be same with your java class for the plugin.
Method 2 (javascript way)
You can also import your plugin from the npm package you published. But be aware of the first line of the generated definitions.ts, declare module "#capacitor/core". This means you have to find your plugin from the specific module, here it is 'Plugins' as other plugins usage.
The following is the method 2:
import { SuperGreatPlugin } from 'YOUR_PLUGIN_NPM_PACKAGE';
async pluginEcho() {
await Plugins.SuperGreatPlugin.echo({value: 'bla bla bla'})
}
call the function pluginEcho() in your ionic page.
When I started I head really trouble distinguishing the web and native plugin but finally I understand that they are completely separated.
I recommend to rename the exported web plugin object in your web.ts file and add Web in the name. For me this was also necessary in order to not have compile errors.
TS2308: Module './definitions' has already exported a member named 'YourPlugin'. Consider explicitly re-exporting to resolve the ambiguity.
If you want to use your web plugin you import and use it like:
import { YourWebPlugin } from 'YOUR_PLUGIN_NPM_PACKAGE';
YourWebPlugin.callSomething();
If you want to use the native plugin you import and use it like:
import { Plugins } from '#capacitor/core';
const { YourPlugin } = Plugins;
YourPlugin.callSomething();
Don't forget to expose your native plugin to your android app project where you use your custom plugin
https://capacitor.ionicframework.com/docs/plugins/android#export-to-capacitor
I'm trying to bundle a NativeScript App with the snapshot flag like this:
tns build android --bundle --env.snapshot
The following error appears:
ERROR in NativeScriptSnapshot. Snapshot generation failed!
Target architecture: x86
# Script run failed in <embedded>#736:2461
ReferenceError: com is not defined
#
# Fatal error in ../src/snapshot/mksnapshot.cc, line 175
# Check failed: blob.data.
#
Anyone have an idea how to fix that?
The problem was that the app.scss in the root folder had been renamed to something else. Make sure it it will pass the regex ( default: /^\.\/app\.(css|scss|less|sass)$/ ) in the vendor.ts file.
See How it works part
Modules included in the snapshotted bundle can still contain native
API calls given that they are not evaluated immediately upon module
loading. For example, the following module:
require("application");
var time = new android.text.format.Time(); can’t be snapshotted
because it touches android.text.format.Time API which is not
available. However, in this one:
require("application");
function getTime() {
return new android.text.format.Time(); } the native API access is not evaluated on module execution. Given that getTime() is called
later in the fully featured V8 context provided by the Android
runtime, we are safe to include the module in the snapshotted bundle.
If the snapshotting step fails because of a reference to an undefined
API, try some of the following solutions:
If you can change the module containing the forbidden API call, wrap
the guilty code in a function that is called once the app is running
on device Keep the module in the bundle but make sure all require
calls of the non-snapshotable module are executed once the app is
running on device: require("application"); var m =
require("non-snapshotable-module");
function doSomething() {
return m.someMethod(); } The code above has a higher chance to be successfully snapshotted if it loads the non-snapshotable module when
it actually needs it:
require("application");
function doSomething() {
return require("non-snapshotable-module").someMethod(); } If doSomething() function is never called in snapshot context, the
non-snapshotable module will not be evaluated and blob generation will
succeed.
Exclude the module containing the forbidden API call from the
snapshotted bundle.
So make sure you are not referencing com in snapshot time.
I would like to use the Scala (2.11) reflection package's runtime mirror in a Scala application compiled for android which is being build using Scala on android.
I was able to fiddle with ProGuard options in order to make it include the required Scala classes. However when I try to get a mirror instance:
universe.runtimeMirror(this.getClass.getClassLoader)
(Indeed it fails during the lazy computation of universe)
The application crashes in run time:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/rmi/Remote;
at scala.reflect.internal.Definitions$DefinitionsClass.RemoteInterfaceClass$lzycompute(Definitions.scala:370)
at scala.reflect.internal.Definitions$DefinitionsClass.RemoteInterfaceClass(D efinitions.scala:370)
at scala.reflect.runtime.JavaUniverseForce$class.force(JavaUniverseForce.scal a:255)
at scala.reflect.runtime.JavaUniverse.force(JavaUniverse.scala:16)
at scala.reflect.runtime.JavaUniverse.init(JavaUniverse.scala:147)
at scala.reflect.runtime.JavaUniverse.<init>(JavaUniverse.scala:78)
at scala.reflect.runtime.package$.universe$lzycompute(package.scala:17)
at scala.reflect.runtime.package$.universe(package.scala:17)
This crash is for me as expected as it isn't:
It is expected as java.rmi is not part of the Android API and I should expect any code trying to load its classes to crash.
It is unexpected as I didn't know that Scala's reflect package used java.rmi
I have traced the code to were rmi is required, that is to JavaUniverse (a trait mixed in JavaUniverse class) force method:
...
definitions.RemoteInterfaceClass
...
Which leads to DefinitionsClass:
lazy val RemoteInterfaceClass = requiredClass[java.rmi.Remote]
Am I wrong to think that this is a no-go for Scala reflection in Android?
If I am, what could be a workaround to this problem?
To summarize your solution and a related solution, it is sufficient to add two files, and modify build.sbt to include:
dexAdditionalParams in Android += "--core-library"
Add java/rmi/Remote.java to your project with the content:
package java.rmi;
public interface Remote {}
Add java/rmi/RemoteException.java to your project with the content:
package java.rmi;
public interface RemoteException {}
I am trying to use this sms plugin - https://github.com/Ivanezko/Phonegap-SMS in my ionic android application. I am following the Readme documentation to install the plugin. In the second step author asked to do -
Require the plugin module
var smsplugin = cordova.require("info.asankan.phonegap.smsplugin.smsplugin");
What this statement means and where should I include this code to make use of this plugin?
I tried to do this in my controller file and its giving error cordova is not defined.
I tried debugging a little more in an emulator and the error given in the require statement is - module info.asankan.phonegap.smsplugin.smsplugin is not found.
First solution: 1. check that this plugin is wrapped by http://ngcordova.com/.
Second solution: 2. write wrapper = angular factory:
.factory('factoryname', ['$q', '$window','$state', function ($q,$window, $state) {
return {
function1: function () {
var q = $q.defer();
if (!$window.cordova) {
q.reject('Not supported without cordova.js');
} else {
secrettext.function1();
return q.promise;
},
3th solutions: add ['$window'] dependency to module you want use plugin. and use secrettext.function1();
*sectretext its clobbers from plugin xml
4 th solutions copy and paste this js code into you angular code on top of it and use function1();