Solve code compilation errors caused by repeated dependencies of jar packages in Android Studio

I have used OKHTTP and rxjava in the original code, and then I kept reporting errors when I relied on retrofit today

Program type already present: okhttp3.internal.ws.RealWebSocket$1.class

It is said that I added the OKHTTP package repeatedly, but in fact, it is useless for me to directly comment out the OKHTTP dependency. As long as it depends on retrofit, this error will be reported.

It is recommended to add the following configuration on the Internet, but it is invalid after I try, you can try

    configurations.all {
        // OkHttp 3.5.0+ includes the websockets API, so we need this to prevent a conflict
        exclude module: 'okhttp-ws'
    }

Finally I found an answer here https://github.com/facebook/react-native/issues/12646, the catch succeeded

The solution is as follows:

Because okhttp-ws was only added after okhttp3.5.0, the version is mandatory to be controlled below 3.5.0:

configurations.all {
      resolutionStrategy.force 'com.squareup.okhttp3:okhttp:3.4.1'
 }

Rebuild, the code no longer reports an error

 

 

----------------------------------------emm, the following is my face-slapping sequel for the second day- ---------------------------------------

 

After using the mandatory control version directly yesterday, I thought it would be all right. Today I wrote the relevant code of retrofit, and after running it, I found that there is a method in okhttp.ws below 3.5.0 that cannot be found, and it is a test of various methods. , or not, until I saw a little light on a blog (source of the blog: https://blog.csdn.net/cx1229/article/details/52786168 ), after implementing the method, the problem was solved (this time it was real solved )

First find out where you are repeating in the log information, which class or package is repeating, for example, I said yesterday that I am repeating the RealWebSocket class in okhttp, then search anywhere in the studio (shortcut key -> double-click shift) RealWebSocket

Seeing two duplicate RealWebSocket classes, you can directly see its directory address

Then I thought of a bunch of retrofit related dependencies that I just added yesterday

implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.5.0'

OK, now even if you find the root of the problem (in fact, I always understand that the problem is here hahaha )

The next step is very simple: 
in AS, choose to display the project as project, and find the bottom External Libraries

 

Then find the corresponding class library, as shown in the figure, after clicking in sequence, a pom.xml is found:

 

Here are some configuration files about this jar. After clicking it, find the groupID of your duplicate package and copy it:

Then go to build.gradle, find that dependency, add {exclude group: 'com.squareup.okhttp3'} 

 

So far, the problem has been solved. If you have multiple repeated dependencies, you can remove them one by one according to the above method.

Kiss, here I suggest that if you have other jar package duplication problems, you can also try to use this method to solve it

 

Guess you like

Origin blog.csdn.net/qq_34081612/article/details/88033600