-1

i am using pcap4j on Android and everything works well on Debug Build. But in release build when activating Proguard/R8 following exception is thrown :

java.lang.IllegalStateException: Unsupported target: class org.pcap4j.packet.IpV4Rfc1349Tos
        at org.pcap4j.packet.factory.PacketFactoryBinder.getPacketFactory(SourceFile:2)
        at org.pcap4j.packet.factory.PacketFactories.getFactory(SourceFile:2)
        at org.pcap4j.packet.IpV4Packet$IpV4Header.<init>(SourceFile:6)
        at org.pcap4j.packet.IpV4Packet.<init>(SourceFile:2)
        at org.pcap4j.packet.IpV4Packet.newPacket(SourceFile:2)
        at org.pcap4j.packet.factory.StaticEtherTypePacketFactory.newInstance(SourceFile:10)
        at org.pcap4j.packet.factory.StaticEtherTypePacketFactory.newInstance(SourceFile:1)
        at org.pcap4j.packet.IpSelector.newPacket(SourceFile:8)

As a workaround for this it is working ok if i add this rule :

-keep class org.pcap4j.packet.**

But with above rule all classes are kept by shrinker and not optimized

My question is how to write more strict rule to shrink pcap4j in maximum and keep functionality without throw exception

I have tried too with below rules but still crashing :

-keepclassmembernames class org.pcap4j.packet.**
-keepclassmembers class org.pcap4j.packet.**
-keepnames class org.pcap4j.packet.**

So another workaround is to keep this 2 classes in this case and everything will work OK :

-keep class org.pcap4j.packet.IpV4Packet
-keep class org.pcap4j.packet.IpV4Rfc1349Tos

But i am asking for a "general" rule, because the app may use other classes like those above in this case.

Thank you

EAK TEAM
  • 5,726
  • 4
  • 30
  • 52

1 Answers1

1

Providing specific rules for a given library typically require some insights into how the library works, and specifically where the library use reflection. In a prefect world the library provider will include the rules needed, see [Development considerations for library modules].(https://developer.android.com/studio/projects/android-library#Considerations).

When R8 (or Proguard) process code it will trace all code reachable from the classes/methods/fields which are specified to be kept by the rules given. This works well for all code except for reflection. When reflection is used the items reflected on are typically expected to be present, and still have their original names. So items reflected upon typically need a keep rule.

Note that in simple cases reflection is handled, e.g.:

  Class.forName("com.example.MyClass")

Here the tracing will trace the class com.example.MyClass, and if obfuscation is enabled the class can also be renamed and the constant string is then rewritten as well.

sgjesse
  • 3,793
  • 14
  • 17