Android linking jar JRE1.7 NoClassDefFoundError - android

I have an android 2.1 app and a separate JRE jar library with core methods of the app, compatible with other web apps. The core uses sqlite4java.
When I specify to compile the library with JRE1.6 there is no problem accessing the classes of the jar library from the app, but as the library uses sqlite4java it doesn't work because it needs to be JRE1.7 (Complains with "Couldn't load sqlite4java-android-armv6l")
When I specify to the library to compile in JRE1.7, it compiles ok, runs ok the app but when the app tries to access to any class in the library it fails with NoClassDefFoundError. During compiling I get the following error with each class of the library:
[2012-05-31 13:00:21 - AndroidApp-0.2] Dx bad class file magic (cafebabe) or version (0033.0000)
...while parsing com/androidapp/core/RES.class
...while processing com/tonads/androidapp/RES.class
I tried to put in classpath the directory where my library is but no result. Also in manifest included every .jar and .class file that the library uses.
After exhaustive googling, couldn't find any solution. Any ideas would be appreciated.

but as the library uses sqlite4java it doesn't work because it needs to be JRE1.7
First, Android does not support Java 7.
Second, since that project has an outstanding issue to confirm Java 7 compatibility, I suspect that your theory regarding Java 7 is incorrect.

Related

Using protobuf to communicate between JNI and Unity

My Unity game is using a Android library. They communicate by sending strings over a JNI bridge. Now I want to send protobuf messages over that bridge instead. I already have Google protobuf as a dependency in both Unity and my Android library Project. However I'm not sure how to go ahead.
What I did
create a .proto file?
download protoc for compiling the .proto file for both Unity (c#) and Android (Java)
add the compiled files to the repective projects
In the Android / Java part, everything seems to compile fine. However in Unity I'm getting a ton of compilation errors when adding the compiled .proto file ( == auto generated file)
Here as a placeholder the first compilation error:
Assets\MyApp\ProtobufExample\Detection.cs(62,13): error CS0234: The type or namespace name 'IBufferMessage' does not exist in the namespace 'Google.Protobuf' (are you missing an assembly reference?)
To me this looks like my GoogleProtobuf dependency is not up to date with the compiler, but this is just a guess.
Update
It was a dependency problem. Apparantly I cannot use the latest protobuf compiler (v3.20), as it adds more dependencys that are unknown to the Protobuf library inside Unity. As pointed out in the protobuf github issues, an old version (v3.0) does the trick.
Now I don't have any compilation issues anymore. However I'm getting runtime issues. JNI is complaining that
System.Exception: JNI: Unknown signature for type 'My.Proto.Example.DetectionType' (obj = My.Proto.Example.DetectionType) equal
Could be still an issue of the protoc version being different for Java and C#. Looking at the gradle file I have "protoc:3.18.0" there which indeed might be incompatible with v3.0 which I needed for Unity.
Downgrading Java protobuf leads to gradle compilation errors Unknown generator option: lite.
If anyone has ever done this, could you please recommend a version number that works for both Uniy and Java
I've also had this problem, I've solved it by upgrading the plugin version (to 2.46) in unity. This allows me to use the newest version of protobuf (3.20) and grpctool (2.45). The plugin is provided by official grpc, you can download it here.

Okhttp NoClassDefFoundError

I am using OkHttp 2.0.0 jar in my android application on eclipse. As OkHttp is now dependent on Okio library, I also added Okio 1.0.0 jar in my project.The project compiles fine with no compilation errors but when I run on my android device, it gives me this NoClassDefFoundError all relating to the OkHttp library classes.
I have placed all the jars in my libs folder of the project and added them to my build path and also tried checking them in the Export and Order Tab, but still it is not working.
I wanted to know since okhttp is dependent on okio, will just adding both jars would work fine or would I have to do anything different.
I have also tried the volley jar and it works all fine without any trouble.Anyone could help me out here, the problem is around for now 3 to 4 days.
Update
I also tried the android studio and added the okhttp library in the build dependency but still not working.
OkHttp v2.0 depends on Okio v1.0. You can download its jar here. This worked for me
Reference from
https://github.com/square/okhttp/issues/870
In OkHttp 2.0.0 the language level was updated to Java 7.
See Change log here https://github.com/square/okhttp/blob/master/CHANGELOG.md
In order to fix the issue you will need to update to java 7.
Also at this point you may also need change the target sdk to kitkat (API 19), and update your build tools etc in order for android to play nice with java 7
EDIT: I had the same problem when i upgraded to okHttp 2.0.0 and built with ant. Although the build seemed to succeed i got the NoClassDefFoundError. Going through my build logs I had a whole bunch of silent errors
The first came from javac
warning: com/squareup/okhttp/<classname>.class major version 51 is newer than 50, the highest major version supported by this compiler.
and the second from dex
[dx] trouble processing:
[dx] bad class file magic (cafebabe) or version (0033.0000)
[dx] ...while parsing com/squareup/okhttp/<classname>.class
[dx] ...while processing com/squareup/okhttp/<classname>.class
Go to Project/Properties/Java Build Path/Order and Export -- Make sure there's a check in front of Android Dependencies and the support library, if you use it. Mark all checkboxes and Click on Apply and clean the project.

Using cwac-camera via JARs

I'm using the commonsguy cwac-camera library, as per the demo-layout example, documented in "Working directlly with cameraview".
All is fine referencing camera/ and camera-v9/ as Android library projects in source form (I need Android 2.3 compatibility, that's what camera-v9 is for).
When I switch to using the library via JARs:
- cwac-camera-v9-0.6.8.jar only includes CameraFragment and BuildConfig classes, so I need also cwac-camera-0.6.8.jar with the other classes.
- including both JARs causes the following self-explaining error when running the project (not at compile time) Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define Lcom/commonsware/cwac/camera/BuildConfig;
I could just use the source as library project, or use Gradle, but I want to know if this is a bug to open an issue on Github, or if I'm doing something wrong.
To replicate the error, just clone the demo-layout example add both .jar files to libs folder, and run the project.
No, this appears to be my fault. They must have changed something in the Gradle build process that I am using to create the JARs. I will try to fix this tomorrow. In the meantime, you could go into the cwac-camera-v9 JAR and try removing the classes in com.commonsware.cwac.camera, leaving only those classes incom.commonsware.cwac.camera.acl.
My apologies for this, and thanks for pointing it out!

Android Junit test - Mockito class not found exception

I'm attempting to build a unit test on Android which utilizes the mockito-all-1.8.5.jar file from mockito.org. I added the jar file to my project using project > properties > project build path > add external jar. This has always worked for me on non-Android java projects.
However, when I run the unit test, I get the following class not found exception:
java.lang.ClassNotFoundException: org.mockito.runners.VerboseMockitoJUnitRunner
Above it is the following message:
Cannot load class. Make sure it is in your apk. Class name: 'org.mockito.runners.VerboseMockitoJUnitRunner'. Message: org.mockito.runners.VerboseMockitoJUnitRunner
I checked the jar and the class is there.
I also tried adding according to this link:
How to use and package a JAR file with my Android app?
But, still no luck. How can I get this class to be found?
Edit: I'm thinking there is a problem with whatever libraries Mockitto depends on - they may not be compatible with the Dalvik jvm. See this post:
http://daverog.wordpress.com/2009/12/14/why-android-isnt-ready-for-tdd-and-how-i-tried-anyway/
1) Import a mocking framework (e.g. mockito) into the project as an additional dependency.
Any imported jars containing class files not compiled to Dalvik bytecode (most) will not work. Attempting to compile the source along with your project will not work either because most libraries will make extensive use of parts of the Java language not compatible with Dalvik: it uses its own library built on a subset of the Apache Harmony Java implementation.
If you are executing inside the Dalvik JVM you won't be able to use Mockito.
If you are running your unit tests on your desktop JVM however (with a testing framework like Robolectric for example) then you are good to go. I am combining the two on my Android projects at present, to good effect.

Android adding external libraries to project

I have a project that I would like to add external libraries to (and have them packaged with the application) but I am not sure it is happening. I read on this link:
https://developer.android.com/guide/appendix/faq/commontasks.html
how to, but they do not show up in any of the /data/data/project directories. Does anyone know how I can confirm that the libraries were in fact added to the project for use at runtime? Thanks.
If you include jars as External Jars under your project's Java Build Path, then the classes will be converted to Dalvik format and be made available in your project's classes.dex file, packaged into the .apk.
To confirm they are available, attempt to use something from the jar (Eclipse should suggest the relevant import when you first supply a class name) build and run the app and see if it works? If it works in development (e.g. from 'run' in Eclipse) then it will also work when the app is built in release and distributed as an APK.
You can also place jar under one of your source folders (perhaps creating special "libs" one) and adding it to build path.
Be warned - external libraries (which are compiled against some version or other of the libraries in a Java JDK) may sometimes have problems when running under android. This is because the Dalvik runtime has its own Java framework libraries, which provide most (but not all) of the Java APIs in the standard JDK Java framework libraries.
You should really recompile any external library against the android libraries so that you can see any missing APIs at compile time - and fix the issues there and then. Otherwise you run the risk of runtime errors under Dalvik when you call the external library from your Android app. See http://geekswithblogs.net/cyberycon/archive/2011/05/17/using-external-libraries-with-android.aspx
for more details.

Categories

Resources