Matias Bjarland
Matias Bjarland

Reputation: 4482

Using okhttp 3.12.x from java 7?

Quoting the okhttp github page:

The OkHttp 3.12.x branch supports Android 2.3+ (API level 9+) and Java 7+

But looking at com.squareup.okhttp3:okhttp:3.12.12 (seems to be the latest 3.12.x release at the time of writing), we find the following:

import java.time.Duration; // OkHttpClient.java line 23

and various uses of Duration which is a java 8 feature. This makes my code break at runtime with a:

java.lang.ClassNotFoundException: java.time.Duration

when running with:

java version "1.7.0_251"
Java(TM) SE Runtime Environment (build 1.7.0_251-b08)
Java HotSpot(TM) 64-Bit Server VM (build 24.251-b08, mixed mode)

which is a hard requirement in my target environment.

Is there in fact a java 7 compatible decently recent version of okhttp?

The way I read the tea leaves (as in not the docs) is that the 3.12.x branch is mostly there to support android where I assume things might be slightly different and packages like java.time are actually available in java 7 under certain conditions.

Assuming it is actually not possible to use okhttp 3.12.x from java 7 (normal, not android), it might be good to update the docs to reflect this to save somebody else the explorative journey I just went through.

< edit - comment added after response by yuri >

I created a gist with a stack trace here. The breaking statement on line 53 is:

      client = new OkHttpClient.Builder()
        .addInterceptor(interceptor)
        .build()

(removing the addInterceptor call does not fix the problem)

Also it should be noted that this is a small, two-class project which I'm building with java 11, then using the mechanics in later versions of gradle (and java) to target java 7 and finally executing the fat jar with the above java 7 version. Quoting the gradle description:

Since version 9, the Java compiler can be configured to produce bytecode for an older Java version while making sure the code does not use any APIs from a more recent version.

So I'm building with:

------------------------------------------------------------
Gradle 6.6
------------------------------------------------------------

Build time:   2020-08-10 22:06:19 UTC
Revision:     d119144684a0c301aea027b79857815659e431b9

Kotlin:       1.3.72
Groovy:       2.5.12
Ant:          Apache Ant(TM) version 1.10.8 compiled on May 10 2020
JVM:          11.0.7 (Amazon.com Inc. 11.0.7+10-LTS)
OS:           Mac OS X 10.15.5 x86_64

and running with the above mentioned oracle java 7.

The reason I'm building with java 11 is that building with java 7 forces me to downgrade gradle and a number of plugins to ancient versions and just generally becomes a mess. Possible, but would be nice not to.

Upvotes: 1

Views: 2929

Answers (1)

Matias Bjarland
Matias Bjarland

Reputation: 4482

Answering my own question. After some feedback from the okhttp devs on an issue created for this at:

https://github.com/square/okhttp/issues/6221

it seems that this issue is caused by groovy's use of reflective access / introspection.

I added the CompileStatic annotation:

https://docs.groovy-lang.org/latest/html/gapi/groovy/transform/CompileStatic.html

to the method exhibiting the issue and the issue is now gone.

Upvotes: 2

Related Questions