I have a very basic Java function that I want to call through a NativeScript Vue app. The .java file looks like this:
package com.example.toastermodule;
import android.content.Context;
import android.widget.Toast;
public class Toaster {
public void show(Context context) {
CharSequence text ="Hello NativeScript!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
This article (https://nativescript.org/blog/plugins-and-jars/) is where I got the Java code, and walks me through exporting the functionality as a jar, and importing it into NativeScript. However, the gradle code provided doesn't actually produce the jar file. I've found various references to this guide online, with updates to the gradle instructions to account for changes in where the build files are stored, but I am still unable to produce a single composite jar file.
I've also used this article (https://docs.nativescript.org/plugins/plugin-reference) to try to import the Java plugin directly. I created a basic package.json file for the Java project which looks like this:
{
"name": "nativescript-my-plugin",
"version": "0.0.1",
"nativescript": {
"platforms": {
"ios": "4.0.0",
"android": "4.1.0"
}
}
}
And I put this file into the main Project folder (e.g. "~/AndroidStudioProjects/Toaster/"). When I open a terminal in my NativeScript project and run tns add plugin "~/AndroidStudioProjects/Toaster/"
, I get the encouraging message: Successfully installed plugin nativescript-my-plugin.
However, if I change the name in the package.json file from "nativescript-my-plugin", I get an error message: Cannot find module 'toaster-plugin/package.json'
. I have no idea why changing the name would give me this error message, since as far as I can tell, the name is arbitrary and not referenced anywhere else.
Additionally, when I try to actually use the plugin (my Home.vue looks like this)
<template>
<Page>
<ActionBar>
<Label text="Home"/>
</ActionBar>
<GridLayout>
<Label class="info">
<FormattedString>
<Span class="fas" text.decode=" "/>
<Span :text="message"/>
</FormattedString>
</Label>
<Button @tap="printJava" text="Print Java"/>
</GridLayout>
</Page>
</template>
<script>
var myPlugin = require("nativescript-my-plugin");
export default {
computed: {
message() {
return "Blank {N}-Vue app";
}
},
methods: {
printJava(){
myPlugin.show();
}
}
}
</script>
<style scoped lang="scss">
@import '~@nativescript/theme/scss/variables/blue';
// Custom styles
.fas {
@include colorize($color: accent);
}
.info {
font-size: 20;
horizontal-align: center;
vertical-align: center;
}
</style>
I get this error message Module not found: Error: Can't resolve 'nativescript-my-plugin' in '~java-test-2appcomponents'
, followed by
Error from chokidar (C:): Error: EBUSY: resource busy or locked, lstat 'C:hiberfil.sys'
File change detected. Starting incremental webpack compilation...
File change detected. Starting incremental webpack compilation...
Despite not actually making any changes to the code.
So my questions are, why does the name declared in package.json
only work if it is "nativescript-my-plugin"
? And what am I doing wrong in this process?
question from:
https://stackoverflow.com/questions/65945903/nativescript-vue-unable-to-use-custom-java-plugin