java 如何设置具有 IDE 支持的 Cordova 插件项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32649070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:33:26  来源:igfitidea点击:

How to set up Cordova plugin project with IDE support?

javaandroidcordovaandroid-studiocordova-plugins

提问by CarlLee

I've been struggling to set up my cordova plugin project. Mainly due the facts that:

我一直在努力设置我的cordova插件项目。主要是由于以下事实:

  • Plugins need to be in a separate folder away from the main project

  • When I use, for example, cordova build androidto build the project, cordova copies the java file from my plugin folder and put it into platforms/android/srcfolder.

  • Thus I should not modify my plugin's .java file in the Android project manually, I have to write my code in my plugin folder.

  • But I can't import plugin folder into the IDE project, thus I don't have code completion.

  • It's basically impossible to write Java/Objective-C without IDE support

  • 插件需要位于远离主项目的单独文件夹中

  • 例如,当我使用cordova build android构建项目时,cordova 从我的插件文件夹中复制 java 文件并将其放入platforms/android/src文件夹中。

  • 因此,我不应该手动修改 Android 项目中插件的 .java 文件,我必须在插件文件夹中编写代码。

  • 但是我无法将插件文件夹导入到 IDE 项目中,因此我没有代码完成。

  • 没有 IDE 支持,基本上不可能编写 Java/Objective-C

How can I set up an IDE(for example, for Android Studio) project with code completion for my plugin development?

如何为我的插件开发设置具有代码完成功能的 IDE(例如,用于 Android Studio)项目?

回答by Niko

Developing a Cordova plugin is a bit of a pain.

开发 Cordova 插件有点痛苦。

Here is a way to do it :

这是一种方法:

  1. Create the basic files of your plugin (plugin.xmlfile, .javafile for Android plugin, .hand .mfiles for iOS) in a separated folder for your plugin
  2. Provide in your plugin.xmlwhat is needed to install the plugin
  3. Install your plugin in your Cordova app : cordova plugin add /path/to/pugin
  4. Build the platforms you want to develop your plugin. cordova build androidor cordova build ios
  1. 创建你的插件(基本文件plugin.xml文件,.java文件Android插件,.h.m针对iOS文件)中分离出来的文件夹给你的插件
  2. 提供plugin.xml安装插件所需的内容
  3. 在 Cordova 应用程序中安装插件: cordova plugin add /path/to/pugin
  4. 构建您想要开发插件的平台。cordova build android或者cordova build ios

Then for each platform, you'll have to work directly on your plugin in the builded project :

然后对于每个平台,您必须直接在构建的项目中处理您的插件:

  • Android : open with Android Studio the builded Android project located in yourCordovaAppFolder/platforms/androidwith "Import project (Eclipse ADT, Gradle, etc.)"

    1. Open the Projecttool window: View > Tool Windows > Project, or ?+ 1
    2. Open your plugin class file located at : android > java > com.your.plugin > YourPlugin
  • iOS : open with Xcode the builded iOS project located in yourCordovaAppFolder/platforms/iosIn Xcode, your plugin class files are located in the Pluginsfolder

  • 安卓:与Android工作室的建造Android项目位于开yourCordovaAppFolder/platforms/android“导入项目(Eclipse的ADT,摇篮等)”

    1. 打开Project工具窗口View > Tool Windows > Project,或?+1
    2. 打开位于以下位置的插件类文件:android > java > com.your.plugin > YourPlugin
  • iOS : 使用 Xcode 打开构建的 iOS 项目yourCordovaAppFolder/platforms/ios在 Xcode 中,你的插件类文件位于Plugins文件夹中

You can then develop and test directly your plugin for each platform without the need to reinstall it again and again and again... just run the project from Android Studio/Xcode, and don't reinstall your plugin, it will erase what you have done in the project.

然后,您可以直接为每个平台开发和测试您的插件,而无需一次又一次地重新安装......只需从 Android Studio/Xcode 运行项目,不要重新安装您的插件,它会清除您拥有的内容在项目中完成

When you think your developments are done (or when you want to), replace the plugin file(s) in the original plugin folder created at very first step, with the one from the project you were developing on.

当你认为你的开发已经完成(或者你想要的时候),将第一步创建的原始插件文件夹中的插件文件替换为你正在开发的项目中的插件文件。

EDIT :

编辑 :

I've discovered that Eclipse made an update last summer, Eclipse Mars (4.5)that includes Thym(The HYbrid Mobile Tools).

我发现 Eclipse 去年夏天进行了更新,Eclipse Mars (4.5)包括Thym(混合移动工具)。

If needed they have a Github project page.

如果需要,他们有一个Github 项目页面

I've quickly tested it, it allows to create Cordova projects, easily add plugins, and run directly on desired platform (Android, iOS).

我已经快速测试了它,它允许创建 Cordova 项目,轻松添加插件,并直接在所需平台(Android、iOS)上运行。

Microsoft has also made a free version of Visual Studio for Apache Cordova, but I haven't tested it.

微软也为 Apache Cordova制作了一个免费版本的Visual Studio,但我还没有测试过。

Regarding Eclipse with Thym, my first quick conclusion is that it is practical to develop Cordova apps but won't be THE ultimate solution to develop Cordova pluginsbecause it is not oriented this way and was not made for plugins but apps making (and it is probably the same for Visual Studio for Apache Cordova).

关于带有 Thym 的 Eclipse,我的第一个快速结论是,开发 Cordova 应用程序是可行的,但不会是开发 Cordova 插件的最终解决方案,因为它不是面向这种方式的,不是为插件制作的,而是为应用程序制作而制作的(而且它是Visual Studio for Apache Cordova 可能相同)。

By the way I had some trouble to launch iOS simulator with Eclipse+Thym, so maybe it still needs improvements even if it looks already good.

顺便说一句,我在使用 Eclipse+Thym 启动 iOS 模拟器时遇到了一些麻烦,所以即使它看起来已经很好,它可能仍然需要改进。

回答by anty

The solution I tend to use follows that outlined by Niko in the accepted answer. However, I add the --link flag when adding the plugin:

我倾向于使用的解决方案遵循 Niko 在接受的答案中概述的解决方案。但是,我在添加插件时添加了 --link 标志:

cordova plugin add --link ../cordova-plugin-example

which (at least on Mac and probably Linux) creates symbolic links to the plugin directory instead of copying it. Then, editing the plugin files within the Android project in 'platforms/android' with Android Studio actually modifies the files in the plugin directory. This allows to keep the plugin directory separate (and under it's own version control). And removing and adding the plugin should - at least theoretically - not lose changes to the plugin.

它(至少在 Mac 和 Linux 上)创建指向插件目录的符号链接,而不是复制它。然后,使用Android Studio 编辑Android 项目中'platforms/android' 中的插件文件实际上是修改插件目录中的文件。这允许将插件目录分开(并在它自己的版本控制之下)。并且删除和添加插件应该 - 至少在理论上 - 不会丢失对插件的更改。

The approach should work equally for iOS plugins.

该方法应该同样适用于 iOS 插件。

Beware that I have no idea whether or how --link works on Windows machines.

请注意,我不知道 --link 是否或如何在 Windows 机器上工作。

Hint: I found this option in the help:

提示:我在帮助中找到了这个选项:

cordova help plugin

which gives:

这使:

     [...]
     [--link] ... when installing from a local path, creates a symbolic link to the plugin
                  instead of copying files. The extent to which files are linked vs copied
                  varies by platform. Useful for plugin development.
     [...]
     [--link] ... when installing from a local path, creates a symbolic link to the plugin
                  instead of copying files. The extent to which files are linked vs copied
                  varies by platform. Useful for plugin development.

回答by Bruno Ranschaert

I would like to suggest yet another workflow for the development of Cordova plugins. It approach is similar to the workflows already mentioned earlier, with the difference that I like to have an independent project to maintain + test + debug the plugin code without the need for a separate test application.

我想推荐另一个用于开发 Cordova 插件的工作流程。它的方法类似于前面已经提到的工作流,不同的是我喜欢有一个独立的项目来维护 + 测试 + 调试插件代码,而不需要单独的测试应用程序。

The outline of the workflow looks like this.

工作流程的概要如下所示。

Step 1 - Prepare the Cordova platform.
1.1 Download android release cordova-android-x.y.z.zip of the version you want from the github source site:
https://github.com/apache/cordova-android

1.2. Expand the archive, go to framework/ directory
1.3. create a local.properties file with contents: 'sdk.dir=/opt/android-sdk'
     Adjust the path so that it points to the location of the Android SDK on your machine.
1.4. Execute: 'ant jar'
The result is a cordova-x.y.z.jar

Step 2 - Create/test the plugin with IDE support.
2.1. Create an empty Android project using Android Studio.
2.2. Copy the cordova-x.x.x.jar to the app/libs directory of your project.
2.3. Create the plugin code + unit tests (you have code completion and can build the code using the unit tests).
2.4. OPTIONAL If necessary you can add a webview and test the code inside the webview as described here
https://cordova.apache.org/docs/en/latest/guide/platforms/android/webview.html

Step 3 - Package plugin files.
3.1. Create a plugin project with plugman.
3.2. Copy the files from the development project into the plugin project
3.3. Deploy and reuse.

The last step 3 could be automized, I personally never got around doing this.

最后一步 3 可以自动化,我个人从来没有这样做过。

回答by adnbsr

There is much simpler solution for Android Studio and Cordova Plugin: After adding android platform, cordova creates an android project under platforms/androidand you can create cordova plugin folder under androidproject and add plugin to project with command once. So, you do not need to use another for Java. As I said, there will be two same java files : /src and /PluginDevelopmentFolder. cordova build androiddoesn't manipulate any java file. I added fileTree to confirm:

Android Studio和Cordova插件有更简单的解决方案:添加android平台后,cordova在platforms/android下创建一个android项目,您可以在android项目下创建cordova插件文件夹,并使用命令一次将插件添加到项目中。因此,您不需要为 Java 使用另一个。正如我所说,将有两个相同的 java 文件:/src 和 /PluginDevelopmentFolder。cordova build android不操作任何 java 文件。我添加了 fileTree 来确认:

enter image description here

在此处输入图片说明

回答by webmak10

read this https://cordova.apache.org/docs/en/edge/guide_appdev_hooks_index.md.html

阅读此https://cordova.apache.org/docs/en/edge/guide_appdev_hooks_index.md.html

I use NetBeans IDE on Debian Jessie. (NetBeans is best IDE for me, use it for QT, PHP, and now Cordova. But it uses a lot of RAM.)

我在 Debian Jessie 上使用 NetBeans IDE。(NetBeans 对我来说是最好的 IDE,将它用于 QT、PHP 和现在的 Cordova。但它使用了大量 RAM。)

After I push build button, my shell script removes, installs and runs the newly built app in VirtualBox (using adb). I don't move plugins to other directory because I don't need it, but you can set your directory and manually put plugin, and then include plugin using a script.

按下构建按钮后,我的 shell 脚本会在 VirtualBox 中删除、安装并运行新构建的应用程序(使用 adb)。我不会将插件移动到其他目录,因为我不需要它,但是您可以设置您的目录并手动放置插件,然后使用脚本包含插件。