Jar file versus reference to the Android Library - android

You will really make my day by explaining the following nuissance.
L is a library. A is an application using L, CA is a class, declared in A extending a class CL in the library L.
Untill recently, I could just place L.jar in the lib folder of A project. But I purchased a new hard disk, reset Eclipse, ADT plugin and SDK, then transferred my project to the new location. This is when it started.
If I place in A a reference to the project L (via Project/Properties/Android/Library), everything still works all right. But with a jar file, it gives a "No Class Defintion Found" error for CA class, even though it belongs to the same class as the calling method!
To avoid some sceptical remarks:
L has no resources in it.
Compiler version is 1.6
I cleaned both projects millions of times.
Both projects are compiled with same API version.
No obfuscation was used and I checked that CL class is really present in L.jar.
Tried placing L.jar in build path, as topmost, then as bottommost - no help.
Thank you for anticipated help.

It was rather silly of me: in "Order and Export" panel of BuildPath configuration I had Android Dependencies ticked, but Android Provite Libraries unticked.
What I found out is that libraries linked through Android properties go to Android Dependencies, whereas the ones in lib folder are considered as Android Private. This explains why the first were included, while second weren't.
The most annoying thing is that Eclipse doesn't complain, the build seems to be all right, and only at the execution stage the error comes out. As a C programmer I am not quite used to that :)

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.

Android: NoClassDefFound error

My setup is as follows:
I have an android project A, and a java project B. Project A uses some files from Project B.
My development environment is eclipse, and I have included Project B in the build path,
as well as marked it for 'Order and export'.
The project builds fine, but at runtime I get a
java.lang.NoClassDefFound error, when I am trying to reference a class from Project B.
How can I resolve this?
I am using eclipse Juno, and ADT 20.0.3.
Thanks.
As you talk about having updated your Eclipse and your ADT, maybe you also changed your environment to Java 7?
If that is the case, the answer would be:
Switch back to Java 1.6.x, as Java 7 is not (yet?) supported by Android.
You need to change the "compiler compliance level" under:
Window - Preferences -- Java - Compiler - Compiler compliance level
Hope that works, at least it did for me:
NoClassDefFoundError when referencing from an Android-Project to a standard Java-Project
You can also try to export the java classes you need in a jar, and include that jar in your Android project.
I had the same problem when I was trying to reference another class withing the same project itself.In my case, the problem was solved when I rechecked the android dependencies. I added two jar files for a single required file. There was a need of android-support-v4.jar file in my project. I added the library twice by mistake. The error was resolved when I deleted one of them.
For me this was happening when using the AIDE development environment. It has happened a couple of times over the past year.
When the Java diamond operator is used to specify a function's generic type the program will compile, but the Dalvic engine crashes with the NoClassDefFound exception.
To fix this I had to explicitly define the generic type.
By the way, this crash can happen in a completely different place to where the problem actually is. For example, we have classes A and B. The bad code is at A.myMethod(). The crash will happen when class B attempts to access any part of class A, even something unrelated such as A.myOther() method.
This is because of how Java/Dalvic loads classes at runtime only when first accessed.

Android: resources unavailable from library project(s) app -> liba -> libb

All,
I am attempting to use an existing open-source project (we'll call it A) within my app as a library. Project A also comes with its own library project (B). Project A compiles and runs just fine; however, when I try to use A within my app (I checked the 'Is Library' within A and added A to my app) I get several errors that "No resource identifier found for attribute 'X'in package..." These particular attributes exist within B. I even checked that they were added to R within A, and in fact, they were.
My question: why doesn't my app see that these resources exist?
I've searched all over and found only one situation like mine:
Android library not pulling its resources through to other projects
but I do not see that any of my resources are named the same.
I'm using Eclipse with ADT v20.0
I appreciate any help!
EDIT: If it's helpful, the open-source app I'm trying use is AnySoftKeyboard (ASK). I've also tried creating a new project and adding ASK as a library -- no dice.
EDIT2: Part of making ASK a library required converting several switch statements to if-else statements because R Ids are not declared as final for libraries.
The errors are apparently due to the usage of custom attributes in the AnySoftKeyboard project that you converted to a library. I found that a solution is recently added into ADT (on Rev 17), mentioned in this answer.
As instructed, I tried replacing all instances of
"http://schemas.android.com/apk/res/com.menny.android.anysoftkeyboard"
with
"http://schemas.android.com/apk/res-auto"
in the newly converted library project and was able to build and run an app which includes the project (didn't really test any functionality though).
Hope this helps.

Regarding NoClassDefFoundError

I have used the isEmpty and isBlank methods of apache's StringUtils Class. These methods worked in a fine manner, after couple of weeks suddenly i got the error
java.lang.NoClassDefFoundError: org.apache.commons.lang3.StringUtils
I was confused why this error happened suddenly.It would be fine if anyone help me to understand this suddenly happened.
People Saying that apache might have changed the definitionin their JAR file, is it a right one,i really confused why this suddenly happened , i removed the old jar from my build path and downloaded the same version of New Jar and added it to Build path, then this issue is resloved.
It is happening in the Market Code , there's no interaction with ADT and market code
so really confused on this issue?
This occurred to me too - after updating the Android SDK Tools. In current version (19), what you need to do is:
make libs/ folder in project's root directory, and put all jars there (folder's name is fixed "libs")
after this, go to project properties --> Java Build Path --> Libraries, and remove all custom jars you previously added
NoClassDefFoundError says NoClassDefFoundError is Thrown if the JVM or a ClassLoader instance tries to load in the definition of a class and no definition of the class could be found.
Maybe startup script is overriding Classpath environment variable.

Android unable to resolve classes from external jars

I came across this after updating my SDK tools from revision 15 to 17, seems like there are quite some changes under the hood that cost me a long evening. Figured I share this so I hopefully save somebody else's nerves:
After updating my project would still compile and build, but at runtime throw all kinds of errors and exceptions NoClassDefFoundError, ClassDefNotFoundException or the dalvik vm complaining in logcat that it was not able to resolve some classes or super classes. Turned out that it was not able to load classes that were located in external jar files (which were on the classpath).
SOLUTION / ANSWER: (unfortunately stackoverflow does not let me answer this due to lacking reputation)
Turns out that since revision 17 of the sdk tools, only external libraries that are marked to be exported in java will be part of the apk at runtime. So the solution is simple:
Go to Java Build Path -> Order and Export and check the box next to all your jars that you need at runtime (should usually be all of them).
I'm not sure whether I should have done that before, but with revision 15 and earlier this was never an issue.
Just add the library Jar files to you apk. Its very easy to do it.
Right Click on the project and click on properties.
Click on the Java Build Path and move to the Order and Export Tab.
Click on Select All on the right side. Click on ok.
Re-build the application and try running the application.
Now it should work. Lemme know if it worked for you.. ?

Categories

Resources