首页 > 解决方案 > 启用 ProGuard 和代码压缩和混淆

问题描述

我已经完全构建了 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

请帮帮我。

标签: androidproguardandroid-r8

解决方案


当使用 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 {
  *;
}

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

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


推荐阅读