Nativescript throwing variable not declared


#1

I created a plugin for native script using this plugin seed https://github.com/NathanWalker/nativescript-plugin-seed .

My plugin contains a .jar file with some classes with packages like com.etc.*.

When running the demo app that comes with this seed everything works, the classes from the jar that I mention on the js file are found and run ok, like com.etc.SomeClass.doSomething works.

But when I plublish this plugin and install on another app, it says that the first package of the class is not declared com.etc.*, ‘com’ is not declared.

While debugging on chrome, I noticed that the com variable is declared while running the demo app, but not is not declared on the other app.

While digging through the documentation, I found that {N} is able to access the android and java packages throught the Metadata mapping (https://docs.nativescript.org/angular/runtimes/android/metadata/overview) I think that this metadata is being generated when running the demo app and not when running the other app, maybe because of the plugin structure after being published?

Structure after being published and installed:

Structure after being published and installed

Structure before on the seed project:

Structure before on the seed project:

UPDATE1: I built the apk from the two apps and tried to find something different between the two. I saw these that these three files: treeValueStream.dat, treeStringsStream.dat e treeNodeStream.dat probably has something to do with the mappings generated and in the treeStringsStream.dat you can see the name of the java and android classes and in my case, my classes are there too on the demo apk but not on the oterapp apk.


#2

try adding
declare var com: any
at the top of your .d.ts files.
(and also at the top of android.ts file if you haven’t already)


#3

The error moved to the next package on the chain.

Even if I declare all the packages, I don’t think the class will be available because it will be just a placeholder. Right?


#4

They will not be placeholder values. You’re just solving the typing conflicts. The full qualified package name com.foo.bar will actually be a reference to the underlying Android class file.


#5

How can I declare the entire package chain then?

And shouldn’t {N} take care of this? I still don’t get it why it works on one app and doesn’t on the other.


#6

A lot of people set-up variables to those package names, i.e:

Why it works in one vs. the other is probably a difference in the actual tsconfig.json configuration as well as plugin demos don’t always to use the compiled versions of the plugin. You can run an npm pack in your plugin’s directory (where the package.json is) and see what the generated files inside the tgz look like. That’ll give you an idea if the typings are not being generated correctly.


#7

Have you generated typings for the jar file?
Use android-dts-generator


#8

I generated them now and placed them in the index.d.ts file on the plugin, but I still get the same error.

I think I am missing something.


#9

Got it to work, I did a lot of things, but what I think that solved was to add the jar to the include.gradle. Before this, the jar wasn’t being added to the plugin folder when adding the plugin using tns plugin add


#10

@rodcs
can you show how your include.gradle looks now?


#11

Sure.

It looks like this now

/* Include.gradle configuration: http://docs.nativescript.org/plugins/plugins#includegradle-specification */

android {
	dexOptions {
        jumboMode true
    }
	productFlavors {
		"nativescript-signer" {
			dimension "nativescript-signer"
		}
	}
}

dependencies {
        compile files('../../../node_modules/nativescript-signer/platforms/android/libs/Signer.jar')
}

It was like this before:

/* Include.gradle configuration: http://docs.nativescript.org/plugins/plugins#includegradle-specification */

android {
	dexOptions {
        jumboMode true
    }
	productFlavors {
		"nativescript-signer" {
			dimension "nativescript-signer"
		}
	}
}

//dependencies {
 //       
//}

I’m using a local jar for now.


#12

thanks that works @rodcs