Referencing External Java and Android Libraries - java

I am trying to reference a Java and an Android library (that I created) to use within my Android application built in Eclipse.
I am referencing the Java library via Properties-->Java Build Path-->Projects-->Add.
I am referencing the Android library via Properties-->Android-->Library-->Add.
Both of these are shown within the windows after adding them. I also moved the Java library to the top of the list within Properties-->Java Build Path-->Order and Export.
My project compiles fine but when I run the application on my device I get a NoClassDefFoundError from the Java library. The class is in there and should work.
Did I miss something that I need to link in order to get this running correctly?

I am referencing the Java library via Properties-->Java Build Path-->Projects-->Add.
This is incorrect. Please undo that, and then copy the JAR into libs/ within your project. If you are on the R22 or higher version of the tools, you may also need to go to "Order and Export" in your build path dialog and check "Android Private Libraries".
What you did only added the JAR to the compile path. Putting the JAR in libs/ will add it to the compile path and package the JAR's contents in your APK, so those classes are available at runtime.

Related

Automatically load libraries on project creating. Android, Eclipse

I have figured out how to obtain the effect i desire on the desktop.
(Window > Preferences > Java > Installed JREs > jre7 [Edit] > [Add External JARs])
but i cant get the same effect on android.
In the desktop project i can see the folder "JRE Sysytem Library(jre7)" contains all the JARs i referenced
This directory is not present in the android project. How do I add libraries to be automatically included in an Android project without needing to use the "R-Click Project > Properties > Java Build Path > Libraries" method OR the "R-Click JAR file > Build Path > Add to build path" method in the IDE "Eclipse"
In the desktop project i can see the folder "JRE Sysytem Library(jre7)" contains all the JARs i referenced
An Android app does not use the JRE.
How do I add libraries to be automatically included in an Android project without needing to use the "R-Click Project > Properties > Java Build Path > Libraries" method OR the "R-Click JAR file > Build Path > Add to build path" method in the IDE "Eclipse"
That is not possible, sorry. Android does not use Eclipse's "Add External JARs" regardless of whether it is manually or automatically populated. It only uses JARs in libs/ or attached Android library projects.
Eclipse doesn't provide this functionality, I believe.
You can on the other hand Select configure build path and just add the libraries.
Put the jars in the "libs" folder in your android project root folder. They will be used and exported from the build system (ant/gradle).
This link will help let you know what each folder in an android project could be used for here:
http://developer.android.com/tools/projects/index.html#ApplicationProjects
Another way would be to setup an Android Library Project which includes common dependencies of your projects, so you could add this library project as dependency in the Project->Properties->Android - Section.
Since nobody gave me an answer to my question (although they all of you did provide useful info and i think you for that) I will post the solution i found (but have not yet implemented)
I am using libgdx to create my applications so after looking and searching for a way to have eclipse automatically include libs when generating an android project, i realized that the answer was obvious (but not easy to do)...
i downloaded the libgdx source from github, learned how to use command line git and ant, and followed their instructions to get the source running. i now have the setup ui project working out of source. and am beginning my day by trying to get the libs i use included in the setup...
i know its possible and will edit this post with information on if/how i managed to accomplish this lofty goal

Exporting android project to jar (that includes other jars)

I've written a Android Game Engine library and would like to export it to a jar file. I've tried to do this with the following:
export > Jar File > Finish
As soon as I add the jar to the build path of another Android project, I get a little box in my code saying:
The type foo cannot be resolved. It is indirectly
referenced from required .class files
I believe this is because my Game Engine library includes other libraries in forms of jars.
How can I add those jar files to my Game Engine jar file?
You can decompile the other jar file using JDui, take what you need, and add it to your library. It all depends on how big is what you need..
Also, check whether you are using proguard in the export. You would have to be careful on how to configure it.
I assuming you are using Eclipse. You also need to use Properties->Java Build Path->Libraries->Add Jar button to add your jar file to the target project

Android Could not find class, Nested jars issues?

I have two projects, the first is a library (Marked as "isLibrary" in the eclipse properties -> android settings) and the second is an android application. The android application uses the library and everything works ok.
I am now trying to include a jar file in the library and access it's functionality in a class (we will call it ClassA) also found again in the library. Now in the android application I create a new instance of the ClassA and tell it to do some stuff that will use functions provided by the jar I added in the library. I end up with this error:
"Android could no find class 'com.blah.blah.blah' etc."
I checked the .classpath in the library and it is exporting the jar properly. What could I be missing?
Thanks in advance.
I think that while you can use any "lib" folder you want in eclipse, and add it to the classpath, I'm pretty sure for Android to package things correctly the jar should be placed in a folder called libs.

How to add a library to an android project?

I have downloaded a messaging framework library from eneter.net. In eclipse I have imported the library to the /libs folder. I right clicked on the library and then added it to the build path. I know that the code is right, but when I compile it and try to run it, it says that that it can't find the classes. Odd thing is when I open up the apk in a zipping program, the library wasn't added to the apk at compile time. Does anyone know why this would be?
Try right-clicking your project in Eclipse's project explorer, then hit Properties. Go to the tab labeled Java Build Path and hit Add Library... (or if it is a jar file, Add JARs... if you already added the Jar to your project and Add External JARs... if it is not yet within the project). From there, browse for the library you wish to add and add it to the project. Re-compile (maybe even Project->Clean) and try running it, and see if the library gets added now.
I right clicked on the library and then added it to the build path
That is not necessary, as of the R17 version of the ADT plugin in Eclipse. Moreover, it may cause problems if you do it.
Does anyone know why this would be?
Possibly because there is a typo in your directory name, such that it is not named libs/. By adding it manually to your build path, you are compiling against it, but the build tools are not adding it to the APK.
Is the library in the form of a complete Android project? This is then used with another Android project by going to the Properties of that project, selecting the Android tab, and then finding and selecting that library in the lower window.

android - linked-resources when building from command line

During development, our Android app is built using Eclipse (ADT). We use an eclipse feature called Linked Resources, which allows eclipse to compile files outside of the source tree.
It works well on eclipse, but when trying to compile from the command line (using android update project and ant), ant/javac won't find the linked resources (as they are defined in the eclipse project).
How can I make it work? Do I need to manually add the additional source dir to build.xml (which was auto-generated by android)? Is there an android setting I should use?
You could put the paths of the extra source code in the source.dir line of ant.properties in your project directory. Something like this:
source.dir=src;../ExtraSource;../MoreSource/java
See http://code.google.com/p/android/issues/detail?id=21205 for more discussion. Apparently they took this feature out at some point but it looks like they have put it back in since then.
Are you using referencing library? If so, you need to tell ant where it can find the referencing library (typically in project.properties)
android.library.reference.1=/pathToReferenceLibrary
A referencing library is where you want to keep all classes and resouces that you want to reuse between different android projects.

Categories

Resources