Referencing other jar files when building a plugin jar?

I’m currently building a plugin using Enterprise.

Originally when I was coding it I put the jar files for the client’s APIs into the projects libs folder. This works fine when building the app directly, but now that I want to build the plugin itself to submit to Corona I’ve hit a snag.

I noticed that when sunning the build.plugin.sh script, the libs folder gets cleaned, so I’ve added my 2 jar files to this section:

\<macrodef name="copy-libs"\> &nbsp; &nbsp; &nbsp; &nbsp; \<sequential\> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \<mkdir dir="libs" /\> \<copy file="JNLua.jar" todir="libs" /\>

  <copy file=“Corona.jar” todir=“libs” />

            <copy file=“mySDK1.jar” todir=“libs” />

            <copy file=“mySDK2.jar” todir=“libs” />
 </sequential>
</macrodef>

          

(I’ve trimmed the path names down to make them more readable here, in my actual code they do look inside the  correct Corona Enterprise directory).

Ignoring all of the other Corona/Lua/Java imports, this is how the start of my myLuaLoader currently looks, with imports referencing the external jar files:

package plugin.myLibrary; import com.clientCompany.myLibrary.myservice.\*; import com.clientCompany.myLibrary.myservice2.\*;

However once I build the plugin, all of the imports for the classes in those jar files do not work any more.

Do I need to change the import class names? Or have I missed something else? I assumed that I could just remove the 2 SDK jars that I was using and replace them with the new plugin jar, but maybe I need to do something else?

Edit:

Just to make it easier to solve, the error message that I get when I start up the app is:

java.lang.RuntimeException: java.lang.NoClassDefFoundError: plugin.myLibrary.LuaLoader$mySDKClass

with the stack trace going right back to require(“plugin.myLibrary”) in main.lua  (“mySDKClass” implements a class that is imported from the client’s SDK). 

I admit, the existing plugin template that we’ve included with Corona Enterprise is a bit confusing.  It would have actually been better if we split the template into 2 Android project directories.  1 for building the plugin JAR file and the other project for testing it via an Android application project.

You actually shouldn’t include JAR files in the plugin project’s “libs” directory.  The reason is because all of its contained classes would be merged into your plugin’s JAR file.  This is definitely not a good idea if its a commonly used JAR file, such as Google’s Android “support” library, because it will cause conflicts and build errors with other plugins that require the same JAR file’s classes.  The main point being that your plugin JAR file should only contain your code/classes and not 3rd party classes.

So, I recommend that you do the following:

  1. Leave you plugin project’s “libs” directory empty.

  2. Set up a separate Android application project directory.  (For example, create a copy of one of our sample projects included with Corona Enterprise.)

  3. Copy your plugin’s JAR file dependency to that application project’s “libs” directory.

  4. After building your plugin’s JAR file, copy and paste that JAR file to your application project’s “libs” directory too.

  5. Building that application project.  It will include both JAR files and work successfully.

When submitting the final plugin to us, you’ll need to include all 3rd party JAR files in the same directory as your plugin’s JAR file.  The directory structure of the plugin template is documented via the link below.

   http://docs.coronalabs.com/daily/native/plugin/submission.html#template

So, your JAR files will go in a directory like the one below, but probably under a different Corona build #…

   ./plugins/2013.1137/android/

I hope this helps!

Ah, so if my plugin requires other jars I just keep them as separate jars and submit all of them to you?

That’s where I was confused, I assumed it all needed to be bundled up into one neat jar file.

You “should” keep them as separate JAR files.  At least if they’re commonly used ones that other plugins might use.  If your plugin requires JAR files that you think only your plugin will use, then it’s okay to add them to your plugin project’s “libs” directory… provided that its license allows it. In which case, that “-pre-clean” target in your “build.plugin.xml” file needs to be changed to only delete the “Corona.jar” and “JNLua.jar” files.

I admit, the existing plugin template that we’ve included with Corona Enterprise is a bit confusing.  It would have actually been better if we split the template into 2 Android project directories.  1 for building the plugin JAR file and the other project for testing it via an Android application project.

You actually shouldn’t include JAR files in the plugin project’s “libs” directory.  The reason is because all of its contained classes would be merged into your plugin’s JAR file.  This is definitely not a good idea if its a commonly used JAR file, such as Google’s Android “support” library, because it will cause conflicts and build errors with other plugins that require the same JAR file’s classes.  The main point being that your plugin JAR file should only contain your code/classes and not 3rd party classes.

So, I recommend that you do the following:

  1. Leave you plugin project’s “libs” directory empty.

  2. Set up a separate Android application project directory.  (For example, create a copy of one of our sample projects included with Corona Enterprise.)

  3. Copy your plugin’s JAR file dependency to that application project’s “libs” directory.

  4. After building your plugin’s JAR file, copy and paste that JAR file to your application project’s “libs” directory too.

  5. Building that application project.  It will include both JAR files and work successfully.

When submitting the final plugin to us, you’ll need to include all 3rd party JAR files in the same directory as your plugin’s JAR file.  The directory structure of the plugin template is documented via the link below.

   http://docs.coronalabs.com/daily/native/plugin/submission.html#template

So, your JAR files will go in a directory like the one below, but probably under a different Corona build #…

   ./plugins/2013.1137/android/

I hope this helps!

Ah, so if my plugin requires other jars I just keep them as separate jars and submit all of them to you?

That’s where I was confused, I assumed it all needed to be bundled up into one neat jar file.

You “should” keep them as separate JAR files.  At least if they’re commonly used ones that other plugins might use.  If your plugin requires JAR files that you think only your plugin will use, then it’s okay to add them to your plugin project’s “libs” directory… provided that its license allows it. In which case, that “-pre-clean” target in your “build.plugin.xml” file needs to be changed to only delete the “Corona.jar” and “JNLua.jar” files.