4

I have an Android application project (A) and an Android library project (L). A uses L as a library.

L, the library project, uses a few external libraries already: Apache Commons Codec 1.7, Apache Commons Lang3 3.1, the Android Support library V4, and ActionBarSherlock 4.0, referenced as a library project. All of these libraries, and the code that calls them, work perfectly fine, and have been working fine for a while.

However, I recently added one more library: Apache Commons IO 2.4. Now my application crashes any time the library code (in L) tries to call a method in Commons IO.

I've added the IO library in exactly the same way as I've added every other external library: build path

When my application first starts I get:

11-01 12:32:14.243: I/dalvikvm(3815): Could not find method org.apache.commons.io.FileUtils.copyInputStreamToFile, referenced from method au.com.grdc.alib.helpers.LibraryCommon.download
11-01 12:32:14.243: W/dalvikvm(3815): VFY: unable to resolve static method 7039: Lorg/apache/commons/io/FileUtils;.copyInputStreamToFile (Ljava/io/InputStream;Ljava/io/File;)V

Later on I get:

11-01 14:50:48.913: E/AndroidRuntime(10030): FATAL EXCEPTION: AsyncTask #1
11-01 14:50:48.913: E/AndroidRuntime(10030): java.lang.RuntimeException: An error occured while executing doInBackground()
...
11-01 12:35:10.268: E/AndroidRuntime(4493): Caused by: java.lang.NoClassDefFoundError: org.apache.commons.io.FileUtils
11-01 12:35:10.268: E/AndroidRuntime(4493):     at au.com.grdc.alib.helpers.LibraryCommon.download(LibraryCommon.java:89)

I saw that it was recommended to rename my "lib" folder to "libs", which I've done — this hasn't fixed the problem.

I also saw recommendations to mark external libraries to be exported, which I've also done, and which also hasn't fixed the problem.

George
  • 2,110
  • 5
  • 26
  • 57
  • 2
    are you exporting this library? check the tab to the right. – doublesharp Nov 01 '12 at 04:14
  • @doublesharp yep, all of the libraries are marked to be exported. – George Nov 01 '12 at 04:16
  • 1
    take a look at this link .it will help you. – Senthil Nov 01 '12 at 04:31
  • @vsk Thanks for the link, but it didn't help - all of my libraries are added with relative paths, not absolute. See http://pastebin.com/raw.php?i=JuwzrW9M for a copy of my .classpath file. – George Nov 01 '12 at 04:35
  • 1
    @George is the commons-io jar listed in your Android Dependencies under the project? – doublesharp Nov 01 '12 at 04:44
  • @doublesharp I'm not seeing any "Android Dependencies", but commons-io-2.4.jar is listed under "Referenced Libraries" along with lang, codec and android support. See http://i.imgur.com/5Ral0.png for a screenshot. I'm thinking there must be a problem specific to this library, because every other library I've added has worked fine? – George Nov 01 '12 at 05:06
  • Can you provide a screenshot of you application project (not the library project) order and export configuration too? BTW, what SDK/ADT version are you using? – yorkw Nov 05 '12 at 21:43
  • @yorkw: the screenshot is at http://i.imgur.com/uueQL.png; I'm using ADT 15 (15.0.0.v201110251216-213216) with SDK rev 16. I'm about to upgrade the entire installation as recmommended by forgemo's answer. – George Nov 07 '12 at 00:48

2 Answers2

5
  1. Update your eclipse-android-plugin and android-tools (They have changed how libs are handled a while ago)
  2. Make sure, that you have in your project the 'libs' (not 'lib') folder.
  3. Put all your library jars in the 'libs' folder.
  4. Make sure that your library jars in the 'libs' folder are NOT added to the build path like you would in plain java projects. Android will add them automatically. If you look at Project > Properties > Java Build Path > Libraries:
    1. Ensure that there is no single one of your library jars shown on the top-level (remove them otherwise)
    2. Check that there is an auto-created top-level dependency group called 'Android Dependencies'.
    3. Check that all your jars from the libs folder have been automatically added to the 'Android Dependencies' group

Do the above steps for both, (A) and (L).

Also ensure that (A) has no plain-java-style project dependency on (L). Project > Properties > Java Build Path > Projects for (A) should not list (L), remove otherwise.

Ensure that Project > Properties > Android for (A) lists in the Library section project (L), add otherwise.

forgemo
  • 4,634
  • 5
  • 22
  • 25
  • If you need more detailed help with one of the steps, let me know. – forgemo Nov 05 '12 at 00:15
  • Well, upgrading my Eclipse / ADT / SDK on it's own seemed to fix things (I followed all of your other suggestions afterwards of course). I'm still confused about why this particular library didn't work in my old environment when all of the other Apache Commons libraries did? Thanks for your answer. – George Nov 08 '12 at 00:07
0

It may be that org.apache.commons.io.FileUtils exists on several jars and the JVM cannot resolve which one to use.. Can you cross check that?

Ashish
  • 510
  • 3
  • 14
  • I've only added the Commons IO Jar once, and I doubt any other Jar would include the org.apache.commons.io package. Is there an easy way to double check? – George Nov 01 '12 at 05:22
  • Press Ctrl+Shift+T and type the fully qualified class name, i.e. org.apache.commons.io.FileUtils You will obtain a result showing where that class exist in your classpath (under which jar) – Ashish Nov 01 '12 at 10:34
  • Thanks @Ashish - I did that and the only jar showing up is libs/commons-io-2.4.jar. Screenshot at http://i.imgur.com/TZc43.png. – George Nov 01 '12 at 23:02