Native Java to Android Java (JsonPath) - java

I'm converting native Java code to android one.
There is a Android Java code from a native Java as following:
import com.jayway.jsonpath.JsonPath;
...
body = new String(bodyPart);
JsonPath.with(body).getString("psIp");
...
However, there is no with method in Android. How can I convert it?

JsonPath class comes from some library (most likely 'com.jayway.jsonpath:json-path:0.8.1'), which has com.jayway.jsonpathpackage. This class is not present neither in native Java, nor in native Android.
You have a dependency to that library, that has JsonPath class, in your Java project (anyway you added it - either through gradle, maven, or simply by adding a reference to jar file through IDE). If you would add that dependency to your Android project (gradle is preferred for adding dependencies in Android, though you can add it any other way), you will be able to use JsonPath class in your Android code also.

Related

Android Studio not excluding a transitive dependency and complains about class conflict

Hi I'm working on an Android app and I need to use a library called Semantics3. The issue I'm having is that the Semantics3 library depends on a few different libraries one of which is org.json but Android Studio automatically includes its own unique version of org.json which causes various issues. I've tried a few things to work around this and I'll describe them below.
First, I just imported the Semantics3 library. All the code compiles but when code from the library runs to make a service call the app crashes. This is due to a "NoSuchMethod" error because Semantics3 makes use of a constructor from a class in org.json which the AS version doesn't include.
To get around this I made my own jar file of the Semantics3 code and add in the dependencies it needs on my own in Gradle. The app still crashes, though not because it can't find the constructor. In this case the error is due to an exception from org.json, specifically "org.json.JSONException: Value com.android.okhttp.internal.huc.HttpURLConnectionImpl of type java.lang.String cannot be converted to JSONObject". I couldn't figure out why this error was popping up so I decided to try out the Semantics3 library outside of Android and just made a simple Maven project. In the Maven project I can make service calls just fine with no issue, but if I add the jar file and dependencies on my own, I get the same error as in AS.
So I figure there's something missing in the source code on Github that I used to make the jar or something but that's beyond me to figure out, so I'm back at trying to just import the library. My final and current attempt involves trying to explicitly import the version of org.json that Semantics3 needs along with the Semantics3 library in Gradle. But when I bring in the Sematnics3 library I try to exclude the transitive dependency Semantics3 introduces to org.json. I've included the Gradle file for this attempt below.
// https://mvnrepository.com/artifact/com.semantics3/Sem3Java
implementation('com.semantics3:Sem3Java:1.2.4'){
exclude group:'org.json:json'
}
implementation 'org.json:json'
constraints {
implementation(group: 'org.json', name: 'json', version: '20190722') {
because 'Semantics3 requires version 20190722 of org.json, not the default AS version'
}
}
The issue is when I try to run the code with the above setup in Gradle I get an error saying there are duplicate class definitions contained in the Semantics3 library and org.json library. And yes there are certainly conflicts but I thought the exclude bit to the Semantics3 implementation would be enough to prevent that from happening. Did I make a mistake somewhere or am I misunderstanding or misusing the exclude? If it makes a difference it appears the Semantics3 library does hardcode the org.json library into its Jar file. I appreciate any help with this. Thank you.

How to use google/api/annotations.proto with Android

I have a problem with proto file in my project
I had import in my proto file:
import "google/api/annotations.proto";
I am getting following error when building the project.
Import "google/api/annotations.proto" was not found or had errors.
How can I use this import in my project? Should I add something to my build.gradle?
On non-Android, you could add this dependency to your build.gradle:
compile 'com.google.api.grpc:proto-google-common-protos:1.12.0'
However, Android uses Protobuf "Lite" instead of full Protobuf and there's not a pre-generated library with Lite for this proto. There is an open issue about this.
However, a workaround discussed for the well-known protos can be used here as well. Namely, use a protobuf dependency instead of a compile dependency. This will generate the code as part of your build.
protobuf 'com.google.api.grpc:proto-google-common-protos:1.12.0'
Unfortunately, this solution only really works for applications. If two libraries use this "solution" they must never be included into the same application as they will have duplicated (and potentially have different versions of) the generated classes.

Android library (.aar) using the same gradle dependency as app - but different versions, causes a NoClassDefFoundError

I have created a Android library which is packaged as an .aar file.
My library uses v1.8.0 of Squares Retrofit library internally.
An end user of my library is also using Retrofit, but they are using 2.0.1.
A problem arises when they try and run their app, a java.lang.NoClassDefFoundError is thrown when it reaches a line in my library that calls a Retrofit function that is no longer present in v2.
dependencies {
compile 'com.mylibrary.sdk:mylibrary:1.0.1#aar'
compile 'com.squareup.retrofit:retrofit:1.8.0'
...
compile 'com.squareup.retrofit:retrofit:2.0.1'
}
I am wondering if there is a standard way of dealing with this this?
The easy options that spring to mind are:
We upgrade our Retrofit version to 2
The end user downgrades their Retrofit version to 1
Both these options feel to be short-term. Also if for example, we upgrade to Retrofit 2 and a new end-user comes along that is using Retrofit 1, we are back to facing the same problem but in reverse.
Having done some research already, creating a full fat .aar build or using shading, may be solutions. If anyone can shed any light on how these can be used in this case it would be much appreciated.
The problem is probably somewhere else. It's not about the fact that there are some methods missing, because the new (2.X.X) version of Retrofit can work side-by-side with the old one (pre 2.0.0). So if both versions were imported properly into the project, you should be able to use both Retrofit and Retrofit2 without any problems. It's possible because the new version of the library has a different package (retrofit2.Retrofit instead of retrofit.Retrofit).
Are you sure that your library includes Retrofit 1.8.X? Maybe it just uses methods from the library but the 1.8.X version is not shipped in your aar? That would actually explain a lot.
What you can is either:
Ask the person that is using your library to include both versions of the Retrofit, apart from your library.
compile 'com.squareup.retrofit:retrofit:1.9.0'
compile 'com.squareup.retrofit2:retrofit:2.0.1'
or
Make sure the proper version of the Retrofit (1.X.X) is included in your library's aar.

How to override a java library in plugin development environment

I develop an application in a "special" plugin-development environment.
This environment needs to include some java libraries by default so that the created plugin-application can be exported and used successfully.
The problem is now that the plugin-environment comes with an old "javax.mail" library. It is not supported to override this library in the environment.
Of course it is possible to include the newer javax.mail library into my plugin-application but the library is not recognized and the old library is used.
Question:
Is it possible to force a Java Application to use a special included library which is using the same package and class names like a "native" library from a "plugin-environment"?
What I have tried:
I tried to rename the package files within the custom library "javax.mail" to "javax_external.mail" and to use something like this in my application:
javax_external.mail.Session session = javax_external.mail.Session.getDefaultInstance(props);
But I get the error: Type mismatch: cannot convert from javax.mail.Session to javax.mail.Session

No class found exception while using json-lib in android

I am taking the input from the web, which is an Xml file and converting into a Json data using the library json-lib . I have created a user library and added the following jars into it:-
json-lib-2.3-jdk15.jar
commons-collections.jar
commons-lang.jar
commons-logging.jar
commons-beanutils.jar
ezmorph-1.0.6.jar
xom-1.1.jar
But still gives the following error:-
08-04 13:58:31.642: ERROR/dalvikvm(484): Could not find class 'net.sf.json.xml.XMLSerializer$CustomElement', referenced from method net.sf.json.xml.XMLSerializer.addNameSpaceToElement
Can anyone help me out in resolving this issue.
Either you have a sdk level / jdk level conflict. I mean dalvik can't get the byte code of the CustomElement class of your librairy as it is compiled with to recent features for your SDK like annotations for instance.
Or there is a conflicting librairy json-lib in some other of your jars or lib folders.
(the 3 first comments are not relevant, it's just the way inner classes are compiled, using a $)
Regards,
Stéphane
Since android already support json org.json a different json library may conflict. (You can download the jar here)
Try to use this library instead of an external library on android.
BTW: You can also use this library if you need on any java code (not only android)

Categories

Resources