【问题标题】:Expo Asset library works in debug but not in release (Expo ejected project/React Native)Expo Asset 库在 debug 中工作,但在 release 中工作(Expo 弹出项目/React Native)
【发布时间】:2021-09-30 08:58:59
【问题描述】:

我尝试在Android 上获取.jpg 文件绝对路径。我为此使用Expo Asset 库。虽然我的应用程序在 debug mode 中运行,但一切都按预期工作。 Expo Asset 库以正确的 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上获取文件绝对路径?

【问题讨论】:

    标签: android react-native expo


    【解决方案1】:

    根据我的经验,像这样在发布模式下构建是不够的。在构建发布时,您需要遵循以下步骤:

    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

    如您所见,首先需要捆绑才能成功打包所有内容以进行发布。

    【讨论】:

    • 我用了上面的命令,结果一样。
    【解决方案2】:

    使用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 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-25
      • 2019-08-07
      • 1970-01-01
      • 2019-02-28
      • 2021-12-26
      • 1970-01-01
      • 2020-04-05
      相关资源
      最近更新 更多