0

I have fully built app that is already used by people on Google Play. Currently on my to-do list is enabling pro-guard and enable code minification and obfuscation.

I tried enabling it, but I it seems to me that minification does something that breaks my code which I am struggling to understand.

I am able to launch app's login screen and it works just fine and logs me in, however, as soon as the profile page is loaded, this error appears and I can't do anything anymore.

The error is:

javax.xml.stream.FactoryFinder

Here is my 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'
        }
    }

my proguard-rules.pro is default except I added one extra line, so it looks like this:

# 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

please help me out here.

Vitaliy-T
  • 733
  • 6
  • 23
  • You are most likely missing some `-keep` rules for reflection used by your app. If the class you mention is not found at runtime adding `-keep class javax.xml.stream.FactoryFinder { *; }` might help. However if you can give more information about the actual exception/stack trace it will be easier to see what you will need to look closer at. If you have not already take a look at https://developer.android.com/studio/build/shrink-code as well. – sgjesse Dec 14 '20 at 07:35
  • @sgjesse okay, you're a genius, really. I figured out that this was caused by the `Konsume-XML` library that I used by Martyn Vysny. What I did was exactly what you said with that exact command and then I got another error. So I used that same line but with another class and woila! It worked. Thank you very much. If you can, please post an actual answer so I can mark it as answered since you were the one to solve. Mention these 2 classes I had to add: `-keep class javax.xml.stream.FactoryFinder { *; }` `-keep class com.bea.xml.stream.MXParserFactory { *; }` – Vitaliy-T Dec 14 '20 at 19:15
  • Good to hear they you found the root cause. I have added an answer and reported this to the developer of konsume-xml. – sgjesse Dec 16 '20 at 07:31

1 Answers1

1

When missing classes are reported at runtime for a build using R8, usually the issue it that some of the code use reflection in a way such that R8 cannot determine that a class is used by the application.

Sometimes this reflection can be in a library that the app does not have control over, and where the code is unknown. One way of moving forward in such a case is to add a -keep rule on the missing class keeping all members, and continue to do so until the app can run.

In the concrete case for this question the konsume-xml library was missing a few classes javax.xml.stream.FactoryFinder and com.bea.xml.stream.MXParserFactory at runtime, and the following rules brought them back:

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

Note, that this approach is no silver bullet, and in many cases actual knowledge about the reflection used in the library will be required.

In all circumstances, when issues like this happen it is always a good idea to reach out to the library developer so they can add these rules to their library. The library developer might also be able to make the rules more precise based on their knowledge on the actual use of reflection in the library. This issue was opened as a result of this question.

sgjesse
  • 3,793
  • 14
  • 17
  • Thank you very much once again. Also, I did indeed reach out to the developer and informed him about this issue, you can find the post here: https://gitlab.com/mvysny/konsume-xml/-/issues/16 – Vitaliy-T Dec 16 '20 at 10:09
  • Thank you. Now it turns out that this might not be caused by Konsume-XML by itself. At least when I just made a simple sample app I could not reproduce the issue (see https://gitlab.com/mvysny/konsume-xml/-/issues/17). Would it be possible for you to make a simple reproduction based on how you use Konsume-XML, so we can dive down and find the root cause? – sgjesse Dec 17 '20 at 16:34