2

I make a simple request just to check if I have Internet access:

return try {
    val request = Request.Builder()
        .url("https://www.google.com")
        .build()

    val response = OkHttpClient()
        .newCall(request)
        .execute()

    response.body()?.close()
    response.isSuccessful
} catch (e: IOException) {
    false
}

And pretty often I fall into catch block with java.net.SocketTimeoutException: Read timed out. I found out that OkHttp doesn't go further this line in okhttp3.internal.connection.RealConnection:

private void connectTls(ConnectionSpecSelector connectionSpecSelector) throws IOException {
    // ...
    try {
        // ...
        sslSocket.startHandshake(); // never goes to the next line
        // ...
    } catch(AssertionError e) {
    // never goes here
    } finally {
    // goes here
    }
}

I can't figure out what happens in sslSocket.startHandshake() because apparently sslSocket is com.android.org.conscrypt.ConscryptEngineSocket and Android doesn't really want me to see how it works. Is it some kind of a bug or I just do something wrong? Is there a fix or at least a workaround?

Log:

Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setUseSessionTickets(Z)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setHostname(Ljava/lang/String;)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setUseSessionTickets(Z)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setHostname(Ljava/lang/String;)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setUseSessionTickets(Z)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setHostname(Ljava/lang/String;)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setUseSessionTickets(Z)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setHostname(Ljava/lang/String;)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setUseSessionTickets(Z)V (blacklist,core-platform-api, reflection, denied)
Accessing hidden method Lcom/android/org/conscrypt/ConscryptEngineSocket;->setHostname(Ljava/lang/String;)V (blacklist,core-platform-api, reflection, denied)

I use Android 11 (API 30) and OkHttp3 v3.12.0.

P.S. As I remember, I can't update OkHttp to something newer because I have another dependencies that rely on OkHttp of this particular version because newer versions are backward incompatible.

Victor K.
  • 151
  • 4
  • 14
  • It works on android 10, 9 or 8? – Link182 Aug 12 '20 at 20:34
  • `buildToolsVersion '29.0.3'`, so it should be on Android 10 as well but it stopped to reproduce even on Android 11 several hours after I wrote this question. Guess, it didn't go there this time. Now I don't really have time to try to solve this problem but I may return to it later. – Victor K. Aug 13 '20 at 03:58
  • It doesn't work on Android 10 as well but works on Android 7. – Victor K. Aug 13 '20 at 09:40
  • Then check my answer and try to decrease buildtools version to android 7 if you need a quick solution – Link182 Aug 13 '20 at 10:45
  • Current `compileSdkVersion` is `29`, so if I want to change `buildToolsVersion` to something older like `28.0.3`, I need to change `compileSdkVersion` to `28` too. And it is kind of problematic. – Victor K. Aug 13 '20 at 15:38

3 Answers3

0

Seems like your okhttp3 library version access any internal restricted (by new android versions) classes by reflection.

Here is described how to conform and how to find the maximal suitable solution for you: https://developer.android.com/distribute/best-practices/develop/restrictions-non-sdk-interfaces

or...

An quick workaround is to decrease "android build tools" version to a version where such restrictions are not implemented yet. Is a small hack to avoid restrictions on new android versions. But be careful because playmarket will not accept an application compiled with a too old "build tools", so it is not a durable solution.

Link182
  • 733
  • 6
  • 15
0

Upgrade to OkHttp3 v3.12.12, this was fixed already.

See https://square.github.io/okhttp/changelog_3x/#version-3129

Yuri Schimke
  • 12,435
  • 3
  • 35
  • 69
0

migrating to OkHttp3 4.4.0 has fixed the issue

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Asad
  • 1,241
  • 3
  • 19
  • 32
  • 1
    Given you are not the OP, how can you say that this "fixed the issue"? – Mark Rotteveel Aug 29 '21 at 06:38
  • @MarkRotteveel, I was facing the same issue, I fixed the issue by doing this. – Asad Aug 31 '21 at 05:25
  • 1
    It is recommend to be explicit in this, because as it stands, it reads as if you're the OP, and that you fixed it by upgrading. While you're actually a different person, recommending the OP to fix it by upgrading (e.g. "I recommend trying to upgrade to OkHttp3 4.4.0. I had the same issue, and doing that fixed it for me", or something similar). – Mark Rotteveel Aug 31 '21 at 09:38