Expo Asset 库在调试中工作但在发布时不工作(Expo 弹出 project/React Native)
Expo Asset library works in debug but not in release (Expo ejected project/React Native)
我尝试在 Android
上获取 .jpg
文件绝对路径。为此,我正在使用 Expo Asset
库。虽然我的应用程序 运行s 在 debug mode
中,但一切都按预期工作。 Expo Asset
库 returns 我的文件具有正确的 localUri
路径但在 release mode
中 localUri
属性 仅包含文件名而不是实际路径。
作为 debug
上的示例,localUri
包含以下内容:file:///data/user/0/com.test.app/cache/ExponentAsset-10f8d3f8108915f49694bdd86e85fcbd.jpg
而在 release
中,相同的 属性 将包含 assets_bg_img
下面一行是请求:
backgroundUri = await Asset.loadAsync(require('../../assets/bg_img.jpg'));
这是我用来构建 release
apk 的命令:
cd android
./gradlew assembleRelease
这是我用于 运行 debug
中的应用程序的命令:
npx react-native start --port 8084 --reset-cache
npx react-native run-android --port 8084
有没有办法让它工作?或者是否有更好的解决方案来获取 Android 上的文件绝对路径?
根据我的经验,仅仅像这样在发布模式下构建是不够的。构建发布时需要遵循以下步骤:
./gradlew clean
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/
./gradlew assembleRelease
如您所见,首先需要捆绑才能成功打包所有内容以供发布。
使用 Expo File System
库找到解决方案:
const imgUri = `${FileSystem.cacheDirectory}/bg_img.jpg`;
await FileSystem.copyAsync({from: 'asset://assets/images/bg_img.jpg', to: imgUri });
我还在以下位置创建了一个资产图像文件夹:/android/app/src/main/assets/images
并在其中添加了我的 .jpg
文件。
我尝试在 Android
上获取 .jpg
文件绝对路径。为此,我正在使用 Expo Asset
库。虽然我的应用程序 运行s 在 debug mode
中,但一切都按预期工作。 Expo Asset
库 returns 我的文件具有正确的 localUri
路径但在 release mode
中 localUri
属性 仅包含文件名而不是实际路径。
作为 debug
上的示例,localUri
包含以下内容:file:///data/user/0/com.test.app/cache/ExponentAsset-10f8d3f8108915f49694bdd86e85fcbd.jpg
而在 release
中,相同的 属性 将包含 assets_bg_img
下面一行是请求:
backgroundUri = await Asset.loadAsync(require('../../assets/bg_img.jpg'));
这是我用来构建 release
apk 的命令:
cd android
./gradlew assembleRelease
这是我用于 运行 debug
中的应用程序的命令:
npx react-native start --port 8084 --reset-cache
npx react-native run-android --port 8084
有没有办法让它工作?或者是否有更好的解决方案来获取 Android 上的文件绝对路径?
根据我的经验,仅仅像这样在发布模式下构建是不够的。构建发布时需要遵循以下步骤:
./gradlew clean
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/
./gradlew assembleRelease
如您所见,首先需要捆绑才能成功打包所有内容以供发布。
使用 Expo File System
库找到解决方案:
const imgUri = `${FileSystem.cacheDirectory}/bg_img.jpg`;
await FileSystem.copyAsync({from: 'asset://assets/images/bg_img.jpg', to: imgUri });
我还在以下位置创建了一个资产图像文件夹:/android/app/src/main/assets/images
并在其中添加了我的 .jpg
文件。