6

We are having problems with Android network requests, to be more exact receiving random

SocketException:
java.net.SocketException: socket is closed
    at com.android.org.conscrypt.ConscryptFileDescriptorSocket$SSLInputStream.read(ConscryptFileDescriptorSocket.java:551)

The request does not seem to time out, the exception is thrown very quickly and it seems to be ONlY in release mode, our guess is that it might be related to ProGuard.

We’re using Retrofit, Moshi, and RxAndroid for our network requests. Has anyone experienced such issues?

Our dependencies and their versions:

implementation ‘io.reactivex.rxjava2:rxandroid:2.1.1’
implementation ‘io.reactivex.rxjava2:rxjava:2.2.9’
implementation ‘com.squareup.retrofit2:adapter-rxjava2:2.5.0’
//Moshi
def moshiVersion = “1.12.0”
implementation “com.squareup.moshi:moshi:$moshiVersion”
kapt “com.squareup.moshi:moshi-kotlin-codegen:$moshiVersion”
implementation “com.squareup.retrofit2:converter-moshi:2.9.0"
implementation “com.squareup.moshi:moshi-kotlin:1.8.0”
//Networking
implementation ‘com.squareup.okhttp3:logging-interceptor:3.12.1’
implementation ‘com.squareup.retrofit2:retrofit:2.6.4’
implementation ‘com.squareup.retrofit2:converter-scalars:2.1.0’

All of our models are annotated with @Keep annotations:

@Keep
class ModelDTO(
  @field:Json(name = “field1")
  var field1: String? = null,
  @field:Json(name = “field2”)
  var field2: List<String>? = null
)
HttpClient + Retrofit:
val client = OkHttpClient.Builder()
  .connectTimeout(300, TimeUnit.SECONDS)
  .writeTimeout(300, TimeUnit.SECONDS)
  .readTimeout(300, TimeUnit.SECONDS)
client.retryOnConnectionFailure(true)
  .addNetworkInterceptor { chain ->
    val request = chain.request().newBuilder().addHeader(“Connection”, “close”).build()
    chain.proceed(request)
  }
val moshi = Moshi.Builder()
  .add(KotlinJsonAdapterFactory())
  .build()
val retrofitBuilder = Retrofit.Builder()
retrofitBuilder.baseUrl(BuildConfig.BASE_URL)
retrofitBuilder.client(client)
  .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
  .addConverterFactory(MoshiConverterFactory.create(moshi).asLenient())
  .addCallAdapterFactory(
    RxJava2CallAdapterFactory.createWithScheduler(Schedulers.io())
  )

Our Gradle settings:

        release {
        debuggable false
        minifyEnabled true
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        consumerProguardFiles 'proguard-rules.pro'
    }

2 Answers 2

3

After multiple attempts we fixed the issue with this:

.connectionPool(ConnectionPool(0, 1, TimeUnit.NANOSECONDS)).protocols(listOf( Protocol.HTTP_1_1))

This was added to OkHttpClient :) Works like a charm!

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, could you please expand on where you applied this change and how it fixed the issue? Thank you!
What does this piece of code does?
0

You can add code below to pro-guard file:

-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }
-dontwarn okhttp3.**
-dontwarn okio.**

But you using R8, everything included.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.