【问题标题】:Proguard no longer works with RetrofitProguard 不再适用于 Retrofit
【发布时间】:2014-05-17 21:09:41
【问题描述】:

我发现了涉及同一主题的旧问题,但在最新版本中,没有一个可用的答案对我有用。

我在我的项目中使用 Retrofit。当我尝试组装时,出现以下错误:

警告:retrofit.client.OkClient:找不到引用的类 com.squareup.okhttp.OkHttpClient

我正在使用以下方法,但没有任何帮助:

-keepattributes Signature

-keep class retrofit.** { *; }
-keep class retrofit.http.** { *; }
-keep class retrofit.client.** { *; }
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }
-keep class com.google.gson.** { *; }
-keep class com.google.inject.* { *; }
-keep class org.apache.http.* { *; }
-keep class org.codehaus.mojo.** { *; }
-keep class org.apache.james.mime4j.* { *; }
-keep class javax.inject.* { *; }
-keep class sun.misc.Unsafe { *; }

-libraryjars libs/acra-4.5.0.jar
-libraryjars libs/radial-menu-v4.jar

-dontwarn javax.xml.stream.events.**
-dontwarn rx.**
-dontwarn org.apache.lang.**

# Application classes that will be serialized/deserialized over Gson
-keep class com.example.package.network.** { *; }

最近有人遇到过这个问题并解决了吗?

【问题讨论】:

    标签: android proguard retrofit


    【解决方案1】:
    -keepattributes Signature
    -keepattributes *Annotation*
    -keep class com.squareup.okhttp.** { *; }
    -keep interface com.squareup.okhttp.** { *; }
    -dontwarn com.squareup.okhttp.**
    
    -dontwarn rx.**
    -dontwarn retrofit.**
    -keep class retrofit.** { *; }
    -keepclasseswithmembers class * {
        @retrofit.http.* <methods>;
    }
    
    -keep class sun.misc.Unsafe { *; }
    #your package path where your gson models are stored  
    -keep class com.example.models.** { *; }
    

    我已将上述 proguard 文本用于使用 OKHTTP 进行改造。

    编辑: 很好的回购参考许多流行的图书馆 https://github.com/krschultz/android-proguard-snippets

    【讨论】:

    • 嘿,你可以添加一些关于你的工作的描述。
    • 对我来说,'-keepclasseswithmember' 很重要——在我添加它之前,我的 @Path 变量没有被替换。谢谢! :)
    • 对我来说即使没有 -keep class com.squareup.okhttp.** { ; } -keep interface com.squareup.okhttp.* { *; } 虽然我也使用 okhttp
    • 请您或其他人确认这将适用于 Retrofit 2
    • @Sniper:是的。你也可以检查一下github.com/krschultz/android-proguard-snippets/blob/master/…
    【解决方案2】:

    可能看起来微不足道,但是您是否尝试过包含此行? (如果你不使用 okhttp 就是)。

    -dontwarn com.squareup.okhttp.**
    

    事情是 Square 内部不使用 Proguard,因此虽然他们的库可能会对正在使用的内容做出一些假设,但如果您的项目不使用它,您可以放心地忽略它。我对毕加索也有同样的问题,这为我解决了问题。

    【讨论】:

    • 这确实使它可以编译,但正如您所提到的,只有在您确定不使用该功能时才是安全的。我会标记这个答案,因为对于很多人来说它是正确的
    • 别忘了保留模型类,这对我来说是个问题。
    【解决方案3】:

    此配置适用于使用 gson 改造

    #Using for retrofit & gson
    -keep class com.google.gson.** { *; }
    -keep class com.google.inject.** { *; }
    -keep class org.apache.http.** { *; }
    -keep class org.apache.james.mime4j.* { *; }
    -keep class javax.inject.** { *; }
    -keep class retrofit.** { *; }
    -keep class sun.misc.Unsafe { *; }
    -keep class com.google.gson.stream.** { *; }
    -keepclassmembernames interface * {
        @retrofit.http.* <methods>;
    }
    -keep interface retrofit.** { *;}
    -keep interface com.squareup.** { *; }
    -dontwarn rx.**
    -dontwarn retrofit.**
    

    另外,您需要添加与改造一起使用的所有 POJO 类,如下所示。

    -keep class com.google.gson.examples.android.model.** { *; }
    -keep class com.packagename.your.pojo.models.** { *; }
    

    keepattributes 如下所示

    -keepattributes Exceptions
    -keepattributes InnerClasses
    -keepattributes Signature
    -keepattributes Deprecated
    -keepattributes SourceFile
    -keepattributes LineNumberTable
    -keepattributes *Annotation*
    -keepattributes EnclosingMethod
    

    A nice discussion about proguard with retrofit goes here

    【讨论】:

      猜你喜欢
      • 2019-07-11
      • 2021-04-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 2020-06-18
      • 2016-09-09
      相关资源
      最近更新 更多