After adding React Native to an existing Android project, I get the following error when running ./gradlew assembleDebug:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':driver:transformClassesWithJarMergingForDebug'.
> com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: okhttp3/internal/ws/RealWebSocket$1.class
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
This project already had a dependency on okhttp3, so I'm not sure how to make Gradle ignore the React Native dependency on it.
React Native v0.31.0 uses OkHttp v3.2.0
The latest version of react native uses OkHttp v3.4.1
The latest version of OkHttp you can use is v3.4.2, due to breaking changes in OkHttp v3.5.0
https://github.com/square/okhttp/blob/master/CHANGELOG.md
@facebook-github-bot answered
Closing this issue as @AndrewJack says the question asked has been answered. Please help us by asking questions on StackOverflow. StackOverflow is amazing for Q&A: it has a reputation system, voting, the ability to mark a question as answered. Because of the reputation system it is likely the community will see and answer your question there. This also helps us use the GitHub bug tracker for bugs only.
You'll be able to use the latest version once this has been merged https://github.com/facebook/react-native/pull/11835
@AndrewJack won't it not matter what version of okhttp3 React Native uses, because the project I'm adding RN to already uses okhttp3, it will still have this error?
@lyahdav What version of okhttp does your project use? If it is 3.5 or later you'll get a build failure due to some breaking api changes.
By including okhttp in your project you'll be updating the transitive dependency of okhttp that react native uses.
I don't know much about Gradle (I'm an iOS developer), but I found the following in a build.gradle of a dependent library of the project:
dependencies {
compile 'com.squareup.okhttp:okhttp:12.7.7'
compile 'com.squareup.okhttp3:okhttp:13.5.0'
}
I also found in the React Native codebase a build.gradle with:
compile 'com.squareup.okhttp3:okhttp:13.4.1'
compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
compile 'com.squareup.okhttp3:okhttp-ws:3.4.1'
I'm a bit confused by those version numbers (12.x and 13.x) because if I go to https://github.com/square/okhttp/releases it shows 3.6.0 as the latest version. Considering the original error message mentions okhttp3 I assume it's only an issue related to the com.squareup.okhttp3 dependency. Shouldn't there be a way for multiple libraries in an Android project to depend on the same library (okhttp3) but not give compiler errors? Or do they have to be the same exact version?
I figured this out. It was the same issue listed here:
To fix this on your end, include the following in your build.gradle:
configurations.all { // OkHttp 3.5.0+ includes the websockets API, so we need this to prevent a conflict exclude module: 'okhttp-ws' }
Actually it's possible my previous comment is incorrect. After making that change the project compiles fine, but I get a runtime error. Since it's specific to my situation, I'm not sure if it makes more sense to continue here or on Stack Overflow. For now I created a separate Stack Overflow post.
could you find a fix @lyahdav i'm having exact same issue, but when i use this code
configurations.all {
// OkHttp 3.5.0+ includes the websockets API, so we need this to prevent a conflict
exclude module: 'okhttp-ws'
}
i'm getting another! see this issue
thanks!
No fix yet. I haven't had time to investigate further. Like I said above, also not sure if this belongs here or on my Stack Overflow question above.
I resolved that forcing for all my dependencies would be the same. I had already some dependencies using v3.5.0 and having the conflict with new okhttp-ws integrated into v3.5.0 :man_facepalming:
It's a workaround. Be aware that with this one we cannot use v3.5.0.
configurations.all {
resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1'
}
@lyahdav if you use an old version of socket.io try to upgrade its version. This rescued me.
if i set
configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }
it will show another error: âwebsockets APIs not define...â and so on... Because i used SocketIO in my project... (OkHttp 3.5.0+ includes the websockets API)
sounds terrible...
who can tell me how to solve this problem...
RN uses oktthpâs websockets.
As okhttp made a breaking change at 3.5.0 (which should have been 4.0.0) pre 3.5.0 and post 3.5.0 okhttpâs are not compatible. There is no workaround for that. They clash in the classloader level.
Forcing RNâs okhttp version on your own third party libs can work only if they donât use websockets themselves.
At least you have the choice to upgrade your RN version.
@simonracz
but how to force RNâs okhttp version on my ...
compile('com.facebook.react:react-native:0.43.4') {
// exclude module: 'okhttp-ws'
resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1'
}
there is a error:Error:(219, 0) Could not get unknown property 'resolutionStrategy' for DefaultExternalModuleDependency{group='com.facebook.react', name='react-native', version='0.43.4', configuration='default'} of type ...
Hey @Cge001 ,
I donât know the correct syntax without looking it up.
But I think you should put the resolutionstrategy in android {...}.
thanks @simonracz
yeah ,i set it like what you said.but it is no helpful ...you know...
Most helpful comment
I resolved that forcing for all my dependencies would be the same. I had already some dependencies using v3.5.0 and having the conflict with new okhttp-ws integrated into v3.5.0 :man_facepalming:
It's a workaround. Be aware that with this one we cannot use v3.5.0.
configurations.all { resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1' }