Cordova 5.3.3 Android app crashes on calling 'registerDevice' for push notifications

621 Views Asked by At

Problem

I'm building an Android app using Cordova 5.3.3 (latest), and using the pushwoosh cordova plugin for push notifications ( https://github.com/Pushwoosh/pushwoosh-phonegap-3.0-plugin ).

The app also uses the facebook-connect plugin ( https://github.com/Wizcorp/phonegap-facebook-plugin )

The pushwoosh plugin seems to conflict with the facebook connect plugin. When the two plugins are added to a new cordova project, the application build starts failing. This is the message:

:compileDebugJava
:preDexDebug
:dexDebug

UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
    at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
    at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
    at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
    at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
    at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
    at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
    at com.android.dx.command.dexer.Main.run(Main.java:246)
    at com.android.dx.command.dexer.Main.main(Main.java:215)
    at com.android.dx.command.Main.main(Main.java:106)

:dexDebug FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':dexDebug'.
> com.android.ide.common.internal.LoggedErrorException: Failed to run command:
    /Users/shishir.srivastava/Development/android-sdk-macosx/build-tools/22.0.1/dx --dex --no-optimize --output /Users/shishir.srivastava/Shishir/MallMate/Code/MallMateCordova-temp/platforms/android/build/intermediates/dex/debug --input-list=/Users/shishir.srivastava/Shishir/MallMate/Code/MallMateCordova-temp/platforms/android/build/intermediates/tmp/dex/debug/inputList.txt
  Error Code:
    2
  Output:

    UNEXPECTED TOP-LEVEL EXCEPTION:
    com.android.dex.DexException: Multiple dex files define Landroid/support/annotation/AnimRes;
        at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:596)
        at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:554)
        at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:535)
        at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:171)
        at com.android.dx.merge.DexMerger.merge(DexMerger.java:189)
        at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:454)
        at com.android.dx.command.dexer.Main.runMonoDex(Main.java:303)
        at com.android.dx.command.dexer.Main.run(Main.java:246)
        at com.android.dx.command.dexer.Main.main(Main.java:215)
        at com.android.dx.command.Main.main(Main.java:106)



* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Fix

To fix the above issue, I added the following to build-extras.gradle file in the android folder.

configurations {
    all*.exclude group: 'com.android.support', module: 'support-v4'
} 

This allowed the app to build without any issues. However, now, the app crashes when pushNotification.registerDevice() is called.

I used adb shell logcat to check the error logs, and saw this:

D/PushNotifications( 4751): Plugin Called
D/Pushwoosh( 4751): broadcastPush = true
I/Pushwoosh( 4751): Log level: NOISE
V/Pushwoosh( 4751): [RegistrationPrefs] Saving regId on app version 178
I/Pushwoosh( 4751): [PushManager] App ID: #####-#####
I/Pushwoosh( 4751): [PushManager] Project ID: 1088448295557
I/Pushwoosh( 4751): [PushManager] This is android device
W/ActivityManager(  943): Unable to start service Intent { cmp=com.shishir/com.pushwoosh.inapp.InAppRetrieverService } U=0: not found
V/Pushwoosh( 4751): [PushRegistrarGCM] number of receivers for com.shishir: 3
V/Pushwoosh( 4751): [PushRegistrarGCM] Found 1 receivers for action com.google.android.c2dm.intent.RECEIVE
W/Pushwoosh( 4751): [RequestManager] Try To sent: registerUser
W/PluginManager( 4751): THREAD WARNING: exec() call to PushNotification.onDeviceReady blocked the main thread for 61ms. Plugin should use CordovaInterface.getThreadPool().
I/chromium( 4751): [INFO:CONSOLE(1)] "pushwoosh initialized", source: file:///android_asset/www/app.js (1)
D/PushNotifications( 4751): Plugin Called
W/Pushwoosh( 4751): [RequestManager] Pushwoosh Request: {"request":{"v":"3.1.8.563","device_type":3,"application":"#####-#####","hwid":"###","userId":"###"}}
W/Pushwoosh( 4751): [RequestManager] Pushwoosh Request To: https://cp.pushwoosh.com/json/1.3/registerUser
V/Pushwoosh( 4751): [com.pushwoosh.GCMRegistrationService] Intent action = com.pushwoosh.gcm.intent.REGISTER
I/chromium( 4751): [INFO:CONSOLE(1)] "pushwoosh: post registration", source: file:///android_asset/www/app.js (1)
W/cr.BindingManager( 4751): Cannot call determinedVisibility() - never saw a connection for the pid: 4751
I/chromium( 4751): [INFO:CONSOLE(1)] "[object Object]", source: file:///android_asset/www/app.js (1)
E/AndroidRuntime( 4751): FATAL EXCEPTION: IntentService[com.pushwoosh.GCMRegistrationService]
E/AndroidRuntime( 4751): Process: com.shishir, PID: 4751
E/AndroidRuntime( 4751): java.lang.NoSuchMethodError: No virtual method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in /data/app/com.shishir-1/base.apk)
E/AndroidRuntime( 4751):    at com.google.android.gms.iid.zzd.zzdo(Unknown Source)
E/AndroidRuntime( 4751):    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
E/AndroidRuntime( 4751):    at com.google.android.gms.iid.zzd.<init>(Unknown Source)
E/AndroidRuntime( 4751):    at com.google.android.gms.iid.InstanceID.zza(Unknown Source)
E/AndroidRuntime( 4751):    at com.google.android.gms.iid.InstanceID.getInstance(Unknown Source)
E/AndroidRuntime( 4751):    at com.pushwoosh.GCMRegistrationService.register(Unknown Source)
E/AndroidRuntime( 4751):    at com.pushwoosh.GCMRegistrationService.onHandleIntent(Unknown Source)
E/AndroidRuntime( 4751):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
E/AndroidRuntime( 4751):    at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime( 4751):    at android.os.Looper.loop(Looper.java:211)
E/AndroidRuntime( 4751):    at android.os.HandlerThread.run(HandlerThread.java:61)
D/ActivityManager(  943): New dropbox entry: com.shishir, data_app_crash, ####
W/ActivityManager(  943):   Force finishing activity 1 com.shishir/.MainActivity

From what I understand, this seems to be the mail reason behind the crash:

java.lang.NoSuchMethodError: No virtual method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Landroid/support/v4/content/ContextCompat; or its super classes (declaration of 'android.support.v4.content.ContextCompat' appears in /data/app/com.shishir-1/base.apk)

I'm a little lost as to how I should proceed from here. Any help will be greatly appreciated.

Thank you!

1

There are 1 best solutions below

0
On BEST ANSWER

After much toil, I was able to figure out the root cause of the issue. I'm posting it here with the hope that it'll be of some help to the next person who faces the same issue.

I realised that the facebook connect plugin that I was using was using an outdated version of FB SDK.

I switched to this one, and the problem was resolved: https://github.com/jeduan/cordova-plugin-facebook4