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'
}
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!