Oct 31, 2012

sbt+ProGuard+addJavascriptInterface = HELL!!

Honestly I really don't understand what's going on, but I have been messed with addJavascriptInterface of WebView(android) used on the sbt project which is using ProGuard for obfuscation for weeks (literally!)
I have guessed the problem was caused by obfuscating class names so that WebView cannot find the method, however proguard options were too tricky.

After try & errors, I finally made it to work adding following setting to Build.scala

      proguardOption in Android := List(
        "-keep public class * extends android.app.Fragment",
        "-keep public class com.test.javaInterface",
        "-keepclassmembers class com.test.javaInterface { <methods> }"
      ).mkString(" ")
The points are
  • -dontobfuscate seems not working. (for my environment at least.)
  • sbt clean seems to be needed every time I changed proguardOption
  • All options should be combined as one line.
  • not only class but classmembers should avoid obfuscation.
I don't think there are very many people do this thing, but I hope this helps somebody. Keep in mind, this information can be very old (today is Oct.31 2012), and this solution may not official (I am afraid it's not). I can only assure you that this works on my project.

No comments: