【问题标题】:ProGuard not working with okhttpProGuard 不能与 okhttp 一起使用
【发布时间】:2015-02-11 03:22:17
【问题描述】:

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 和改造的 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 的更改有关吗?

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

【问题讨论】:

    标签: android android-studio proguard retrofit okhttp


    【解决方案1】:

    这对我有用:

    您必须在您的 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://stackoverflow.com/a/24178851/4102045

    【讨论】:

    • 仍然不适合我。警告被抑制,但 APK 无法正常工作。
    • APK 建好了吗?如果警告被抑制并构建了 APK,我认为问题更多与您如何使用 okhttp 库有关,而不是与 Android Studio 中的 ProGuard 配置有关。
    • 我想你弄错了,它必须是配置。 APK 已构建,但无法正常工作。但是,如果我关闭 ProGuard,它工作正常。我不直接使用okhttp,只是通过Retrofit间接使用。
    • 这对我也不起作用,但我直接使用okHttp。
    • 用户 okHttp3 现在
    【解决方案2】:

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

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

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

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

    更改后一切正常。

    【讨论】:

    • 字段被重命名为 ab 简直不好玩!
    【解决方案3】:

    OkHttp

    -keepattributes 签名

    -keepattributes 注解

    -保持类正常http3.** { *; }

    -保持界面正常http3.** { *; }

    -dontwarn okhttp3.**

    【讨论】:

      【解决方案4】:

      这个配置对我有用。对于所有封装了子包的子类,使用** 而不是*

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

      【讨论】:

      • -dontwarn com.squareup.** 只是抑制警告。我可以创建 APK 但 okhttp 不起作用。取消注释第 2 行后,再次出现相同的警告。
      【解决方案5】:

      将此添加到您的 proguard 设置中:

      -dontwarn com.squareup.okhttp.internal.huc.**
      

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

      【讨论】:

      • 忽略警告让我编译但也破坏了功能,之前尝试过。
      • “破坏功能”是什么意思?
      • 我能够解决这个问题,请看我的回答:stackoverflow.com/a/31872274/2427585 你的观点是正确的,警告可以忽略。
      【解决方案6】:

      万一还有人掉进这里。所需的 Proguard 配置在 OkHttp 主存储库中记录和维护:

      https://github.com/square/okhttp

      【讨论】:

        【解决方案7】:

        对于okhttp3,you needthe following

        # 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
        

        【讨论】:

          【解决方案8】:
          # 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
          

          【讨论】:

            【解决方案9】:

            okhttp3 Proguard 规则

            这里是 okhttp3 Proguard 的正确格式。

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

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2014-04-20
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-08-02
              • 2015-12-12
              • 2016-02-06
              相关资源
              最近更新 更多