Menu

How to stop devirtualization for specific classes

Help
FxT
2019-10-30
2019-11-03
  • FxT

    FxT - 2019-10-30

    How do you stop proguard from devirtualizing a class's members?

    When using
    -keep com.myPackage.JVMSpecificCode

    proguard keeps the public method name and signature but changes it from an instance method to a static method. While normally this is a great optimization for methods that do not access instance state it is causing problems for my particular scenario resulting in the following error:

    java.lang.IncompatibleClassChangeError: Expected static method....

    I am applying proguard on a mult-release jar file where 99% of the code is JVM agnostic. This is done by:
    1) Classes under META-INF/versions are filtered out
    2) Classes that containing JVM specific code is identified and placed into a -keep list in a generated proguard config file.

    However proguard still applies devirtualization on classes listed with the keep parameter, while the filtered out classes under META-INF/versions are still instance methods, causing errors when run with JVM that pulls classes from META-INF/version instead of the jar root.

     
  • Eric Lafortune

    Eric Lafortune - 2019-11-03

    You should not just specify the class, but also its methods. The following should work:

    -keep,allowshrinking class com.myPackage.JVMSpecificCode { *; }