NoSuchMethodException in Sealed Interface

Hello,
At the moment I am facing the following problem:

We have a sealed interface SomeSealedInterface which has two instances, A and B. Furthermore SomeSealedInterface has a Companion Object, which contains a Function, which should read data from the bundle SomeSealedInterface.

This function was defined inside another interface, from which the companion object inherits.

These classes are extracted from the bundle within a fragment using by navArgs() and made available.
However, in a build with proGuard this function disappears and we get a NoSuchMethodException.
Keep rules to keep interfaces, class did not bring any success.

So far we have found out the following:

  • The error does not occur if we use data class instead of a sealed interface.
  • The error does not occur if we use ProductListFragmentArgs.fromBundle(requireArguments()) instead of by navArgs

I uploaded a small Sample Project to Github (GitHub - TheRealDarkEagle/SomeSealedInterface)

Any help/ideas would be greatly appreciated.

Hello @Kai

Welcome to the Guardsquare community and thanks for asking the question.

A NoSuchMethod exception mostly indicates that ProGuard has shrunk / obfuscated the method while it should not do so. The solution should therefore be a keep rule. To formulate the right keep rule, you can follow the steps below:

  1. Check the stacktrace to find out what method is causing the issue (I expect it to be navArgs()).
  2. Check the mapping.txt file generated by ProGuard during the build to see:
  • if this method name has been obfuscated
  • The information about this method (what package is it coming from, is it inside a public interface, private class, …)
  1. Formulate the keep rule to keep this specific method from being processed by ProGuard using the information from step 1 and 2. For example:
-keepclassmembers public class com.example.MyMain {
    public static void main(java.lang.String[]);
}

Kind regards,

Ewout

Hey @ewoutd ,
thanks for the reply.

There are already some keep Rules added to the project, which did not helped at all.

We also tried to add the following rules:
-dontshrink
-dontoptimize
-dontobfuscate

if i understand this correctly, these rules should completely protect the codebase from proguard customizations and therefor should prevent the error from happening?

you can find the applied Rules inside the Sample Project here

kind regards,

Kai

Dear @Kai ,

Can you elaborate on the results when you applied these 3 don’t options? Did this resolve the issue? You can read about the different things ProGuard will do for you here:

  • The shrinking step detects and removes unused classes, fields, methods, and attributes.
  • The optimizer step optimizes bytecode and removes unused instructions.
  • The obfuscation step renames the remaining classes, fields, and methods using short meaningless names.
  • The final preverification step adds preverification information to the classes, which is required for Java Micro Edition and for Java 6 and higher.

So the only thing that will be done when you use these 3 don’t options will be the preverification step.

If you struggle to target the right part of the code when specifying keep rules, I highly recommend having a look at the ProGuard Playground tool where you can upload your apk/jar file and see how ProGuard will handle your keeprules (what will be kept when you provide a specific keeprule):

Kind regards,
Ewout

1 Like

Hello @ewoutd ,
now that we have investigated this problem further and also disabled all of proGuard’s optimizations, the problem still exists.

This leads me to the conclusion that this problem is not caused by your site.

In my opinion, this thread can be closed.
Thank you for the support you have provided.

Kind regards,
Kai

2 Likes