user437212
user437212

Reputation:

Split package "okhttp3" errors out build if okhttp-urlconnection dependency added

I'm using OkHttp3 to build up a simple connection API for a pure Java app, and I've run into a build problem that seems triggered by the fact that Square uses the same package name for multiple dependency artefacts.

I've seen some previous Q&A that discusses Maven dependencies and messaging from Eclipse, but all of those indicate that a Maven or Gradle build still works even if Eclipse annotates imports with module errors. In this case the Gradle build fails as soon as I simply add a dependency and make no other changes.

The app is a pure Java 11 module build. I'm using a very recent Eclipse with Gradle nature as an IDE, but I don't think this is strictly relevant. I'm using OkHttp3 to turn private endpoints into API, and one of those endpoints requires a CookieJar. Hoping to just use the default implementation, I add 'com.squareup.okhttp3:okhttp-urlconnection:3.14.9' as a dependency in a project that has already pulled in 'com.squareup.okhttp3:okhttp:3.14.9' as a transitive dependency. Both of these technically offer classes using the same package name: "okhttp3".

e.g., all I do is uncomment the dependency line seen in this snippet and save the build.gradle:

dependencies {
    implementation ('com.squareup.retrofit2:retrofit:2.9.0')
    implementation ('com.squareup.retrofit2:converter-gson:2.9.0')
    implementation ('com.squareup.okhttp3:logging-interceptor:3.14.9')
//  implementation ('com.squareup.okhttp3:okhttp-urlconnection:3.14.9')

As soon as the project refreshes I get annotations in Eclipse for all "okhttp3" imports:

The package okhttp3 is accessible from more than one module: okhttp3, okhttp3.logging, okhttp3.urlconnection

A clean build results in:

$ ./gradlew clean build

[...]

> Task :compileJava FAILED
error: the unnamed module reads package okhttp3 from both okhttp3.urlconnection and okhttp3
error: module retrofit2.converter.gson reads package okhttp3 from both okhttp3 and okhttp3.urlconnection
error: module retrofit2 reads package okhttp3 from both okhttp3 and okhttp3.urlconnection
error: module org.apache.commons.io reads package okhttp3 from both okhttp3 and okhttp3.urlconnection
error: module httpcore5 reads package okhttp3 from both okhttp3 and okhttp3.urlconnection
[...]

I don't think it matters, but I'm using Gradle wrapper 5.6.4.

All OkHttp3 libraries, as far as I know, have set module info enough to satisfy Java 9+. The module stuff in Eclipse seems satisfied by that. It looks like neither Eclipse or Gradle like the fact that two different dependencies advertise their Java package as "okhttp3". It seems to me that any Gradle or Maven based project using Java 9 or higher will fail with split-package dependencies.

Based on some advice I read elsewhere, I've tried excluding 'com.squareup.okhttp3:okhttp' from all the dependencies that include it transitively and then pull it in separately, but this did not help (not that I thought it would, but I'm trying any hail-mary at this point).

Workarounds include hacks like simply dropping the two Kotlin classes I want into the project directly and renaming the package that way. This is an egregious hack, and is probably contrary to the library license. I can also implement the cookie stuff I need directly, but I'm lazy (though, apparently, I want to spend my energy solving this problem instead of implementing a cookie class using the interface I already have).

I feel like this is a bug on the part of Square and how they package these libraries/modules. Since their focus is so much on Android maybe I'm the only one who has wanted okhttp-urlconnection on Java 9 or higher? So, this Question is more about seeing if I should raise this as a defect, and also maybe I've overlooked something obvious.

Upvotes: 3

Views: 1242

Answers (1)

Jesse Wilson
Jesse Wilson

Reputation: 40593

It's OkHttp’s fault and we can fix it for you. Please open a tracking bug with a link to this issue.

We'll move those two classes to a new package. For backwards compatible we'll also need to leave delegating implementations behind. Hopefully the tools permit this!

It's too bad that JPMS has this constraint. We already fixed some of our other open source projects but didn't fix this one.

Upvotes: 1

Related Questions