【问题标题】:Enabling ProGuard and code minification and obfuscation启用 ProGuard 和代码压缩和混淆
【发布时间】:2020-12-13 18:27:14
【问题描述】:

我已完全构建了已在 Google Play 上被人们使用的应用。 目前在我的待办事项清单上是启用 pro-guard 并启用代码缩小和混淆。

我尝试启用它,但在我看来,缩小会破坏我正在努力理解的代码。

我能够启动应用程序的登录屏幕,它工作正常并让我登录,但是,一旦加载个人资料页面,就会出现此错误,我不能再做任何事情了。

错误是:

javax.xml.stream.FactoryFinder

这是我的build.gradle

buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
        debug {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

我的 proguard-rules.pro 是默认的,只是我多加了一行,所以它看起来像这样:

# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
#   http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
#   public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-keepattributes LineNumberTable,SourceFile

请帮帮我。

【问题讨论】:

  • 您很可能错过了一些 -keep 应用程序使用的反射规则。如果在运行时找不到您提到的类,则添加 -keep class javax.xml.stream.FactoryFinder { *; } 可能会有所帮助。但是,如果您可以提供有关实际异常/堆栈跟踪的更多信息,那么您将更容易看到您需要仔细查看的内容。如果你还没有看一下developer.android.com/studio/build/shrink-code
  • @sgjesse 好吧,你真是个天才。我发现这是由 Martyn Vysny 使用的 Konsume-XML 库引起的。我所做的正是您使用该确切命令所说的内容,然后又出现了另一个错误。所以我使用了同一条线,但使用了另一个类和woila!有效。非常感谢。如果可以,请发布一个实际答案,以便我可以将其标记为已回答,因为您是要解决的问题。提到我必须添加的这两个类:-keep class javax.xml.stream.FactoryFinder { *; }-keep class com.bea.xml.stream.MXParserFactory { *; }
  • 很高兴听到他们找到了根本原因。我已经添加了一个答案并将这个报告给了 konsume-xml 的开发者。

标签: android proguard android-r8


【解决方案1】:

当使用 R8 构建时在运行时报告缺少类时,通常是因为某些代码使用反射的方式导致 R8 无法确定应用程序使用了某个类。

有时,这种反射可能位于应用无法控制的库中,并且代码未知。在这种情况下前进的一种方法是在缺少的类上添加-keep 规则以保留所有成员,并继续这样做直到应用程序可以运行。

在这个问题的具体案例中,konsume-xml 库在运行时缺少一些类 javax.xml.stream.FactoryFindercom.bea.xml.stream.MXParserFactory,以下规则将它们带回来:

-keep class javax.xml.stream.FactoryFinder {
  *;
}
-keep class com.bea.xml.stream.MXParserFactory {
  *;
}

请注意,这种方法不是灵丹妙药,在许多情况下,需要有关库中使用的反射的实际知识。

在任何情况下,当发生此类问题时,最好联系库开发人员,以便他们将这些规则添加到他们的库中。库开发人员还可以根据他们对库中反射的实际使用的了解来使规则更加精确。这个issue 是由于这个问题而被打开的。

【讨论】:

  • 再次非常感谢您。另外,我确实联系了开发人员并告知了他这个问题,你可以在这里找到帖子:gitlab.com/mvysny/konsume-xml/-/issues/16
  • 谢谢。现在事实证明,这可能不是由 Konsume-XML 本身引起的。至少当我刚刚制作了一个简单的示例应用程序时,我无法重现该问题(请参阅gitlab.com/mvysny/konsume-xml/-/issues/17)。是否可以根据您使用 Konsume-XML 的方式进行简单的复现,以便我们深入了解根本原因?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
相关资源
最近更新 更多