1

I am using expo 43 with Amplify when i run the app in mobile using

expo run:android

I can see my application running on mobile.

But when i try to create the build for same

eas build -p android

i am getting below error

Gradle build failed with unknown error. Please see logs for the "Run gradlew" phase

here the eas json

{
  "cli": {
    "version": ">= 0.46.0"
  },
  "build": {
    "development": {
      "distribution": "internal",
      "android": {
        "gradleCommand": ":app:assembleDebug"
      },
      "ios": {
        "buildConfiguration": "Debug"
      }
    },
    "preview": {
      "distribution": "internal"
    },
    "production": {}
  },
  "submit": {
    "production": {}
  }
}

Here's the log for eas build

[stderr]   40 | import { enableScreens } from "react-native-screens";
Error: Unable to resolve module ./aws-exports from /root/workingdir/build/App.js: 
None of these files exist:
  * aws-exports(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
  * aws-exports/index(.native|.android.ts|.native.ts|.ts|.android.tsx|.native.tsx|.tsx|.android.js|.native.js|.js|.android.jsx|.native.jsx|.jsx|.android.json|.native.json|.json)
  40 | import { enableScreens } from "react-native-screens";
  41 | import Amplify from 'aws-amplify'
> 42 | import config from './aws-exports'

i have the aws-exports.js in root directory and it working for expo run:android

colinux
  • 3,989
  • 2
  • 20
  • 19
Ahmed Anwar
  • 125
  • 1
  • 1
  • 12

2 Answers2

5

The root problem is shown in the log : EAS build can't resolve module ./aws-exports.

There are 2 reasons for this :

  • a) this file is ignored in your repository because it contains sensitive data (this is the default config made by amplify in .gitignore)
  • b) EAS upload a copy of the repository on their servers, so because of a) aws-exports.js is not copied. Hence the build error.

I solve this by using the EAS Secret feature. The trick is to base64 encode the content of aws-exports.js in a variable, saying AWS_EXPORTS. This variable can be used in a pre-install hook named eas-build-pre-install, so we can tell EAS to decode the value and write it back in aws-exports.js, so the file will be available at build time.

This is done by adding these 2 scripts in package.json :

 "aws-export": "echo 'Update Expo AWS_EXPORTS secret with value: \n\n' && cat aws-exports.js |base64"

Run aws-export script, then set the encoded content via Secrets tab on Expo website, or with eas secret:create command. Don't forget to update the value each time you change something in amplify to reflect the local changes in aws-exports.js !

Then the pre-install hook will decode the value (this is automatically run by EAS) :

"eas-build-pre-install": "echo $AWS_EXPORTS | base64 -d > ./aws-exports.js"

Run the build again.

colinux
  • 3,989
  • 2
  • 20
  • 19
  • Thanks a lot @colinux the above solution works for me. I had faced few more issues after that, will share the steps in seperate answer shortly. – Ahmed Anwar Feb 05 '22 at 14:26
0

As mentioned by colinux EAS upload a copy of the repository on their servers and since aws-exports is a part of .gitignore, it was not uploaded at the server and hence we have the build error. Please read colinux's answer for more details on this.

The solution here is to add these 2 lines in your package JSON.

1. "aws-export": "echo 'Update Expo AWS_EXPORTS secret with value: \n\n' && cat aws-exports.js |base64"
2. "eas-build-pre-install": "echo $AWS_EXPORTS | base64 -di > ./aws-exports.js"

Note: There a small change here in step 2. instead of base64 -d, we have to use base64 -di. This is because the older cli was not able to deocde newline and other chars. You can check more details here: decode base64: invalid input This part resolves the build issue i posted. I was getting few more build errors realted to app:mergeReleaseResources FAILED

This link should help you out in case of Duplicate resources error: Duplicate resources error

After resolving these errors you should get a successfule build from EAS.

And in case you want an .apk file instead of .aap. Please create a dev profile as mentioned below: Configuring a profile to build apks

Ahmed Anwar
  • 125
  • 1
  • 1
  • 12
  • I tried your method and didn't work out for me...I added the 2 lines and still got the same error, I posted my side of the issue I'd appreciate if you can help out in any way – coolDog Jul 04 '22 at 17:51