简体   繁体   English

Android / PhoneGap:在插件开发中使用第三方库

[英]Android/PhoneGap: Using third-party libraries in plugin-development

I'm working on a PhoneGap/Cordova plugin that's supposed to provide a socket for sending and receiving OSC messages (Open Sound Control). 我正在开发一个PhoneGap / Cordova插件,该插件应该提供一个用于发送和接收OSC消息的套接字(Open Sound Control)。 For that purpose I'd like to use JavaOSC but I'm uncertain about how to include the library into my project. 为此我想使用JavaOSC,但我不确定如何将库包含到我的项目中。

I'm using Android Studio and I've basically followed this tutorial to set up my project. 我正在使用Android Studio,我基本上按照本教程来设置我的项目。 First I placed the raw JavaOSC class-files in the same directory as my OSCPlugin.class and placed the import declarations at the to of my OSCPlugin.class: 首先,我将原始JavaOSC类文件放在与我的OSCPlugin.class相同的目录中,并将导入声明放在我的OSCPlugin.class的to:

import com.illposed.osc;

That didn't work. 那没用。

As a next step I tried to add the library from maven within the project's module settings. 作为下一步,我尝试在项目的模块设置中添加maven库。 I was able to download the jar files from maven and install put them into /platforms/android/libs. 我能够从maven下载jar文件并安装到/ platforms / android / libs中。 Within the settings for the module 'android' I can see that 'Android API 17' is supposed to be used as SDK, including cordova-3.1.0 and com.illposed.osc:javaosc-core:0.2 - both activated. 在模块'android'的设置中,我可以看到'Android API 17'应该用作SDK,包括cordova-3.1.0和com.illposed.osc:javaosc-core:0.2 - 两者都被激活。 I can see the cordova-3.1.0.jar as well as javaosc-core-0.2.jar, containing com.illposed.osc in the navigator within Android Studio. 我可以在Android Studio的导航器中看到cordova-3.1.0.jar以及javaosc-core-0.2.jar,其中包含com.illposed.osc。

However, when trying to compile my project I get: 但是,在尝试编译我的项目时,我得到:

Gradle: cannot find symbol class osc

triggered from within OSCPlugin.class that contains the above mentioned import declaration 从包含上述导入声明的OSCPlugin.class中触发

I have very little experience with Java and even less with Android development. 我对Java的经验很少,对Android开发的经验也很少。 But I'd be interested in solving this riddle and get started. 但是我有兴趣解决这个谜题并开始。 I have searched the Java docs but the problem doesn't merely lie within Java but rather within the structure of the Android project. 我搜索过Java文档,但问题不仅仅在于Java,而在于Android项目的结构。

I'd be thankful if someone could shed some light on this issue. 如果有人能对这个问题有所了解,我会感激不尽。 Any hint's highly appreciated! 任何提示都非常感谢!

For one of my Phonegap projects I needed the Apache Commons Net , trying to follow these steps : 对于我的一个Phonegap项目,我需要Apache Commons Net ,尝试按照以下步骤操作

...
<source-file src="src/android/xxx.jar" target-dir="libs" framework="true" />
<source-file src="src/android/MyPlugin.java" target-dir="src/com/mypackage" />
...

unfortunately, without success. 不幸的是,没有成功。 The trick was to embed the third-party library in another plugin (following the very plugin structure). 诀窍是将第三方库嵌入另一个插件中(遵循非常插件的结构)。 Having the org.apache.commons.net as a top level directory: 将org.apache.commons.net作为顶级目录:

    org.apache.commons.net
     +src 
       +android(this is where the .jar is located)
     +www (empty, not referencing any .js)
     +plugin.xml

For brevity, plugin.xml as follows: 为简洁起见,plugin.xml如下:

<?xml version="1.0" encoding="UTF-8"?>

<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
           id="org.apache.commons.net"
      version="0.1.0">
    <name>org.apache.commons.net</name>
    <description>org.apache.commons.net</description>
    <license>Apache License, Version 2.0</license>
    <keywords>org.apache.commons.net</keywords>

    <!-- android -->
    <platform name="android">
        <config-file target="res/xml/config.xml" parent="/*">
            <feature name="org.apache.commons.net">
                <param name="android-package" value="org.apache.commons.net"/>
            </feature>
        </config-file>

        <source-file src="src/android/commons-net-2.2.jar" target-dir="libs" framework="true" />    
     </platform>          
</plugin>

Assuming the org.apache.commons.net directory is located in your local git repo, adding it to your project is as trivial as: 假设org.apache.commons.net目录位于您的本地git仓库中,将其添加到您的项目中就像以下一样简单:

phonegap local plugin add /path/to/your/org.apache.commons.net

To add external library, basically all you have to do is copy the jar to the /libs folder. 要添加外部库,基本上您只需将jar复制到/ libs文件夹即可。

Here you have a bad import in your source. 在这里,你的来源有一个糟糕的导入。

import is used to import a class by specifying the package name followed by the class name and here you only specify the class name, so the error "cannot find symbol class osc" you are having is because there is no class osc. import用于通过指定包名后跟类名来导入类,这里只指定类名,因此错误“找不到符号类osc”是因为没有类osc。

You should use either 你应该使用其中之一

  • import com.illposed.osc.*; if you want to import all classes from the package 如果要从包中导入所有类
  • or add an import for each class from the package that you are going to use. 或者从您要使用的包中为每个类添加导入。

And if you want make the plugin installable using the CLI or phonegap build, you also have to update plugin.xml to add the copy of the jar file. 如果您想使用CLI或phonegap构建来安装插件,您还必须更新plugin.xml以添加jar文件的副本。

ps in case you don't know, you won't be able to use classes from com.illposed.osc.ui as they are using swing and designed for the jvm and not android. ps如果你不知道,你将无法使用com.illposed.osc.ui中的类,因为他们使用swing并为jvm设计而不是android。

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

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