ProGuard: duplicate definition of library class?

39,933

Solution 1

If you add a proguard option -printconfiguration config.txt you'll see proguard adds

-libraryjars 'D:\tools\android\platforms\android-23\android.jar'

-libraryjars 'D:\tools\android\platforms\android-23\optional\org.apache.http.legacy.jar'

your duplicated classes (e.g. SslError) are presented in both android.jar and org.apache.http.legacy.jar

Proguard adds second jar even if you don't useLibrary 'org.apache.http.legacy' Here is an open bug describing the problem.

So now we can't do anything with the issue. Just ignore it:

-dontnote android.net.http.*
-dontnote org.apache.commons.codec.**
-dontnote org.apache.http.**

There is no need to keep the classes as long as they are located in library jar (phone's library actually). dontwarn doesn't work because it's not a warning, it's a note.

Solution 2

Probably, you have mentioned "-injars" and -libraryjars" in your proguard-project.txt,considering the latest build system takes care of mentioning them for you ..so you dont need to mention it again.

source: http://proguard.sourceforge.net/manual/troubleshooting.html#duplicateclass

I think this will help.:)

Solution 3

You can tell gradle not to allow duplicate classes (take only the first) by adding the following to your build.gradle:

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}
Share:
39,933

Related videos on Youtube

confile
Author by

confile

Java, GWT, JavaScript, Grails, Groovy, Swift, Objective-C, iOS

Updated on October 17, 2020

Comments

  • confile
    confile over 3 years

    I run my ProGuard for my Android project and get the following warnings:

    Note: duplicate definition of library class [org.apache.http.conn.scheme.HostNameResolver]
    Note: duplicate definition of library class [org.apache.http.conn.scheme.SocketFactory]
    Note: duplicate definition of library class [org.apache.http.conn.ConnectTimeoutException]
    Note: duplicate definition of library class [org.apache.http.params.HttpParams]
    Note: duplicate definition of library class [android.net.http.SslCertificate$DName]
    Note: duplicate definition of library class [android.net.http.SslError]
    Note: duplicate definition of library class [android.net.http.SslCertificate]
    
    Note: there were 7 duplicate class definitions.
    

    I found here to fix this with ignoring it with:

    -keep class org.apache.http.** { *; }
    -dontwarn org.apache.http.**
    -keep class android.net.http.** { *; }
    -dontwarn android.net.http.**
    

    I do not see a way to remove the duplicates from the used libraries. Even after using dontwarn the warnings do not vanish.

    Is this the right way of handling this warning in just ignoring it or could this lead to problems?

    • Adem
      Adem over 8 years
      it seems you imported some classes from multiple library. you might add android library jar inside of proguard config
    • confile
      confile over 8 years
      Could you please post an example for that. Android plrguard def says that you should not use: libraryjars. See: proguard.sourceforge.net/manual/…
    • N J
      N J about 8 years
      post your progaurd rule file
    • NickUnuchek
      NickUnuchek about 8 years
      @confile did you solve your issue?
    • ban-geoengineering
      ban-geoengineering about 8 years
      I solved this by dropping the apache library from my project and replacing with my own code.
  • aasu
    aasu about 8 years
    Well, I'm on a pretty new Android Studio (Dec 1, 2015 build) and nobody takes care of mentioning them for me. Am I missing something simple?
  • Radim Vaculik
    Radim Vaculik about 8 years
    Great! Thanks. This answers should be marked as the correct one!
  • wviana
    wviana almost 8 years
    Just tried to place useLibrary 'org.apache.http.legacy' in build.gradle and place your -dontnote into the proguard file. But still getting problems. I was using api 22, was working fine. Now I upgrade into 23, I'm getting these errors when trying to build the signed. Some times grade works for indefinite. Don't know what else to do.
  • Edijae Crusar
    Edijae Crusar over 7 years
    Which build.gradle? project or app? if app, under which block do you place it? i have tried to put it in my app's build.gradle android block android{jar { duplicatesStrategy = DuplicatesStrategy.EXCLUDE } } but i get the error Error:Gradle DSL method not found: 'jar()'
  • voxoid
    voxoid over 7 years
    jar { is a task within the gradle java plugin, so if you apply plugin: 'java' then you should be able to use that jar block; it doesn't go inside another block; it's at the root level of the build.gradle. (see this gradle doc) I used it in an app, but not sure if that matters.
  • MML
    MML about 5 years
    Where I add this option proguard option -printconfiguration config.txt ?
  • Flavio
    Flavio about 5 years
    In command line were you run proguard. The config.txt will be created at the current folder