Skip to content

Upgrade OkHttp 3 to Kotlin and call it OkHttp 4 #4723

Closed

Description

@swankjesse

Inspired by Okio 2 (blog post, presentation), OkHttp 4 is almost exactly like OkHttp 3, except the implementation language is Kotlin instead of Java. We punt breaking API changes to a hypothetical OkHttp 5 that remains in our icebox.

Goals

Implemented in Kotlin with a dependency on the Kotlin standard library. We want the option to support coroutines and multiplatform including Kotlin/Native .

Compatible with OkHttp 3.x. We want 100% binary compatibility so .jar files compiled against OkHttp 3.x will run against OkHttp 4.x without modification. We’ll need japicmp to enforce this. We also want 100% source compatibility for Java callers. Kotlin callers will not be source-compatible but we’ll offer automated upgrades via the language’s rich deprecation facilities. One surprising consequence of this approach is that the Maven coordinates and package name of OkHttp 4 will be okhttp3.

No performance regressions. We must write careful Kotlin, paying attention to abstractions that have a runtime cost.

Tasks

  • Migrate from Maven to Gradle.
  • Integrate japicmp.
  • Get a japicmp release with this fix and delete all the suppressed methods.
  • Migrate public API types
  • Adopt Kotlin idioms everywhere appropriate (examples)

FAQ

Why Kotlin? It’s a great language for both applications and libraries. It gives us options to execute on more platforms including iOS. Coroutines are powerful concurrency abstractions that may allow us to implement nonblocking I/O without hating ourselves.

Isn’t Kotlin just a trend? It’s backed by the best tools maker and the default language of the most popular mobile platform. It has a simple upgrade path from Java.

Java already has var and I heard Java 13 will have string literals. The Java language is moving faster than ever and adding many great features! But Java has deep layers of technical baggage (checked exceptions! null! JavaBeans™) and I don’t want it for my children. Also, Oracle wants to copyright APIs and I think that’s gross!

Did you just say iOS?! It should be possible to use OkHttp’s requests, responses, and interceptors with a NSURLSession backend. If that works it’d be a pretty awesome way to write networking code for mobile apps.

When will Retrofit/Moshi/Okio/Wire be migrated to Kotlin? Okio is already there. We’re doing OkHttp and Wire right now. No firm plans for Retrofit and Moshi.

Can I stay on OkHttp 3.x forever? We’re hoping that you won’t have to. It’s our job to make OkHttp 4 compelling enough that the cost of this upgrade is justified.

Activity

  1. changed the title [-]Upgrade OkHttp 3 to Kotlin and Call it OkHttp 4[/-] [+]Upgrade OkHttp 3 to Kotlin and call it OkHttp 4[/+] on Mar 14, 2019
  2. added this to the 4.0 milestone on Mar 15, 2019
  3. yschimke commented on Mar 15, 2019

    @yschimke
  4. swankjesse commented on Mar 15, 2019

    @swankjesse
    Author
  5. alosdev commented on Mar 15, 2019

    @alosdev
  6. JakeWharton commented on Mar 15, 2019

    @JakeWharton
  7. JakeWharton commented on Mar 15, 2019

    @JakeWharton
  8. yschimke commented on Mar 17, 2019

    @yschimke
  9. swankjesse commented on Mar 17, 2019

    @swankjesse
    Author
  10. bsideup commented on Mar 19, 2019

    @bsideup

    Too bad that one of the best http libraries for the JVM is no longer a good choice.
    Sad to see that the world does not learn from previous mistakes (Java libraries in Groovy / Scala).

    One of the best things about OkHttp/Okio was the fact that it was zero-dependency library and it was easy to shade it. This is no longer true 🤷‍♂️

    I will leave this comment here, please come back and tick "+1" next time you fix "OkHttp doesn't work with Kotlin version X.Y.Z"-like issues 😅

  11. VISTALL commented on Mar 19, 2019

    @VISTALL

    I agree with @bsideup.

    There no way for exclude kotlin libraries, and that will be painful when more than one kotlin runtime exists in classloader with different versions.

    As java developer, I don't saw any positive news. iOS ? Nope. Kotlin? (I'm not mobile developer) Also - nope.

    Can I stay on OkHttp 3.x forever? We’re hoping that you won’t have to. It’s our job to make OkHttp 4 compelling enough that the cost of this upgrade is justified.

    It's my way 🚶 . Later i think need find other http library.

    Thanks for okhttp3 👍

  12. rafal-glowinski commented on Mar 19, 2019

    @rafal-glowinski

    @VISTALL there are plenty of companies that use Kotlin for their backend services.

  13. VISTALL commented on Mar 19, 2019

    @VISTALL

    @rafal-glowinski but not all (and not even 50% of them). It's a holy war ⚔️

  14. swankjesse commented on Mar 19, 2019

    @swankjesse
    CollaboratorAuthor

    We are going to be very conservative with which Kotlin standard library features we use. It's extremely important to me and to this project that we make it easy to stay up-to-date and also secure.

    Please give us a chance to be your preferred HTTP library for the JVM. Despite the fact that OkHttp uses Kotlin, we are still very concerned with usability from Java. Square maintains a huge number of Java services that use OkHttp and we need to keep those working smoothly!

  15. bsideup commented on Mar 19, 2019

    @bsideup

    @swankjesse let me explain our use of the library:

    We ( http://github.com/testcontainers/testcontainers-java/ project ) have migrated from Netty to OkHttp because we wanted to have a zero dependency http library (and shade-able).

    We care about the amount of dependencies because the library is popular, being used in many different projects, with different dependencies on its own, and every new dependency we add may lead to a conflict (as seen many times in the past). Even the most stable / popular ones caused us some headache.

    This is not just a panic.
    Most probably, we will stay with okhttp3 as long as it works, but also start looking for another library in parallel.
    Why? Because we're not ready to depend on an language ecosystem just because one of our dependencies have decided to cut some boilerplate. I wish Kotlin adds "stdlib-less" mode.

    Thanks for a great library, we enjoyed it so far, I respect your decision because obviously the project is very Android-focused, but wanted to provide some details why we regret about this decision and how it affects another, slightly less yet popular Java library.

  16. 17 remaining items

  17. shenliuyang commented on May 16, 2019

    @shenliuyang
  18. swankjesse commented on May 23, 2019

    @swankjesse
    Author
  19. swankjesse commented on May 26, 2019

    @swankjesse
    Author
  20. yschimke commented on May 28, 2019

    @yschimke
  21. swankjesse commented on May 28, 2019

    @swankjesse
    Author
  22. swankjesse commented on May 28, 2019

    @swankjesse
    Author
  23. swankjesse commented on Jun 1, 2019

    @swankjesse
    CollaboratorAuthor

    I’ve got a branch with the latest japicmp report. Only interesting binary-compatibility change is a bunch of accessors that became final in OkHttpClient. Otherwise going from 3.14 to 4.0 is no problem for Java callers.

  24. swankjesse commented on Jun 1, 2019

    @swankjesse
    CollaboratorAuthor

    Here’s what I want to do before the release:

    • Write a migration section for the changelog. This should include a list of incompatible changes and deprecations. Use KotlinSourceCompatibilityTest to find these!
    • Change all -deprecated_ to DeprecationLevel.ERROR. This might mean we have to delete KotlinSourceCompatibilityTest!
    • Cut the 4.0.0-RC1 release for early adopters.
    • Do a performance comparison with 3.14.x. Try to find regressions!
    • Hide Kotlin-internal APIs from Java. Use annotations like @file:JvmName("-Base64").
    • Write a blog post announcing the release.
  25. swankjesse commented on Jun 11, 2019

    @swankjesse
    CollaboratorAuthor

    Quick and dirty benchmarks reveal no performance regressions.

  26. BoxResin commented on Jun 20, 2019

    @BoxResin

    Would Android 5.0+ requirement be eliminated in v4?

  27. swankjesse commented on Jun 20, 2019

    @swankjesse
    CollaboratorAuthor

    OkHttp 4 has the same requirements as OkHttp 3.13+

  28. gitahinganga commented on Jul 15, 2019

    @gitahinganga

    Upgraded to OkHttp 4 last week. Now I can't release my Android app without multidex because I have exceeded the 64K limit. OkHttp 4's Kotlin dependencies add approximately 7K method references. I don't want multiple dex files, certainly not because of one library, so I am downgrading to OkHttp 3. Or does anyone have any suggestions about how to address this issue better?

  29. avently commented on Jul 15, 2019

    @avently

    Maybe proguard can help

  30. ZakTaccardi commented on Oct 23, 2019

    @ZakTaccardi

    We are developing server software and microservices

    Kotlin fully supports your use case

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions