Wednesday, July 25, 2012

Proguarding your PhoneGap Android App

If you've ever run into the following error when trying to run Proguard against your Android application using PhoneGap:
Proguard returned with error code 1. See console Warning: org.apache.cordova.CameraLauncher: can't find referenced class org.apache.commons.codec.binary.Base64 Warning: org.apache.cordova.CameraLauncher: can't find referenced class org.apache.commons.codec.binary.Base64 Warning: org.apache.cordova.CordovaWebView: can't find referenced method 'WebView(android.content.Context,android.util.AttributeSet,int,boolean)' in class android.webkit.WebView Warning: org.apache.cordova.CordovaWebViewClient: can't find referenced method 'android.webkit.WebResourceResponse shouldInterceptRequest(android.webkit.WebView,java.lang.String)' in class android.webkit.WebViewClient Warning: org.apache.cordova.CordovaWebViewClient: can't find referenced class android.webkit.WebResourceResponse Warning: org.apache.cordova.CordovaWebViewClient: can't find referenced class android.webkit.WebResourceResponse Warning: org.apache.cordova.CordovaWebViewClient: can't find referenced class android.webkit.WebResourceResponse Warning: org.apache.cordova.CordovaWebViewClient: can't find referenced class android.webkit.WebResourceResponse Warning: org.apache.cordova.CordovaWebViewClient: can't find referenced class android.webkit.WebResourceResponse Warning: org.apache.cordova.FileUtils: can't find referenced class org.apache.commons.codec.binary.Base64 Warning: org.apache.cordova.FileUtils: can't find referenced class org.apache.commons.codec.binary.Base64 Warning: there were 9 unresolved references to classes or interfaces. You may need to specify additional library jars (using '-libraryjars'). Warning: there were 2 unresolved references to program class members. Your input classes appear to be inconsistent. You may need to recompile them and try again. Alternatively, you may have to specify the option '-dontskipnonpubliclibraryclassmembers'. java.io.IOException: Please correct the above warnings first. at proguard.Initializer.execute(Initializer.java:321) at proguard.ProGuard.initialize(ProGuard.java:211) at proguard.ProGuard.execute(ProGuard.java:86) at proguard.ProGuard.main(ProGuard.java:492)
It can be resolved by adding the following lines to your proguard.cfg file:

-keep public class * extends com.phonegap.api.Plugin 
-keep public class * extends org.apache.cordova.api.Plugin 
-keep public class org.apache.cordova.DroidGap
-keep public class org.apache.cordova.**
-libraryjars /Path/To/PhoneGap/libs/commons-codec.jar
-dontwarn android.webkit.*


I can't take credit for this as it was passed onto me by Nasir Hussain who figured it out while trying to proguard his own app. He thought that others reading this blog may find the information useful. Thanks Nasir!

7 comments:

  1. Thank you Simon, this code fixed an export problem with plugin classes not being included in the final apk.

    ReplyDelete
  2. I think the latest android-bundle renamed that file to proguard-projects.txt

    ReplyDelete
  3. and now the latest version of phonegap ..the file to be edited is: proguard-android-optimize.txt

    ReplyDelete
  4. Thank you Simon.I have I just one question...is there a some plugin for cordova/phonegap which can discover all device connected on (my) local wifi network

    ReplyDelete
  5. @Sabiha Zirić

    I'm not aware of any such plugin. The code required to do this is possible but not very portable between OS's.

    ReplyDelete
  6. cordova proguard plugin: https://github.com/greybax/cordova-plugin-proguard

    ReplyDelete