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

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.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 18
  • Comments: 16 (4 by maintainers)

Most upvoted comments

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.

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.

I am still having this error is it fixed??

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