Home > Blockchain >  Expo Asset library works in debug but not in release (Expo ejected project/React Native)
Expo Asset library works in debug but not in release (Expo ejected project/React Native)

Time:09-30

I try to get a .jpg file absolute path on Android. I'm using Expo Asset library for this. While my application runs in debug mode, everything is working as expected. The Expo Asset library returns my file with the correct localUri path but in release mode the localUri property contains only the name of the file and not the actual path. As an example on debug the localUri contains the following: file:///data/user/0/com.test.app/cache/ExponentAsset-10f8d3f8108915f49694bdd86e85fcbd.jpg while in release the same property will contain assets_bg_img

The following line is the request:

backgroundUri = await Asset.loadAsync(require('../../assets/bg_img.jpg'));

And this is the commands I use to build my release apk:

cd android
./gradlew assembleRelease

This is the commands I use to run the app in debug:

npx react-native start --port 8084 --reset-cache
npx react-native run-android --port 8084

Is there a way to make this work? Or is there a better solution to get a file absolute path on Android?

CodePudding user response:

Building in release mode just like that is not enough in my experience. You need to follow these steps when building for release:

  1. ./gradlew clean
  2. react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res/
  3. ./gradlew assembleRelease

As you can see, bundling is required first in order to successfully package everything for release.

CodePudding user response:

Found a solution using the Expo File System library:

const imgUri = `${FileSystem.cacheDirectory}/bg_img.jpg`;
await FileSystem.copyAsync({from: 'asset://assets/images/bg_img.jpg', to: imgUri });

I also created an asset image folder under: /android/app/src/main/assets/images and added my .jpg file there.

  • Related