SearchSourceBuilder not functional on Android (Bug)

Our team is trying to use the Elasticsearch Java High Level REST Client 7.7.1 on an Android device. So far we've only been indexing data, not querying, and that code has been functional. But when trying to simply instantiate a SearchSourceBuilder object, the following error occurs:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.security.CodeSource java.security.ProtectionDomain.getCodeSource()' on a null object reference
   at org.elasticsearch.Build.getElasticsearchCodeSourceLocation(Build.java:194)
   at org.elasticsearch.Build.<clinit>(Build.java:137)
   at org.elasticsearch.common.logging.DeprecationLogger.<clinit>(DeprecationLogger.java:154) 
   at org.elasticsearch.search.builder.SearchSourceBuilder.<clinit>(SearchSourceBuilder.java:83)

The specific line of code which causes this NullPointerException is the following (Build.java:194):

final CodeSource codeSource = Build.class.getProtectionDomain().getCodeSource();

Apparently, per this StackOverflow answer, the method getProtectionDomain() "is not implemented in Android's version of Java." Build.class.getProtectionDomain() thus returns null, hence the NullPointerException.

This would indicate that any classes which use the DeprecationLogger, such as the critically important SearchSourceBuilder, will not function on Android. This is an issue with the Java REST Client.

Welcome!

I think this would be great to open this discussion on elasticsearch GitHub repo as well.

@DavidTurner WDYT?

1 Like

Opened an issue on GitHub, thanks for the tip!

Write Once Run Anywhere™ my foot :slight_smile:

I'm pretty sure we don't have any test infrastructure targeting Android, even for client libraries, so the answer may be that it's simply not a supported platform. If the only change needed is a null check here then that might fly (we added related null checks in #27442) but who knows what other problems may be lurking for you down the line.

As a workaround you can use the low-level REST client and construct the request yourself.

Strange that this is the first time anyone has encountered this. I guess folk don't often expose Elasticsearch directly to mobile clients as you're doing, there's usually a webapp to mediate things. Anyway, thanks for opening the issue, it's definitely a valid one for us to discuss internally.

2 Likes

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.