Jackson unable to load JDK7 types on Android

2019-02-13 20:05发布

问题:

I use Jackson 2.8.2 in my Android app to deserialize JSON. The deserialization itself works, however, I can see the following warning in the application log:

Unable to load JDK7 types (annotations, java.nio.file.Path): no Java7 support added

Proguard is disabled, sourceCompatibility is set to '1.7'. How can I add these seemingly missing types to my build?

回答1:

First, your sourceCompatibility setting of 1.7 doesn't mean anything about the runtime environment, so it has no impact on this message.

This is purely a "do these classes exist at the time Jackson initializes this class" issue -- and they do not exist in some combination. And maybe that is ok, if you do not use the Java 7 java.nio.file.Path class then you should have no issue with this logged warning message. Because that is what this warning is about, Jackson supporting serialization/deserialization of this specific class.

Looking at Android java.nio.* packages, it does not have java.nio.file.* packages at any API level. So that explains why you see the warning message. And since you can't use java.nio.file.Path anyway, this isn't even a real issue other than an annoying logging message.

If this message is bothersome you can always set the Java Util Logging level for logger com.fasterxml.jackson.databind.ext.Java7Support to be level ERROR. Then you will no longer see these warning messages.

More about the logged message:

In Jackson 2.8.x this support for Java 7 class java.nio.file.Path is all loading from the same JAR file and is built-in. One class dynamically checks if another can load without error:

Class<?> cls = Class.forName("com.fasterxml.jackson.databind.ext.Java7SupportImpl");

The only way this can fail is if something is stripping this class from the final set of classes. Or one of these classes it depends on is missing from the runtime:

import java.beans.ConstructorProperties;
import java.beans.Transient;
import java.nio.file.Path;

If any of those are missing then you will see the logged error message. So one of these is true:

  • one or more of the JDK 7 classes are missing at runtime
  • com.fasterxml.jackson.databind.ext.Java7SupportImpl is missing at runtime

Neither of those causes are Jackson's fault, they are something about your runtime environment (i.e. they don't exist in Android API's), or Proguard stripping classes it doesn't think are used.

See also:

  • Android import java.nio.file.Files; cannot be resolved
  • How to use java.nio.file package in android?
  • Android java.nio packages