ProGuard不使用okhttp

wka*_*arl 22 android proguard android-studio retrofit okhttp

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)
Run Code Online (Sandbox Code Playgroud)

这些是我的okhttp和改造的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*
Run Code Online (Sandbox Code Playgroud)

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

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

小智 34

这对我有用:

你必须添加到proguard-rules.pro这两行:

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

-keep interface com.squareup.okhttp.** { *; }
Run Code Online (Sandbox Code Playgroud)

完成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*
Run Code Online (Sandbox Code Playgroud)

资料来源:https://stackoverflow.com/a/24178851/4102045

  • 我认为你错了,它必须是配置.APK已构建但无效.但是,如果我关闭ProGuard,它工作正常.我不直接使用okhttp,只是通过Retrofit间接使用. (4认同)

wka*_*arl 9

我终于能够解决这个问题了.

我遇到的警告实际上毫无意义,可以忽略不计.

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

-keep class com.example.datamodel.** { *; }
Run Code Online (Sandbox Code Playgroud)

在这一改变之后一切正常.

  • 将字段重命名为`a`和`b`只是不好玩! (2认同)

Ali*_*nia 6

OkHttp

-keepattributes 签名

-keepattributes注解

-保持类okhttp3.** { *; }

-保持接口okhttp3.** { *; }

-不要警告 okhttp3.**