Java Protobuf in Android - NoClassDefFoundError - java

I'm building an Android app in ADT v21.0.0. This application needs to use a protobuf library I've compiled into a jar.
By putting this jar as well as protobuf-java-2.4.1.jar in my libs/ folder, Eclipse recognizes it at build time, however whenever I attempt to run the application in my Android emulator, I get a NoClassDefFoundError exception when attempting to instantiate one of my auto-generated custom classes produced by the protobuf compiler.
I've seen a lot of tips regarding the naming of the libs/ folder (apparently a prior ADT update broke this by requiring the /lib folder to be /libs), as well as adding .jars explicitly from the "Java Build Path" settings window (and checking the jars in the Order and Export pane).
None of these have helped, however, and I'm wondering if there's something more fundamental preventing my protobuf classes from being recognized at runtime.
Note: if I create a standard Java application in Eclipse, I am able to use my proto classes just fine. This is only an issue when running the Android application.

seems that your project import some library what your auto-generated jar depends..and is not on device or emulator..

Related

Android: java.lang.NoClassDefFoundError after importing jar. Wrong jar?

After I've imported jar file into Android project and ran it, I get java.lang.NoClassDefFoundError At first I thought that I'm doing something wrong while importing it, but then I tryed to import jar that I've from some online class and it worked! So here is my question:
Is there any specific way of creating jar files for Android project?
I've to point out that first jar is created from Java class that I wrote some time ago. This jar works without problems in another java app. The jar was created using Eclipse Kepler.
Thanks for your help.
The solution shoved up to be pretty simple. Currently I'm using Java 1.7 to create my jar files, but Android accepts only jars that were created with Java 1.6. So the problem is caused probably by some incompatibility in bytecode and therefore you need to force compiler to use Java 1.6.
If you're using Eclipse, go to Project properties > Java compiler and select Compiler compliance level: 1.6 Save changes and rebuild your project. You're ready to export "working" jar :)

NetBeans not correctly compiling my Java sources

I am encountering a problem with NetBeans 7.4 (but also previous versions such as NetBeans 7.3). When I clean & build my Java Web application, it gets successfully built, but my newly added code is not included with that build.
I confirmed this by decompiling the classes to check whether my changes got through, but they did not.
I have tried different versions of NetBeans to see what happened. At one point in time, I also compiled the application through command line using ANT. I have also tried deleting the build and dist folders manually. None of these methods solved my issue.
The only, not acceptable solution is that I compile the class individually, then it is ok. But when I clean and build the whole application, the older sources are compiled.
Looking forward for some help, because I'm really lost, and compiling individually is not an option, since before releasing, I would like to use a single .war file.
This issue was a silly mistake. The web part of this project was retrieved by copying the web folder from the server in order to make sure that my team has the latest sources. By doing this the class files were being included within the WEB-INF folder, therefore, when attempting to rebuild the application, for some reason, the build was retaking the files within the Web/WEB-INF folder and not generating the classes from the sources.
When that folder was cleaned up from any class files, the project now builds successfully.

Cannot debug project created with NB6.9.1 within NB7+ (has JAR dependency which uses JNI)

I just tried to migrate our working Java projects from Netbeans 6.9.1 to Netbeans 7.2 and I'm experiencing a problem due to one of our project dependencies being an external JAR which uses JNI.
I created a copy of our project directory and simply opened the copies of NB projects originally created with the older version of the IDE. Everything went smooth. I can build the projects and run compiled executable JAR-s outside of Netbeans without any problems.
However when I try to debug the project, the application fails to init properly due to said dependency JAR with JNI. It's like the JAR is failing to find a DLL associated with it (giving me java.lang.UnsatisfiedLinkError as a result). This does not happen with 6.9.1!
Why would this be happening? Do I need to explicitly set java.library.path in NB7+?
There were no changes made to the projects (not by us) and the same JDK is being used in both versions of the IDE. I suspect the newer version applies changes to the project setup silently and breaks something in the process. Anyone experienced something similar?
Edit 1:
Tried fiddling with project.properties, setting -Djava.library.path VM arg, different JDKs/JREs, ... all to no avail. This is driving me nuts. Obviously I'm doing something wrong.
I figured out what went wrong. This only became apparent after I got source of the external JAR (ext.jar from now on) in form of a Netbeans project so I could debug it.
ext.jar may load one of several DLLs depending on certain conditions. It constructs a file path to the appropriate DLL by using SomeClass.class.getProtectionDomain().getCodeSource().getLocation().getFile() and then calls System.load(path) with it's value. This path is different between the two versions of Netbeans. ext.jar is actually being used by another JAR (which is also a Netbeans project), which in turn is used by the main executable JAR of the application (also a project).
Main JAR (Netbeans project)
˪ Common JAR (Netbeans project)
˪ External JAR with JNI (ext.jar)
All external libraries in our project setup are placed within a directory at the same level where all the projects reside. This enables multiple projects to use a common set of libraries. Projects use relative paths to reference the libs.
CommonLibraries
MainJARProjdir
CommonJARProjdir
RandomProjdir1
…
RandomProjdirN
When Main is built, Common must be built before it (Common is a project dependecy of Main). In the process of building Common (by default) all dependency JARs get copied to ${common.proj.dir}/dist/lib. The copy process is of course unaware of the fact that it should copy DLLs along with ext.jar.
The root of the problem is however different handling of dependencies in 6.9.1 and 7+. If I specify ext.jar to be a dependency to both Main and Common (which is what was done, even though Main doesn't directly use any code from it) 6.9.1 will use the ../CommonLibraries/ext.jar when debugging, which always has all the required DLLs beside it, while 7+ will always use ${common.proj.dir}/dist/lib/ext.jar, which is missing the DLLs.
After recognizing what the problem is, the solution becomes trivial. I added a -post-clean target to build.xml of Common, which simply copies required DLLs from ../CommonLibraries/ to ${common.proj.dir}/dist/lib/ after each clean. It worked. This should be done either way – for completeness sake.
The reason for different handling of dependencies in the two versions of the IDE appears to be a new checkbox which is present in Project Properties/Build/Packaging called Copy Dependent Libraries. Ticking that off for Common also works. Note that this checkbox is enabled by default (even for library projects).

Java project won't compile to a JAR correctly for Android

in an app I'm working on I have an issue with a Java library which is here - https://github.com/kennydude/Boid-Twitter-API
When I run ant android which builds an Android package it builds fine and Eclipse can read the Jar fine.
But on my device it says that dalvik cannot locate any of the classes and eventually throws a ClassDef Exception.
I have used Jars before, so I'm wondering if it's my ant file that's broken
I would open your app project in eclipse (the one that depends on the Java library that is not found at run-time) and add the jar file the Java library produced to its build path. Then execute
android -s update project -p .
in the same directory as your apps' Manifest.xml file in order do generate ant build files.
After this, you should be able to build your app using ant by doing
ant debug
or
ant release
I fixed it by ensuring Ant was compiling to 1.6 of Java which is my target in Eclipse for the application.
Perhaps you're not placing your jar in the libs directory as opposed to lib directory? Here's a similar issue Java (Android) - class not found exception
First of all to build an apk with ant for Android you have to use ant debug or ant release, i don't know what ant android does and why you are using it but this command is not part of the standard approach to ant for Android, that's for sure.
Ant is just a building tools, if a developer provide a building solution in ant, does not mean that his application is for Android.
Under Android everything that is not a native library is converted in a DEX object, which is something similar to bytecode for the Dalvik, there is no jar or other container in your app.
This question may be helpful for you Adding a library/JAR to an Eclipse Android project .
There are a lot of conceptual errors in this topic, you probably want to study the Android platform before using it.

Getting Eclipse project info within an OSGi application

I'm working on a project which inspects Eclipse Java projects and getting some information of source files, using Eclipse AST. Right now it's an Eclipse plugin, but we need to make it independent from Eclipse, we are using only AST libraries of Eclipse.
I've tried copying required JAR's and run the project as a normal Java app(not an Eclipse plugin, not dependent to Eclipse) but then I started getting some errors, because when I run the app outside of Eclipse, AST can't get needed information(project data) from Eclipse and returning null for some cases. For example, when I visit a method invocation(MethodInvocationNode class), I need to know which class's method is this, but I can't because of AST errors I explained(MethodInvocationNode.resolveMethodBinding() returns null in this case).
I think I need to get an IProject, in Eclipse plugin I'm getting that from IWorkspace(ResourcesPlugin.getWorkspace()) which I think available in all Eclipse plugins.
Now, I think if I can make an OSGi app of it, maybe I can somehow get that IProject but I have no idea how can I do that. Documentations and tutorials didn't help much. It's also high probability that I'm missing some fundamental points about Eclipse projects and/or OSGi apps :) .
In short, how can I get IProject data within an OSGi app?
First, every Eclipse application is an OSGi-application, so there is no difference in doing things in a pure OSGi environment or Eclipse.
Second the "Workspace" is an Eclipse specific construct, so making your application "independent" from Eclipse makes no sense at all, the contents of a project, especially Java projects are eclipse specific (so you also have to parse the .classpath files with API from Eclipse to know which resources have to be compiled, parse .project files, etc).
The workspace mechanism is part of the bundle org.eclipse.core.resources. You need to include that in your OSGi application. Like already mentioned you can access all things the same way like in an Eclipse environment.

Categories