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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19298346/
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-08-12 15:48:35  来源:igfitidea点击:

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

javaandroidcordovaphonegap-plugins

提问by 5tefan

I'm working on a PhoneGap/Cordova plugin that's supposed to provide a socket for sending and receiving OSC messages (Open Sound Control). For that purpose I'd like to use JavaOSCbut I'm uncertain about how to include the library into my project.

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

I'm using Android Studio and I've basically followed this tutorialto set up my project. 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:

我正在使用 Android Studio,并且基本上按照本教程来设置我的项目。首先,我将原始 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. I was able to download the jar files from maven and install put them into /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. 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.

作为下一步,我尝试在项目的模块设置中添加来自 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。

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. 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 的经验很少,对 Android 开发的经验更少。但我有兴趣解决这个谜语并开始。我已经搜索了 Java 文档,但问题不仅仅在于 Java,而是在于 Android 项目的结构。

I'd be thankful if someone could shed some light on this issue. Any hint's highly appreciated!

如果有人能对这个问题有所了解,我将不胜感激。任何提示的高度赞赏!

回答by Syncing

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.netdirectory is located in your local git repo, adding it to your project is as trivial as:

假设该org.apache.commons.net目录位于您的本地 git repo 中,将其添加到您的项目中非常简单:

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

回答by QuickFix

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.
  • import com.illposed.osc.*;如果要从包中导入所有类
  • 或者为您要使用的包中的每个类添加一个导入。

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 设计。