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

时间:2013-10-10 14:18:34

标签: java android cordova phonegap-plugins

我正在开发一个PhoneGap / Cordova插件,该插件应该提供一个用于发送和接收OSC消息的套接字(Open Sound Control)。为此,我想使用JavaOSC,但我不确定如何将库包含到我的项目中。

我正在使用Android Studio,我基本上按照this tutorial来设置我的项目。首先,我将原始JavaOSC类文件放在与我的OSCPlugin.class相同的目录中,并将导入声明放在我的OSCPlugin.class的to:

import com.illposed.osc;

那不起作用。

下一步,我尝试在项目的模块设置中添加maven库。我能够从maven下载jar文件并安装到/ platforms / android / libs中。在模块'android'的设置中,我可以看到'Android API 17'应该用作SDK,包括cordova-3.1.0和com.illposed.osc:javaosc-core:0.2 - 两者都被激活。我可以在Android Studio的导航器中看到cordova-3.1.0.jar以及javaosc-core-0.2.jar,其中包含com.illposed.osc。

但是,在尝试编译我的项目时,我得到:

Gradle: cannot find symbol class osc

从包含上述导入声明的OSCPlugin.class中触发

我对Java的经验很少,而Android开发的经验则更少。但是我有兴趣解决这个谜题并开始。我搜索过Java文档,但问题不仅仅在于Java,而在于Android项目的结构。

如果有人能够对这个问题有所了解,我会感激不尽。任何提示都受到高度赞赏!

2 个答案:

答案 0 :(得分:5)

对于我的一个Phonegap项目,我需要Apache Commons Net,尝试关注这些steps

...
<source-file src="src/android/xxx.jar" target-dir="libs" framework="true" />
<source-file src="src/android/MyPlugin.java" target-dir="src/com/mypackage" />
...
遗憾的是,没有成功。诀窍是将第三方库嵌入另一个插件中(遵循非常插件的结构)。将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

为简洁起见,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>

假设org.apache.commons.net目录位于您的本地git仓库中,将其添加到您的项目中就像以下一样简单:

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

答案 1 :(得分:1)

要添加外部库,基本上您只需要将jar复制到/ libs文件夹。

此处您的来源导入错误。

import用于通过指定包名后跟类名来导入类,这里只指定类名,因此错误“找不到符号类osc”是因为没有类osc。

你应该使用

  • import com.illposed.osc.*;如果要从包中导入所有类
  • 或从您要使用的包中为每个类添加导入。

如果您想使用CLI或phonegap构建来安装插件,您还必须更新plugin.xml以添加jar文件的副本。

如果你不知道,你将无法使用com.illposed.osc.ui中的类,因为他们使用swing并为jvm设计而不是android。