ProGuard与okhttp不兼容

25

ProGuard无法与okhttp合作,我一直得到以下警告:

Warning:com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl: can't find referenced method 'long getContentLengthLong()' in program class com.squareup.okhttp.internal.huc.HttpURLConnectionImpl
Warning:com.squareup.okhttp.internal.huc.HttpsURLConnectionImpl: can't find referenced method 'long getHeaderFieldLong(java.lang.String,long)' in program class com.squareup.okhttp.internal.huc.HttpURLConnectionImpl
Warning:com.squareup.okhttp.internal.huc.JavaApiConverter$CacheHttpsURLConnection: can't find referenced method 'long getContentLengthLong()' in program class com.squareup.okhttp.internal.huc.JavaApiConverter$CacheHttpURLConnection
Warning:com.squareup.okhttp.internal.huc.JavaApiConverter$CacheHttpsURLConnection: can't find referenced method 'long getHeaderFieldLong(java.lang.String,long)' in program class com.squareup.okhttp.internal.huc.JavaApiConverter$CacheHttpURLConnection
Warning:there were 4 unresolved references to program class members.
         Your input classes appear to be inconsistent.
         You may need to recompile the code.
         (http://proguard.sourceforge.net/manual/troubleshooting.html#unresolvedprogramclassmember)

以下是我针对OkHttp和Retrofit的Proguard设置:

-dontwarn rx.**

-dontwarn okio.**

-dontwarn com.squareup.okhttp.*

-dontwarn retrofit.appengine.UrlFetchClient


-keep class retrofit.** { *; }

-keepclasseswithmembers class * {

@retrofit.http.* <methods>; }

-keepattributes Signature 
-keepattributes *Annotation*

这可能与Android Studio 1.0中对ProGuard的更改有关吗?

我尝试了相关问题的答案,但他们只建议使用我已经拥有的设置。

9个回答

36
这对我很有效:
您需要在proguard-rules.pro中添加以下两行内容:
-keep class com.squareup.okhttp.** { *; }

-keep interface com.squareup.okhttp.** { *; }

完整的proguard-rules.pro文件如下:

-dontwarn rx.**

-dontwarn okio.**

-dontwarn com.squareup.okhttp.**
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }

-dontwarn retrofit.**
-dontwarn retrofit.appengine.UrlFetchClient
-keep class retrofit.** { *; }
-keepclasseswithmembers class * {
    @retrofit.http.* <methods>;
}

-keepattributes Signature
-keepattributes *Annotation*

来源: https://dev59.com/82Ah5IYBdhLWcg3wDfkW#24178851

当你在Java中使用System.out.println()时,它实际上是在向标准输出流打印内容。默认情况下,这会将文本发送到控制台窗口。但是,您可以通过调用System.setOut()方法来更改输出流的目标。例如,您可以将其设置为写入文件或网络套接字。


仍然对我无效。警告已被抑制,但 APK 无法正常工作。 - wkarl
APK是否已经构建?如果警告被抑制且APK已构建,则我认为问题更多地涉及您如何使用OkHttp库,而不是Android Studio中的ProGuard配置。 - pikufolgado
4
我认为你误解了,问题应该出在配置上。虽然 APK 已经构建完成,但是它并不能正常运行。但是,如果我关闭 ProGuard,它就能正常工作。我并没有直接使用 okhttp,只是通过 Retrofit 间接使用。 - wkarl
1
这对我也不起作用,但我直接使用okHttp。 - seato
用户现在使用的是“okHttp3”。 - Darpan
使用 dontwarn 只是隐藏了警告,而没有解决问题。如果你不能在不设置 dontwarn 的情况下让类引用正常工作且没有警告,那么你就有一个问题... - daniel.gindi

13

我终于成功解决了这个问题。

我遇到的警告实际上是无意义的,可以忽略。

相反,我忘记了不要混淆我的模型类:

-keep class com.example.datamodel.** { *; }

经过这个更改后,一切都正常工作了。


3
将字段重命名为ab并不好玩! - EpicPandaForce

6

OkHttp

-keepattributes Signature(保留签名)

-keepattributes Annotation(保留注释)

-keep class okhttp3.** { *; }(保留okhttp3包下所有类及其属性)

-keep interface okhttp3.** { *; }(保留okhttp3包下所有接口及其方法)

-dontwarn okhttp3.**(禁止显示okhttp3包下警告信息)


1
将以下内容添加到您的ProGuard设置中:
-dontwarn com.squareup.okhttp.internal.huc.**

我认为可以安全地假设你没有使用 com.squareup.okhttp.internal 中的任何类,因为这是你收到警告的来源。


只是忽略警告让我编译通过,但也会破坏功能,之前试过了。 - wkarl
你说的“破坏功能”是什么意思? - seato
我已经解决了这个问题,请查看我的答案:https://dev59.com/xV4c5IYBdhLWcg3w3dd_#31872274。不过你说的也有道理,这些警告可以安全地忽略。 - wkarl

1
# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**

# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform

1

对于okhttp3,你需要 以下内容

# JSR 305 annotations are for embedding nullability information.
-dontwarn javax.annotation.**

# A resource is loaded with a relative path so the package of this class must be preserved.
-keepnames class okhttp3.internal.publicsuffix.PublicSuffixDatabase

# Animal Sniffer compileOnly dependency to ensure APIs are compatible with older versions of Java.
-dontwarn org.codehaus.mojo.animal_sniffer.*

# OkHttp platform used only on JVM and when Conscrypt dependency is available.
-dontwarn okhttp3.internal.platform.ConscryptPlatform

1

使用这些配置对我有效。 对于所有封装子包中的所有子类,请使用**而不是*

-dontwarn org.xmlpull.v1.**
-dontwarn com.squareup.**
-keep class com.squareup.** { *; }

2
-dontwarn com.squareup.** 只是抑制了警告。我能够创建APK,但Okhttp不会工作。取消注释第二行后,同样的警告再次出现。 - wkarl

1

0

okhttp3 Proguard规则

这是okhttp3 Proguard的正确格式。

-keepattributes Signature  
-keepattributes Annotation  
-keep class okhttp3.** { *; }  
-keep interface okhttp3.** { *; }  
-dontwarn okhttp3.**  
-dontwarn okio.**

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接