使用 Picasso 和 minifyEnabled 时应用程序崩溃 true

Posted

技术标签:

【中文标题】使用 Picasso 和 minifyEnabled 时应用程序崩溃 true【英文标题】:App crashing with Picasso and minifyEnabled true 【发布时间】:2017-07-25 13:14:44 【问题描述】:
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.OkHttpClient
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Cache
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.OkHttpClient
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.CacheControl
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.CacheControl$Builder
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Request$Builder
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.OkHttpClient
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Call
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Response
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.ResponseBody
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Response
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.ResponseBody
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.OkHttpClient
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Cache
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.CacheControl
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.CacheControl$Builder
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Request$Builder
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Response
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Cache
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.CacheControl
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Request
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Call
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Response
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.ResponseBody
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.OkHttpClient
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.CacheControl$Builder
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.CacheControl
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Request$Builder
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Response
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.ResponseBody
Warning:com.squareup.picasso.OkHttpDownloader: can't find referenced class com.squareup.okhttp.Cache
Warning:there were 52 unresolved references to classes or interfaces.
Warning:Exception while processing task java.io.IOException: Please correct the above warnings first.
Error:Execution failed for task ':app:transformClassesAndResourcesWithProguardForRelease'.
Job failed, see logs for details

这是我在尝试构建 release.apk 时遇到的错误。

我尝试通过添加来修复它:

-dontwarn com.squareup.picasso.**

在我的pro guard-rules.pro 文件中。

release.apk 现在可以正确构建,至少表面上是这样。

但是当我在我的手机(或任何手机)上安装该应用程序时,它会在启动时崩溃,我想当它开始使用 Picasso 加载图像时。

请帮我解决这个问题。

我尝试了一些在互联网上找到并建议您的解决方案,它们似乎都可以正确构建,但应用程序仍然不断崩溃。它会依赖于除专业守卫规则之外的其他东西吗?

这就是我的 gradle 的样子:

buildTypes 
    release 
        shrinkResources true
        minifyEnabled true
        proguardFile 'proguard-rules.pro'
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
    

【问题讨论】:

github.com/krschultz/android-proguard-snippets/blob/master/… @cricket_007 谢谢,我也试过这个,也没有运气:( 这至少应该修复毕加索,而不是任何其他图书馆 @cricket_007 没有其他库会发出任何警告。另外,代码必须没问题,因为在调试模式下构建 apk 时它可以工作 试试这些? github.com/krschultz/android-proguard-snippets/blob/master/… 【参考方案1】:

你似乎还在缩小 OkHTTP

can't find referenced class <b>com.squareup.okhttp</b>

前往Picasso's Github page

-dontwarn com.squareup.okhttp.**

【讨论】:

感谢您的回答。我也尝试使用这个,但没有任何运气。它确实修复了它,因为 apk 是正常生成的。仍然当我安装该应用程序时,它会在启动时崩溃,因为它必须使用 Picasso 加载图像 你能过滤 logcat 的错误而不是警告吗? 您确定在 Gradle 中加载了正确的 proguard 文件吗? 这实际上修复了毕加索错误。应用程序崩溃是因为SearchView 无法加载。我将-keep class android.support.v7.widget.SearchView *; 添加到我的专业守卫规则中,该应用程序运行良好。 这与是否“缩小 OkHTTP”无关。那将是-keep。这里有-dontwarn。 docs 说:“指定不警告未解决的引用和其他重要问题。 […] 忽略警告可能很危险。例如,如果确实需要处理未解析的类或类成员,则处理后的代码将无法正常运行。”但是根据this issue,这些警告确实可以忽略。【参考方案2】:

在你的 proguard.cfg 中试试这些:

#### -- Picasso --
 -dontwarn com.squareup.picasso.**

 #### -- OkHttp --

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

 #### -- Apache Commons --

 -dontwarn org.apache.commons.logging.** 

【讨论】:

谢谢你的回答,我试试这个 我想你想要keep 而不是dontwarn 没有,它构建正确,但在启动时不断崩溃 @cricket_007 我应该怎么keep @Daniele 这个答案所做的就是让警告静音。它不能修复未解决的类【参考方案3】:

已解决在proguard-rules.pro 中添加这一行:

-dontwarn com.squareup.okhttp.**

【讨论】:

【参考方案4】:

在你的应用 gradle 中试试这个

minifyEnabled 假 收缩资源错误

【讨论】:

这个想法不好,你正在尝试禁用Proguard【参考方案5】:

我的问题是我无法使用 minifyEnabled true 生成签名的 apk,我已通过将以下内容添加到 proguard-rules.pro 文件来修复它:

#for Picasso library
-dontwarn okio.
# 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

如果上述问题仍然存在,您可以使用以下方法:

#for Picasso library
-dontwarn com.squareup.okhttp3.**
-keep class com.squareup.okhttp3.*  *;
-dontwarn okio.
-dontwarn okhttp3.internal.platform.ConscryptPlatform
-dontwarn javax.annotation.Nullable
-dontwarn javax.annotation.ParametersAreNonnullByDefault

【讨论】:

以上是关于使用 Picasso 和 minifyEnabled 时应用程序崩溃 true的主要内容,如果未能解决你的问题,请参考以下文章

Android 因 minifyEnabled 和 AWS 调用而崩溃

Android 9 崩溃 minifyEnabled 和 multiDexEnabled

minifyEnabled 删除使用过的资产

Android minifyEnabled true 在调试和发布模式下不起作用

当我使用 minifyEnabled 应用程序时不断崩溃

当我设置 minifyEnabled = true 时,应用程序无法使用 API