Home > database >  How to implement okhttp 5.0.0 in react native module
How to implement okhttp 5.0.0 in react native module

Time:07-07

I need to use the alpha-staged Java OkHttp library for a login method. It has the Happy Eyeballs (RFC 6555) Algorithm implemented, which improves loading times on requests between IPv6 and IPv4.

OkHttp (Version 5.0.0-alpha-5) supports a fastFallback method, which implements this feature. This is why I am eager to use this for my react native application. I imported the okhttp implementation in my build gradle file of my react native module.

dependencies {

implementation "com.facebook.react:react-native: "
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.10'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.10'

}

but on runtime, the app crashes and throws following error:

2022-07-06 16:30:11.477 12623-12725/com.timomobileapp E/AndroidRuntime: FATAL EXCEPTION: OkHttp Dispatcher
Process: com.timomobileapp, PID: 12623
java.lang.NoClassDefFoundError: Failed resolution of: Lokhttp3/internal/Util;
    at okhttp3.JavaNetCookieJar.decodeHeaderAsJavaNetCookies(JavaNetCookieJar.kt:81)
    at okhttp3.JavaNetCookieJar.loadForRequest(JavaNetCookieJar.kt:59)
    at com.facebook.react.modules.network.ReactCookieJarContainer.loadForRequest(ReactCookieJarContainer.java:44)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:74)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
    at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205)
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:533)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
    at java.lang.Thread.run(Thread.java:761)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "okhttp3.internal.Util" on path: DexPathList[[zip file "/data/app/com.timomobileapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.timomobileapp-1/lib/x86, /data/app/com.timomobileapp-1/base.apk!/lib/x86, /system/lib, /vendor/lib, /data/downloads, /data/priv-downloads]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:380)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at okhttp3.JavaNetCookieJar.decodeHeaderAsJavaNetCookies(JavaNetCookieJar.kt:81) 
    at okhttp3.JavaNetCookieJar.loadForRequest(JavaNetCookieJar.kt:59) 
    at com.facebook.react.modules.network.ReactCookieJarContainer.loadForRequest(ReactCookieJarContainer.java:44) 
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:74) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:65) 
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109) 
    at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:205) 
    at okhttp3.internal.connection.RealCall$AsyncCall.run(RealCall.kt:533) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607) 
    at java.lang.Thread.run(Thread.java:761) 

I cannot really tell where to get that missing Class. Help would be appreciated. Using 4.10.0 works fine.

I can't tell wether my configruation is wrong or OkHttp just does not support android yet.

Thanks in advance

CodePudding user response:

You need to upgrade all the OkHttp dependencies, including okhttp-urlconnection.

dependencies {

implementation "com.facebook.react:react-native: "
implementation 'com.squareup.okhttp3:logging-interceptor:5.0.0-alpha.10'
implementation 'com.squareup.okhttp3:okhttp:5.0.0-alpha.10'
implementation 'com.squareup.okhttp3:okhttp-urlconnection:5.0.0-alpha.10'

}
  • Related