React Native集成Expo后打包Release APK遇AssertionError求助
错误信息
AssertionError [ERR_ASSERTION]: Assets must have hashed files. Ensure the expo-asset plugin is installed.
Task :app:createBundleReleaseJsAndAssets FAILED
FAILURE: Build failed with an exception.
- What went wrong:
Execution failed for task ':app:createBundleReleaseJsAndAssets'.Process 'command 'node'' finished with non-zero exit value 1
- Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org.Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.
You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.
For more on this, please refer to https://docs.gradle.org/8.3/userguide/command_line_interface.html#sec:command_line_warnings in the Gradle documentation.
BUILD FAILED in 2m 17s
解决方案
- 安装并配置expo-asset插件
- 安装依赖:
npx expo install expo-asset - 修改
babel.config.js,添加插件:module.exports = { presets: ['module:metro-react-native-babel-preset'], plugins: ['expo-asset/babel'], };
- 安装依赖:
- 配置Metro以处理Expo资产
在metro.config.js中更新配置,确保资产解析正确:const { getDefaultConfig } = require('expo/metro-config'); const config = getDefaultConfig(__dirname); // 根据你的项目资产类型添加对应的扩展名 config.resolver.assetExts.push('db', 'mp3', 'ttf', 'png'); module.exports = config; - 清理构建缓存
执行命令清除旧缓存:npx react-native clean cd android && ./gradlew clean - 重新构建Release包
清理完成后重新执行构建:cd android && ./gradlew assembleRelease
内容的提问来源于stack exchange,提问作者Engr.Aftab Ufaq

