简体   繁体   English

将本机代码添加到现有的Worklight混合应用程序

[英]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. 我有一个在Android上运行的worklight混合应用程序,我想添加一些功能以调用当前未通过worklight或cordova提供的本机android库。 My understanding is I'll need to create a cordova plugin to do this. 我的理解是,我需要创建一个cordova插件来执行此操作。 Creating the cordova plugins seems pretty straight forward given the steps explained in the Getting started tutorial . 鉴于入门教程中介绍的步骤,创建cordova插件似乎非常简单。

The one hang-up I have is when I go to create the Java class to implement the plugin. 我遇到的一个麻烦就是我去创建Java类来实现插件。 The tutorial says "Start by creating a Java class for a plug-in"... but it doesn't really say 'where' to create it. 该教程说“开始为插件创建Java类”……但是它并没有真正说出在何处创建它。 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. 在Worklight Studio中,每当我要创建一个新的Java类(右键单击worklight项目>“新建”>“类”)时,它都想在Project / server / java目录中创建它。

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). 查看本教程的示例代码,看起来我应该能够在Project / Apps / name / android / nativeResources / src目录中创建一个新类,因为这是创建示例类的地方,但是右键单击任何这些目录没有列出“ Java类”作为选项(除非转到“其他”并在那里进行选择)。 Also, there is no 'src' directory under the nativeResources directory and from my experience, that directory is typically created by eclipse as needed. 另外,在nativeResources目录下没有“ src”目录,根据我的经验,该目录通常是根据需要由eclipse创建的。

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. 我还尝试创建一个“ Native API”子项目,以查看是否会生成所需的目录,但是它没有,并且似乎与我想要实现的目标无关。

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? 因此,最终的问题是我应该手动在nativeResources文件夹下创建目录并添加我的新类(即使eclipse / worklight studio似乎无法将其识别为有效位置)还是我完全丢失了某些东西?

The NativeAPI option is meant for native application development. NativeAPI选项适用于本机应用程序开发。 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). 为了方便起见,Worklight提供了nativeResources文件夹(您可以在Stack Overflow中搜索“ [worklight] nativeResources”,您会发现大量阅读材料)。 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. 使用此文件夹,您可以在实际本机文件夹中按其位置存储各种文件,并且在构建时,Worklight Builder将用您放置在nativeResources文件夹中的任何文件替换本机文件夹的内容。

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). 就像在任何Android应用程序中一样(不是Worklight唯一的),将类文件添加到的位置在native \\ src文件夹(native \\ src \\ com \\ app_name)中。 The native folder is a representation of the generated Android project. 本机文件夹表示生成的Android项目。

So you can create this file either under the native folder, or in the generated Android project. 因此,您可以在本机文件夹下或在生成的Android项目中创建此文件。
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). 如果您在生成的Android文件夹中创建它,则需要将其复制回本机文件夹中的相同位置,以便Worklight在下次运行Build时将其添加到生成的Android项目中(运行方式>在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). 在出现的对话框中,选择“浏览”,然后展开对话框中列出的android应用“ Generated Folder”(这是我上面缺少的部分)。 Under that folder is a "src" directory to select. 在该文件夹下有一个“ src”目录可供选择。
  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. 根据教程填写对话框的其余部分,您现在应该看到添加了可用于cordova插件的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Worklight 6.1 Hybrid应用程序将参数从本地返回到混合 - Worklight 6.1 hybrid app return param from native to hybrid Worklight 6.2 Hybrid应用程序中的空闲时间 - Idle period in Worklight 6.2 Hybrid app 部署本机.js文件时,IBM Hybrid上的IBM Worklight更改应用程序消失,我该如何保存更改? - IBM Worklight Changes at Hybrid app on Native .js files disappear when i deploy the app, How could I mantain my changes? 如何在Worklight中实现Native和Hybrid应用程序之间的交互? - How to achieve interaction between Native and Hybrid Applications in Worklight? 将本机应用程序与Worklight项目相结合 - Combining native app with worklight project 如何检查应用程序(本机或混合) - How to check for an app (native or hybrid) 如何强制Worklight Android混合应用崩溃? - How can I force a Worklight Android hybrid app to crash? 如何从一个基于Worklight的混合应用程序向另一个基于Worklight的混合应用程序发送推送通知 - How to send a push notification from one Worklight-based Hybrid app to another Worklight-based Hybrid app IBM Worklight 6.0 - 无法在avd上使用dojo toolkit for android环境运行示例混合工作灯应用程序? - IBM Worklight 6.0 - Unable to run sample hybrid worklight app using dojo toolkit for android environment on avd? 将本机 Android 代码添加到 Flutter 应用程序 - Adding native Android code to a Flutter app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM