启用 ProGuard 和代码缩小和混淆

Enabling ProGuard and code minification and obfuscation

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

请帮帮我。

如果在 运行 时报告使用 R8 构建缺少 classes,通常问题是某些代码使用反射的方式使得 R8 无法确定 class 被应用程序使用。

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

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

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

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

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