简体   繁体   中英

Adding native code to an existing Worklight hybrid app

I have a worklight hybrid app running on android and I wanted to add some functionality to call into a native android library that isn't currently provided via worklight or cordova. My understanding is I'll need to create a cordova plugin to do this. Creating the cordova plugins seems pretty straight forward given the steps explained in the Getting started tutorial .

The one hang-up I have is when I go to create the Java class to implement the plugin. The tutorial says "Start by creating a Java class for a plug-in"... but it doesn't really say 'where' to create it. In Worklight Studio, whenever I go to create a new java class (Right click worklight project > New > Class) it wants to create it in the Project/server/java directory.

Looking at the tutorial sample code, it looks like I should be able to create a new class in the Project/Apps/name/android/nativeResources/src directory, as that is where the example class was created, but right clicking on any of those directories doesn't list a "Java Class" as a option (short of going to "Other" and picking it there). Also, there is no 'src' directory under the nativeResources directory and from my experience, that directory is typically created by eclipse as needed.

I also tried creating a "Native API" sub-project to see if that would generate the needed directories but it did not and seems to be unrelated for what I want to achieve.

So the ultimate question is should I just manually create the directories under the nativeResources folder and add in my new class (even though eclipse / worklight studio doesn't seem to recognize that as a valid location) or am I totally missing something?

The NativeAPI option is meant for native application development. As this is not what you're looking for, this is not the option to use.

The nativeResources folder is provided by Worklight for the sake of convenience (you can search Stack Overflow for "[worklight] nativeResources", you'll find plenty of reading material). Using this folder you can store various files per their location in the actual native folder, and upon build time the Worklight Builder will replace the contents of the native folder with whatever files you've placed in the nativeResources folder.

Because it's "just a folder", right-clicking it will not do much.

The place to add the class file to, just like in any Android application (this is not unique to Worklight), is in the native\\src folder (native\\src\\com\\app_name). The native folder is a representation of the generated Android project.

So you can create this file either under the native folder, or in the generated Android project.
If you create it in the generated Android folder, you'll then need to copy it back to the same location in the native folder, so that Worklight will add it to your generated Android project the next time you run a Build (Run As > Run on Worklight Development Server).

After trying a few more things, it looks like the following process worked:

  1. Right click on the project to add your native class to.
  2. Select "New > Other > Class"
  3. In the dialog that appears, select "browse" and then expand the android app "Generated Folder" listed in the dialog (this was the part I was missing above). Under that folder is a "src" directory to select.
  4. Fill out the rest of the dialog based on the tutorial and you should now see a file added that you can use for the cordova plugin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM