Android - cannot use external library - android

I was asked to make a project work in Android and I am a complete noob in this aspect and, well... things are not working...
I have created a little code in Java which uses libraries - this code works perfectly. I am now trying to make this code work on Android but I have problems...
It seems I cannot use any element from the libraries I imported to my Android project. The project loads on the phone perfectly fine when no instance of the library is created, but when I make use of the library the app crashed and I get errors.
http://i.imgur.com/OILHQ.jpg
Here is what the project package looks like
http://i.imgur.com/HQEX9.jpg
The part with the arrow is what I think makes the program crashed. When I remove this line, everything works fine.
I checked online about problems with Android and external libraries but I could not understand everything... Could you help me pinpoint exactly what is wrong and how to solve this?
Thanks!

I got this issue when I first started android development.
Key to this is to the external library seems to require them being another android project itself, instead of java project. After creating this android project, right click on the "external project" and choose properties. Under Android Tab, there should be something to check to denote that it is a library instead of application. After doing this, the linking is quite similar to how we link normal java projects to external java library

If you are using eclipse, than just create a "libs" folder in project root and drop your external libraries there.
Alternatively you Right-click on the jar file > build path > add to build path

Related

java.lang.NoClassDefFoundError with external jars in Android project

I've been searching the internet for two days now, and I feel like I've tried everything.
Please let me know if any code snippets might be helpful in finding a solution.
I've created an android application with the standard login activity. In the doInBackground()-method, I invoke a static method from another external class. Since this method will be invoked once the submit button is clicked, the error occurs during runtime. The class is included in my file, and there are no compilation errors. Whenever I tap/click on the Submit-button, I get a java.lang.NoClassDefFoundError referring to a class (in my external jar) that is used and properly included in my external file I refer to in the doInBackground()-method.
All my external libraries are in the 'libs'-folder. I've tried adding them to the build path manually instead and checking the boxes next to them in the 'order and export'-tab instead of the checkbox next to the 'Android Private Libraries' entry. I've tried putting the 'gen'-directory above the 'src'-directory, which didn't change anything either. Restarting eclipse, reimporting the project as a whole, playing around with the order of the external libraries in the build path, fixing project properties and cleaning the project over and over, nothing helped. I've checked this
http://javarevisited.blogspot.de/2011/06/noclassdeffounderror-exception-in.html
and tried every solution, but none of them worked for me. I ad a similar problem with a library before, and I could solve it by changing some build path settings and restarting Eclipse. However, this just doesn't work for me any more. I hope anybody has another solution or an idea, I have no clue what's wrong...
Thank you all in advance!
Yeehaw
Edit:
Despite creating a whole new project and adding my previous files to this new project, I still had the same problem in the new project. I think I figured out where this error came from: When I added my external library, I still had dependencies from the just added .jar to other jars. After adding those as well, the compiler said I needed the java.rmi.Remote class, which is in Java's JRE system library (rt.jar). Adding the whole jre library to my project made the compiler error disappear, the runtime error, however, still persisted.
After some more research I found out that my external library is probably not compatible with the android runtime environment, which means that the jar's .class-files don't have an equivalent .dex-representation which is neededfor the Dalvik virtualmachine on android powered devices. For many java jar's, especially those designed for use with the android platform, those problems don't occur.
Please correct me if I'm wrong, but this explanation seemed pretty plausible to me. Too bad, since this is the only way to currently access our database programmatically.
Make sure you've updated to the latest version of ADT Eclipse plugin and SDK Tools and also make sure "Android Dependencies" is checked in your project properties.

Adding source files to android

Ok, i've wasted an hour trying to figure it out, even though it's something really simple.
I've downloaded some code from github. I'd like to use this in my android application. How do I go about adding it? It's not a jar file, and i tried drag dropping it and linking the files, and it didn't work. Importing and refreshing / cleaning the project didn't work.
I'm not sure where I'm going wrong. Any help is greatly appreciated!
If this is a project you want to import in eclipse, right click on eclipse left pane (the project list), import, android project from files, select the repository folder.
If this is a library you want to use in you app, there is another step : go to the properties of your project, android tab and in the library section click on add, then select the library project.
By design, android code that contains layout resources (for example ActionBar Sherlock) can not be embedded in a jar. So you have to use it as a library project. It is very convenient though, since it allows you to look at or modify the library code very quickly.
You may also need to let Eclipse know that your library project is a library and not an app. To do so, in the same Properties/Android screen you just have to check the "is a library" checkbox.
Reference on that topic : official documentation
In Eclipse:
File->New->Other->"Android Project from exiting Code"

xmlvm - import errors compat libs in xcode

Does anyone know where the android compat libary is
located in xmlvm so I could copy it by myself to the project folder because this is
what I guess is missing, also would be provided by target=android-on-iphone but as mentioned below this target isn't available in the current build.
I had absolutely no problem installing xmlvm and getting in touch with the
demo/samples also all the demos worked perfectly for me.
But now im stuck at the last 4 errors in xcode, i migrated my android project via --
skeleton=android:migrate then cross-compiled it via --target=iphone and opened
up the project in Xcode. after few project setup changes the errors went down from
72 to 4 errors ;)
however the errors are importing file not found errors of compat libs, so this
should somehow get to be solved i think...
is it because --target=android-on-iphone wasnt used by me because this option
will copy the android compat libary to the project folder.. well i tried to use this
target mentioned in 2 documentations of xmlvm but in the actual build of xmlvm
this target however does not exist..
How to solve these errors?
Screenshots:
http://img526.imageshack.us/img526/8957/bildschirmfoto20120620u.png
http://imageshack.us/f/138/bildschirmfoto20120620u.png/
From what I can see, it looks like you are using part of the Android API which is not yet implemented.
Unfortunately it seems that you either need to rewrite your application or implement the API yourself.

Using Android library in eclipse and jumping to class files instead of source file that is within eclipse workspace

I found this bug http://code.google.com/p/android/issues/detail?id=20731 that is resolved but it did not fix this exactly, but I see in comments that this is mentioned there as problem. Is there some way in eclipse to setup dependencies in project somehow , that I will be able to normally jump to source when I am in eclipse editor ? I tried numerous ways to solve this.
My setup is:
Android project that has Android Library setup in Android>Library
Android Library project that is within workspace and have dependency on Java library project
Java Library project that is within workspace and is opened as class file instead of source !!!
Including java library project through build path normally (this is causing build to fail)
[2012-03-13 11:07:21 - Taxicar Driver Android] Dx
UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.IllegalArgumentException: already added: Lrensoft/java/collections/GenericIterator;
[2012-03-13 11:07:21 - Taxicar Driver Android] Dx at com.android.dx.dex.file.ClassDefsSection.add(ClassDefsSection.java:123)
[2012-03-13 11:07:21 - Taxicar Driver Android] Dx at com.android.dx.dex.file.DexFile.add(DexFile.java:163)
Not including , project runs ok , but editor is jumping to library class files.
Note that I have to include Android Library project through build path , even though it runs ok without it (it is enough to set is as dependent android library) , but when I remove it I get also those files as class files :) .
So I think that right way is not including projects through build path at all but how then setup project to jump to source ?
I had a similar problem - Open Declaration command would bring up the .class file instead of the .java file. My problem however was occurring when I would use Open Declaration in my Android app project to goto some source in my Android Library project. By following some instructions from a comment on this blog post Eclipse now takes me to the .java file (instead of the .class file) when I use the Open Declaration command.
The instructions are:
Right click on your project, select Properties
Select Java Build Path page
Select Order and Export tab
Select each library project your project depends on, and use Top or Up to move it above the projects outputs. Eg. move all library projects to the top.
That fixed the goto .java instead of .class file issue for me.
(Unfortunately though, after making these changes, this issue appears (at least it did for me): Could not find Library.apk!)
I then setup my workspace as you describe in your question (Android App depends on Android Library project depends on Java Library project) I find that Open Declaration is always taking me to .java files, so it all appears to be working ok in this respect.
Refactoring
I tried to refactor a method which is defined in a class in the Android Library. A popup with a "problem" of
Binary references to a refactored element have been found. They will
not be updated, which may lead to problems if you proceed.
However after pressing Continue the refactoring took place without any issues.
I then tried to refactor a method which is defined in a class in the Java Library. I didn't get any popups this time, and the refactoring worked.
The java library aspect might just work these days because the bugs you guys mentioned have been fixed... (I am using Eclipse 4.2.0 with ADT 20.0.3). I thought I would add this answer however incase it helps someone out with issues of Open Declaration jumping to .class instead of .java file.
I found solution. I have to create Android library from my java library as well and then include it as android library as well as Java library , Then I have to reorder build path export items so Android "Library Projectsā€¯ are last . And well not much work, Now even rename refactoring works , with some warning but works :) , Well I am happy developer again.

Referencing Android custom library cause runtime class not found error in Application

I am working on a custom Android library. I start off with a java project (in Eclipse) with another UI testing Android project.
In my UI testing project, I reference the library project in build path, and everything is good and I can test the library codes in the UI.
Later on, I decided I need to have the 'Gen' code (for custom styles attributes), so I try to change my library to an Android project (via Android-tools -> convert.. ), and disasters happens! After fixing up all the compile time problems, it finally 'let' me start the UI testing application, and bangs! When I try to create any objects from the library class, it said (in the Log) that Class not found.
Two questions:
Any idea how to fix this? I tried but seems it is for referencing another .apk that need to install separately.
If I just keep it to be java project (not android) for my library, can I use the resource generator? and how?
PS: the dependency (my library) is installed before the UI test application, according to the Console of Eclipse said.
Update: I tried to compile the library project to jar and reference it from the UI testing, and in this way, it works. But I don't want to use this approach since it is very slow in terms of testing.
for those who are doing same mistake like me
when library project is an android project
got to
properties > android > click add button at bottom of the popup inside
library section
and add project, instead of
properties > java build path > project > add
later approach is for pure java projects only .
Finally I found the answer to my own question. This is actually mentioned in official documentation.

Categories

Resources