Cordova-plugin-firebase: Error on second build

Created on 29 Jun 2016  ·  22Comments  ·  Source: arnesson/cordova-plugin-firebase

Hello,
Thank you for this project and your great job!

I have problem in ionic project.
First, I add the string in my config.xml:
<plugin name="cordova-plugin-firebase" spec="https://github.com/arnesson/cordova-plugin-firebase" />
Second, I put google-services.json (which I got from firebase web console) in root directory.
I run my build, and this works:

$ cordova run android
Running command: /home/user/android-app/App/hooks/after_prepare/010_add_platform_class.js /home/user/android-app/App
add to body class: platform-android
**Discovered plugin "cordova-plugin-firebase" in config.xml. Adding it to the project**
Fetching plugin "https://github.com/arnesson/cordova-plugin-firebase" via git clone
Repository "https://github.com/arnesson/cordova-plugin-firebase" checked out to git ref "master".
Installing "cordova-plugin-firebase" for android
ANDROID_HOME=/home/user/Android/Sdk/
JAVA_HOME=/opt/java/jdk1.8.0_92
No target specified, deploying to device 'F9AZCY31J032'.
:preBuild UP-TO-DATE

But my second build always fails...

$ cordova run android
Running command: /home/user/android-app/App/hooks/after_prepare/010_add_platform_class.js /home/user/android-app/App
add to body class: platform-android
Error: Invalid data, chunk must be a string or buffer, not object

I was trying to install plugin by cordova plugin add https://github.com/arnesson/cordova-plugin-firebase.git --save. But in this case even first build crashes with Error: Invalid data, chunk must be a string or buffer, not object

And when I start android application with first build, code below doesn't work for me:

angular.module('app', ['ionic', 'app.controllers', 'app.routes', 'app.services', 'app.directives', 'firebase'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }

    window.FirebasePlugin.getInstanceId(function(token) {
      // save this server-side and use it to push notifications to this device
      console.log("Token", token);
    }, function(error) {
      console.error(error);
    });

  });
})

This code returns ["Token", "null"] to console.

Thank you in advance.

All 22 comments

do you get any dupes or strange values in platforms/andoid/res/values/strings.xml?

try running cordova prepare android before building (although it should work without it)

Same issue here. In strings.xml i have:

    <string name="google_app_id">727247059290</string>
    <string name="google_app_id">@string/google_app_id</string>
    <string name="google_api_key">@string/google_api_key</string>

Remove google_app_id from string.xml and try again. I have removed and it is working for me.

How about ios? do you have any idea?

Nope. I don't have any idea for iOS. I think you need to add GoogleService-Info.plist file in iOS project and install Pod.
For you, is it started working in Android now?

Nope, i am getting Error: invalid data when i try to emulate it. I guess that the cordova prepare hook is changing the file back.

But i am more interested in making it work in ios. In ios i have a build issue: https://github.com/arnesson/cordova-plugin-firebase/issues/11

@raducostea - I suggest you to use APN (Apple Push Notification). Here is the link of plugin: https://github.com/phonegap-build/PushPlugin. It is working very accurately for me in iOS.

@dhruv270 i am already using that. I want this plugin for the analytics part to record events and add them inside google analytics.

Error: Invalid data appeared because of the google-services.json and googleService-info.plist copy to platforms folder , if the platforms folder and json, plist doesn't exist it shows error.
I fixed by adding the platforms folder and json , plist file. Sorry for my bad english

Yes, you need to create project at firebase console. And add application in that project by adding your application package name / bundle id. You will get google-services.json for Android and GoogleService-Info.plist for iOS. That file you need to add in your project.

make sure you put the files in the ROOT of your project, alongside config.xml etc, not in e.g. platforms or www.

@raducostea @dhruv270 Regarding APN and push, I would suggest using this plugin for that as well. PushPlugin will eventually switch to Firebase FCM as well (since GCM is deprecated). FCM uses APN for IOS so its the same thing, but you get the benefit of a common interface across all platforms

Already did that but i am getting compilation errors. Check https://github.com/arnesson/cordova-plugin-firebase/issues/11

I don't have any dupes or strange values in strings.xml.
I reinstalled cordova-plugin-firebase and made "cordova prepare". After that I was seeing new values in strings.xml:

<string name="google_app_id">@string/google_app_id</string>
<string name="google_api_key">@string/google_api_key</string>

And when I build the same error again:
Error: Invalid data, chunk must be a string or buffer, not object

Are you added both ios and android platform ?

Once I build android , i got this error

**UNEXPECTED TOP-LEVEL EXCEPTION:
com.android.dex.DexException: Multiple dex files define Lcom/google/firebase/FirebaseException;
at com.android.dx.merge.DexMerger.readSortableTypes(DexMerger.java:579)
at com.android.dx.merge.DexMerger.getSortedTypes(DexMerger.java:535)
at com.android.dx.merge.DexMerger.mergeClassDefs(DexMerger.java:517)
at com.android.dx.merge.DexMerger.mergeDexes(DexMerger.java:164)
at com.android.dx.merge.DexMerger.merge(DexMerger.java:188)
at com.android.dx.command.dexer.Main.mergeLibraryDexBuffers(Main.java:504)
at com.android.dx.command.dexer.Main.runMonoDex(Main.java:334)
at com.android.dx.command.dexer.Main.run(Main.java:277)
at com.android.dx.command.dexer.Main.main(Main.java:245)
at com.android.dx.command.Main.main(Main.java:106)

FAILED**

I have the same issue as @neosekar . Any ideas?

EDIT:

Found more about this on internet:

Seems like we reached the method count limit due to the big dependencies. I will try to find a fix.

EDIT 2:

I fixed it by doing the following:

  • Remove line 5,6,7 from /plugins/cordova-plugin-firebase/src/android/build.gradle
  • Remove line 1 from /plugins/cordova-plugin-firebase/src/android/build-extras.gradle
  • Remove /platforms/android folder
  • Add platform (cordova platform add android)
  • Build/Run (cordova run android --device)

However I think this only works when you already have the 'com.google.gms.google-services' installed by another plugin.

Regarding Multiple dex files define Lcom/google/firebase/FirebaseException:

This is caused by duplicate dependencies, most likely another plugin which is also using com.google.gms:google-services, Firebase SDK or parts of it. I have not tested this myself but you should be able to list all dependencies used in your project and find the duplicates:
http://stackoverflow.com/questions/30648172/gradle-library-duplicates-in-dependencies

Having a GoogleService-Info.plist when there's no ios platform will cause the "second build error". I'd recommend GoogleService-Info.plist is copied only if platform/ios exists....

You can patch after_prepare.js to work around this, though I've not tested my patch yet. I'll post it if it works

@st8st8 The problem is already fixed (on the git version)

...
if(fileExists("GoogleService-Info.plist") && directoryExists("platforms/ios/")){
...

I came across the same issue just now and after a while just redownloaded both the GoogleService-Info.plist and google-services.json files and it worked just fine.

I facing the same issue with ionic. I fix that by remove platform and plugin then add again platform and plugin, then everything is completely normal again.
For this issue in ionic 2 I write this tutorial.

Reinstalling cordova-plugin-firebase worked for me

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

chrissterling picture chrissterling  ·  3Comments

arunkatariaoodles picture arunkatariaoodles  ·  4Comments

dhavalsoni2001 picture dhavalsoni2001  ·  5Comments

LKruglov picture LKruglov  ·  4Comments