Home » Android » java – Android/PhoneGap: Using third-party libraries in plugin-development

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

Posted by: admin June 16, 2020 Leave a comment

Questions:

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 JavaOSC but I’m uncertain about how to include the library into my project.

I’m using Android Studio and I’ve basically followed this tutorial to 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:

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.

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

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.

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

How to&Answers:

For one of my Phonegap projects I needed the Apache Commons Net, trying to follow these 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" />
...

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
     +src 
       +android(this is where the .jar is located)
     +www (empty, not referencing any .js)
     +plugin.xml

For brevity, plugin.xml as follows:

<?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:

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

Answer:

To add external library, basically all you have to do is copy the jar to the /libs folder.

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.

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.

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.