I've been researching for many hours about a possible solution to this but whatever I try it just fails. Here's the explained situation:
I have this library project on Android Studio which generates the app-debug.aar file.
Then I add this new module in the implementing project, following the wizard to import .JAR/.AARs I pick the .aar and update the dependency for :app in the Project Structure.
Once the project is cleaned (even using ./gradlew clean--Mac OS X) I can make references of this class and its methods in any of the activities.
Now at runtime, once built and installed on the real devices the app will crash throwing the aforementioned Runtime Exception.
Note: I'm importing the .aar because I would need to protect the code and it has resources so a .jar is out of the question. And if I import the library module (with source and all instead of an.aar) then the app has no issues at runtime.
I've tried everything and if anyone can throw some light on what this could be, it'll be highly appreciated.
The class extends ViewGroup and is instantiated at runtime in case that implies anything. The code was given to me so major changes might not be possible unless it's absolutely necessary.
Thanks beforehand to all!
Armando
For those, who are still looking for the solution, following two options worked for me to solve the exact same problem as OP has mentioned.
Include the (problematic) library dependency in the target module as well, i-e the in my case i was including the protobuf-lite as a dependency to my library module but getting noclassdefFound error when lib imported as .aar in app module. As a workaround, i added the protobuf-lite dependency to my app module too and it worked like charm.
Second option that worked for me that instead of adding the gradle dependency `compile 'com.google.protobuf:protobuf-lite:3.0.1', i downloaded the protobuf-jar from maven and added manually to the libs of my library module, and the problem got solved.
Don't know what's wrong with the gradle plugin, but hope it helps someone else looking for the same problem.
The NoClassDefFound error has actually happening to a backward-compatibility library being used by this class I mentioned on the question ("Class A"). No details were given other than Class A couldn't be found and later on found out that another class had a similar issue BUT was pointing at that compat lib with the same exception and I noticed that Class A was also calling its methods and implementing its callbacks. By bumping up the min version (to use the native API) I could overcome that issue. It was indeed a problem of including that backward-comp library in the .aar or so it seems. Should I understand this issue a lot better I'll update this "answer."
Related
This is my problem: I'm new at Java, I started a Maven project because I needed to use a lib that requierd that. Then I no longer needed it but keep going with the Maven project. Now I'm trying to build it but the .jar does not run the project when double clicked.
Since I don't know Maven very well (I found this but didn't understand it), I thought that the easiest way was to convert the proyect into a "normal" Java App project. So I copied all files with classes from the Maven project folder to a new Java App project folder manually.
It opens fine, but I got this error when retrieving an ArrayList:
Exception in thread "pool-1-thread-5" java.lang.ClassCastException: java.util.concurrent.CopyOnWriteArrayList cannot be cast to java.util.ArrayList
Since this doesn't happen in the Maven project I assume that it happens due to my archaic method to copy the classes...
So, is there a way to convert my Maven Project into a normal Java Project? I have found a lot of topics about doing the opposite but anything useful about what I need (only this but it is not what I need).
Thanks!
You can try closing the project on NetBeans and importing a project from existing file system.
However I feel that is not the ideal way to do it, but it should allow you to run it as you require.
Disabling the Maven nature is also viable.
According to Java Docs, java.lang.ClassCastException is
thrown to indicate that the code has attempted to cast an Object to a subclass of which it is not an instance.
There might be a chance that you are getting that exception cause you are missing some library or class which implements the same interface or allows you to convert the object to a given type.
I recently asked a very similar question, but was unsure how to phrase it. Having received some feedback and researched some of the potential solutions, I'm now asking again with what I believe to be a better explanation of the issue.
My Android App uses my Library (parentLib), this library is built referencing methods from another library (childLib). Therefore, the App itself uses parentLib (A .jar library), which in turn uses childLib (Another .jar library).
The problem here is that when the app attempts to use anything that involves the nested library (childLib), it receives a NoClassDefFoundError, which crashes the App. I believe that this issue is due to the App being unable to resolve the nested dependency.
I can't really use Android library projects, as I need the source code to be as obfuscated and difficult to understand as possible. I have also tried creating these libraries using Android Studio, only to sadly receive no better outcome.
My options (I think) are:
Combine the Libraries
I could potentially combine childLib and parentLib into a single .jar library, seperating them out into different packages. This would be difficult though, as in practise there are around 6 nested libraries for my intended App.
Insist the Client use multiple libraries
Instead of saying to the client, "Here's one simple library (parentLib) for you to use", I could instead say, "Here's 7 different libraries for you to import (parentLib, childLib, etc.), otherwise nothing will work". Not exactly professional!
Other Options
I would welcome any other suggestions anybody has!
Thanks in advance.
The NoClassDefFoundError means that the the class was present during compilation, but not available in the classpath when trying to execute it.
So, you need to tell gradle to compile those libraries and package them inside the apk.
Copy all the library jars you need to the libs folder in the root of you project. Then, edit the gradle.build file and add the following:
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
This will cause all the required libraries to be added to your apk. This way, the classes you need will be present in the classpath when you execute the code that needs them.
Resolved by moving to Android Studio (where such issues are not present).
I'm using ORMLite on our Android app, and I've previously set up an ormlite_config.txt as described in the documentation
http://ormlite.com/docs/table-config
I've now updated my classes, so I need to regenerate the file, but I can no longer get my DatabaseConfigUtil to run in Eclipse. I've set the Run Configuration as described in the documentation, choosing the local JRE and removing the Android bootstrap entry from the Classpath tab. But I just get a whole bunch of java.lang.NoClassDefFoundError for all the Android-derived classes in my project.
I've obviously changed something since I originally generated the ormlite_config.txt file, as I didn't have any problems before, but I can't see what I've done to break everything.
Can somebody tell me exactly what I should have in my Classpath / source tabs?
In the end the instructions in the documentation were all that was required, there was just a pair of bugs introduced in 4.43 that prevented it working as it should. These have now been fixed, and will presumably be in the next version. See https://groups.google.com/forum/?fromgroups=#!topic/ormlite-android/nBuQTVpCs0g
I ran into this exact problem and found out a solution myself.
I think the problem came from Google's ADT plugin. Since the plugin manages the Android libraries as private libraries, ORMLite Util cannot find the Android related classes in the project's build path.
The solution is to manually add the android.jar from your android sdk folder (in my case it's C:\android-sdk\platforms\android-18) into the build path as an external jar.
Hope it helps.
You have to remove the Android Lib from Bootrap Entries!
Do as follows:
Project > Properties > Run/Debug Settings
Select the class you want to compile and click edit
open tab "classpath" and remove Android Lib from Bootstrap Entries
I think i do not understand Android Library Project correctly. I'm having the following issue:
i have a library project in a package a.b.c.lib
i have a application which is uses the library project in package a.b.c
i have another application which is uses the library project in package a.b.c.pro
Everything is OK with the application in package a.b.c, however i hit the following issue (during runtime) in the a.b.c.pro app:
java.lang.NoClassDefFoundError: a.b.c.lib.c1$c2
the same class is found OK in the a.b.c app.
So, what is the right way to define packages in this case?
There's no right way. Every way is right. It should work so I think the problem is not in packages. Do you use Proguard for obfuscation? It can remove some classes which are used in your app. Also such errors happen because of some bugs in the toolchain. Try to clean and rebuild you app .
I've a strange and new issue with an eclipse 3.5 rcp application: Everything worked fine, then I changed the running system and moved a single class from one plugin to a dependent plugin. No big issue, but all of a sudden the application complains with a class not found exception (or NoClassDefFoundError - don't know exactly but will look it up and edit the post...)
I double, triple, ... checked all that I know, and made sure, that the manifest contains the dependancy with the correct version number and that the package is correctly exported in the other plugin. Eclipse jdk doesn't complain (no compiler errors/warnings) but when I debug just to the point where the class is instantiated and use 'CTRL+SHIFT+I' to inspect the 'new MyClass()' statement, the inspectors tooltip sais, that it can't resolve that class.
Any help, hint and encouraging comment is highly appreciated!
I also encountered a NoClassDefFoundError in an Eclipse 3.5 Galileo RCP app where I had a src/java folder in one plugin and needed to export a package for use by another plugin.
The solution for me turned out to be including the bin folder as part of the runtime class path. Normally it's only been necessary to include the . folder.
Solved the issue just by chance (and don't have the faintest idea whether this was a bug or a feature...):
The plugin project, where I moved the class into, had a maven-like folder structure with src/main/java and src/test/java, both folders defined as source folders.
Just because I wanted to try almost everything, I changed this structure back to the typical eclipse structure with just src as the single source folder.
And, after another 'project clean', it's running again. Strange...?
Edit
thanks for the hint, the folder structure was just a typo in this post.
After a lot more struggle - I'm quite positive that this was an eclipse issue. After 'solving' as above, the very next day the application failed to load other classes from the
same plugin and I think, after a restart of the Galileo workbench, the problems are gone.