Search code examples
androidandroid-proguardjodd

Using Jodd on Android with proguard


Jodd is a fantastic library, but I'm having trouble when using it with the release version of my app with Proguard. It occurs with the following line of code throwing an exception detailed belowL

            BeanCopy.beans(source, destination).copy();

Before the exception however, if I choose not to suppress warnings during the gradle build, several warnings referring to classes not included in the Android runtime come up, for example:

Warning:jodd.util.ClipboardUtil: can't find referenced class java.awt.datatransfer.StringSelection

This is the exception thrown:

11-19 23:45:00.458 16897-16897/com.myfab5.mobile.debug D/AndroidRuntime: Shutting down VM
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime: FATAL EXCEPTION: main
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime: Process: com.myfab5.mobile.debug, PID: 16897
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime: java.lang.ExceptionInInitializerError
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at b.e.c.a(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at b.a.g.a(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at b.a.g.a(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at b.a.g.b(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at b.a.a.a(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at com.myApp.android.services.copyBeans(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at com.google.android.gms.location.internal.zzk$zzb.handleMessage(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6837)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:  Caused by: java.lang.IllegalArgumentException: Invalid module: class b.e.j
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at b.a.a(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:     at b.e.j.a(Unknown Source)
11-19 23:45:00.468 16897-16897/com.myfab5.mobile.debug E/AndroidRuntime:    

Does anyone know how to fix this? Can Jodd be used with Proguard successfully?


Solution

  • Yes :) You have several options:

    • First, you might use jodd-mobile.jar. This mobile version is a small bundle of core, bean and props modules, without all the system classes, like ClipboardUtil (which btw Im going to remove today :)

    • Other solution is to fine-tune proguard to skip the Jodd classes, so you can put something like this in your proguard configuration:

    -keep class javax.** { *; }
    -keep class jodd.** { *; }
    -keep class twitter4j.** { *; }
    
    • If you feel adventurous, you can remove problematic classes from Jodd jars by your own script.

    That's it. Enjoy Jodd!