Java ProGuard:库类的重复定义?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33047806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:13:47  来源:igfitidea点击:

ProGuard: duplicate definition of library class?

javaandroidandroid-studioproguardandroid-proguard

提问by confile

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

我为我的 Android 项目运行 ProGuard 并收到以下警告:

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 hereto fix this with ignoring it with:

我发现here通过忽略它来解决这个问题:

-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 dontwarnthe warnings do not vanish.

我没有看到从使用的库中删除重复项的方法。即使使用后dontwarn警告也不会消失。

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

这是处理此警告的正确方法而忽略它还是会导致问题?

回答by Sankalp Pandya

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.

可能,您在 proguard-project.txt 中提到了“-injars”和-libraryjars,考虑到最新的构建系统会为您提及它们..所以您不需要再次提及它。

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

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

I think this will help.:)

我认为这会有所帮助。:)

回答by Flavio

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

如果添加 proguard 选项,-printconfiguration config.txt您将看到 proguard 添加

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

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

-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

您的重复类(例如 SslError)同时出现在 android.jar 和 org.apache.http.legacy.jar 中

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

即使你没有,Proguard 也会添加第二个 jaruseLibrary 'org.apache.http.legacy'这是一个描述问题的开放错误

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.

只要它们位于库 jar(实际上是手机的库)中,就不需要保留这些类。dontwarn 不起作用,因为它不是警告,而是注释。

回答by Amit Tiwari

You can try this in your build.gradle for everything that is indicated as duplicate in the logs. I am not sure if this will work, so try it out and inform if it works or not.

您可以在 build.gradle 中针对日志中指示为重复的所有内容尝试此操作。我不确定这是否有效,所以请尝试并告知它是否有效。

packagingOptions {
        exclude 'android.net.http.SslCertificate'
    }

回答by voxoid

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

您可以通过将以下内容添加到 build.gradle 来告诉 gradle 不允许重复的类(只取第一个):

jar {
    duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}