Amplify-js: [react-native] Android builds fail out of the box (with workarounds)

Created on 6 Mar 2018  路  15Comments  路  Source: aws-amplify/amplify-js

At the moment, the documentation for react-native will not work for android builds.

It simply says to add the NPM packages then react-native link:

> react-native init SomeProject
...
> cd SomeProject
> awsmobile init <existing-project-guid>
...
> yarn add aws-amplify aws-amplify-react-native  # already added by awsmobile init, but being clear
> react-native link
Scanning folders for symlinks in C:\code\skilitics\dev\react-native\MultiLogin\node_modules (22ms)
rnpm-install info Linking aws-amplify-react-native android dependency
rnpm-install info Platform 'android' module aws-amplify-react-native has been successfully linked

but this will cause react-native run-android to fail with:

A problem occurred configuring project ':app'.
> Could not resolve all dependencies for configuration ':app:_debugApkCopy'.
   > Could not find com.google.firebase:firebase-messaging:11.8.0.
     Required by:
         MultiLogin:app:unspecified > MultiLogin:aws-amplify-react-native:unspecified
   > Could not find com.google.firebase:firebase-core:11.8.0.
     Required by:
         MultiLogin:app:unspecified > MultiLogin:aws-amplify-react-native:unspecified

This seems to be because the allprojects section only applies from the top-level build.gradle, so the user must also add the google maven repository:

 allprojects {
     repositories {
         mavenLocal()
         jcenter()
         maven {
             // All of React Native (JS, Obj-C sources, Android binaries) is installed from npm
             url "$rootDir/../node_modules/react-native/android"
         }
+        maven {
+            url "https://maven.google.com"
+        }
     }
 }

With this added, the project builds and installs, but then fails with a redbox error:

Default FirebaseApp is not initialized in this process com.multilogin. Make sure to call FirebaseApp.initializeApp(Context) first.
getInstance
    null:56
getInstance
    null
initialize
    RNPushNotificationModule.java:50
doInitialize
    ModuleHolder.java:198
markInitializable
    ModuleHolder.java:86
notifyJSInstanceInitialized
    NativeModuleRegistry.java:125
run
    CatalystInstanceImpl.java:387
handleCallback
    Handler.java:790
dispatchMessage
    Handler.java:99
dispatchMessage
    MessageQueueThreadHandler.java:31
loop
    Looper.java:164
run
    MessageQueueThreadImpl.java:194
run
    Thread.java:764

The docs say that manually calling FirebaseApp.initializeApp() is not required normally, that it will be done using information in google-services.json, but of course an out-of-box RN app won't have one. Possibly the module could check if FirebaseApp.getApps(Context) is empty?

For now commenting out the new RNPushNotificationPackage() in MainApplication.java gets past this.

From there, cognito login fails with undefined is not an object (evaluating 'RNAWSCognito.computeModPow), since react-native link did not pick up the indirectly linked amazon-cognito-identity-js, so you have to react-native link amazon-cognito-identity-js too, or to have already added it as a direct NPM dependency before react-native link. Unfortunately, react-native link will add duplicate links for already linked packages, if you have already linked aws-amplify-react-native.

So in summary:

  • Add docs for adding the google maven repo (or find an alternative, maybe including them in the repo?)
  • Add docs for getting amazon-cognito-identity-js react-native linked
  • Describe adding a google-services.json for firebase, and handle it not being initialized.
documentation

Most helpful comment

Ok, so I've been digging a bit in the older working versions I have with the non working new one.

Despite updates in both app builds, it seems not be the case. Also, the new gradle build has no affect in the issue.

The only real difference in the MainApplication.java is this line 7 which I commented out after unlinking amplify. Which is added if you link amplify, otherwise, it won't be there. ( I just left it there for comparison)
import com.amazonaws.amplify.pushnotification.RNPushNotificationPackage;
image

Looking at https://aws.github.io/aws-amplify/media/install_n_config for react-native it does not mention to link aws-amplify and neither aws-amplify-react-native
image

So, the only link requirement is react-native link amazon-cognito-identity-js

It now works like a champ.

All 15 comments

Thank you @simonbuchan , we'll get this updated.

I am still having this error is it fixed??

Not sure what you mean? The post describes the extra steps required in Android, the PR is to update the docs to include them.
You could make the case that it shouldn't need anything extra (e.g. merge amazon-cognito-identity-js so react-native link works out of the box, not erroring if there is no firebase info etc...) but I wouldn't say it's broken - it's basically all pretty normal react-native android junk.

@simonbuchan Why does AWS Amplify have a dependency on Firebase? Or am I totally off-base (ha) in that thinking? You say its pretty normal RN android junk, but I have yet to install a library where I've had so many issues getting android to run. That seems at least a little broken to me :/

@jdeanwaite that's because the AWS Pinpoint service uses GCM/FCM to push notifications to Android devices so we need developers to have a corresponding Firebase project.

@powerful23 Ah, I see. I am very new to the Android world, so it was confusing that Firebase would be included because I always saw Firebase and AWS as competing services.

As the documentation improves, hopefully we will run into less of these questions :)

@simonbuchan Ever find a fix for this? I'm trying to implement FCM to my React Native app that utilizes AWS services, getting the same errors when I try doing react-native run-android

@nicer00ster - Quoting myself:

Not sure what you mean? The post describes the extra steps required in Android, the PR is to update the docs to include them.
You could make the case that it shouldn't need anything extra (e.g. merge amazon-cognito-identity-js so react-native link works out of the box, not erroring if there is no firebase info etc...) but I wouldn't say it's broken - it's basically all pretty normal react-native android junk.

Im also getting this error.. I already added

to my build.gradle

"For now commenting out the new RNPushNotificationPackage() in MainApplication.java gets past this " @simonbuchan is there an update to this? what I did was also comment it out, and the error about firebase is removed

About 15 days ago I created a RN app using amplify amd rn- amplify.
I did not install js cognito, Ibthen linked and it worked out of the box.
Yesterday I started a new app just the way I did with previous. It gave me hell for 4 hours until I gave up on it and went with Firebase.
And I attempted just about everything I found online to no avail.

Something must have changed and broke it somehow in that time frime
I will look into what version's worked before amd the one failing now.

Ok, so I've been digging a bit in the older working versions I have with the non working new one.

Despite updates in both app builds, it seems not be the case. Also, the new gradle build has no affect in the issue.

The only real difference in the MainApplication.java is this line 7 which I commented out after unlinking amplify. Which is added if you link amplify, otherwise, it won't be there. ( I just left it there for comparison)
import com.amazonaws.amplify.pushnotification.RNPushNotificationPackage;
image

Looking at https://aws.github.io/aws-amplify/media/install_n_config for react-native it does not mention to link aws-amplify and neither aws-amplify-react-native
image

So, the only link requirement is react-native link amazon-cognito-identity-js

It now works like a champ.

@andythedandyone You don't need to link aws-amplify-react-native if you are not using the push notification feature.

@powerful23 , yes, but the post is talking about out of the box. There are some incongruences to it.
A lot of people care less about pinpoint services and more about commonly used features such as user authentication/Cognito.
Out of the box, the packages work fine. Otherwise, if need some of the other features, there are more steps to go thru.

For notifications, follow this documentation:
https://aws.github.io/aws-amplify/media/push_notifications_setup

So, leaving the impression that one needs to go thru all that settings to get amplify running is not the case.

@andythedandyone yeah I agree for now it will bring some trouble if you link the aws-amplify-react-native but don't follow those steps. We have this in our roadmap to move the push notification module out of the react native package.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cosmosof picture cosmosof  路  3Comments

callmekatootie picture callmekatootie  路  3Comments

benevolentprof picture benevolentprof  路  3Comments

TheRealRed7 picture TheRealRed7  路  3Comments

josoroma picture josoroma  路  3Comments