Expo 51.0.8执行assembleRelease出现大量警告,请求技术支持
问题描述
使用Expo 51.0.8执行./gradlew assembleRelease构建Release包时,触发大量警告,核心类型包括:
- 全局变量未声明(如
URL、fetch、Promise等浏览器/React Native全局对象) Direct call to eval(), but lexical scope is not supported代码执行警告
关键构建日志片段:
./gradlew assembleRelease
Configure project :expo
Using expo modules
- expo-asset (10.0.6)
- expo-constants (16.0.1)
- expo-file-system (17.0.1)
- expo-font (12.0.5)
- expo-keep-awake (13.0.2)
- expo-linear-gradient (13.0.2)
- expo-modules-core (1.12.11)
- expo-splash-screen (0.27.4)
- expo-sqlite (14.0.3)
- expo-system-ui (3.0.4)
- expo-web-browser (13.0.3)
Configure project :react-native-reanimated
Android gradle plugin: 8.2.1
Gradle: 8.6
Task :app:createBundleReleaseJsAndAssets
warning: Bundler cache is empty, rebuilding (this may take a minute)
Writing bundle output to: android\app\build\generated\assets\createBundleReleaseJsAndAssets\index.android.bundle
...
android\app\build\generated\assets\createBundleReleaseJsAndAssets\index.android.bundle:763:63: warning: Direct call to eval(), but lexical scope is not supported.
...typeof expo === "undefined" ? eval("require") : function (moduleId) {
^~~~~~~~~~~~~~~
android\app\build\generated\assets\createBundleReleaseJsAndAssets\index.android.bundle:843:24: warning: the variable "URL" was not declared in function "_fetch"
props[0] = new URL(props[0], window.location?.origin).toString();
^~~
...(其余警告略)
解决方案
1. 声明全局变量消除未定义警告
这类警告源于Babel未识别React Native/Expo内置的全局对象,通过Babel配置显式声明即可解决:
- 确保已安装
@babel/plugin-transform-runtime - 修改项目根目录
babel.config.js,添加全局变量映射:
module.exports = function(api) { api.cache(true); return { presets: ['babel-preset-expo'], plugins: [ [ '@babel/plugin-transform-runtime', { globals: { 'URL': true, 'fetch': true, 'Promise': true, 'setImmediate': true, 'setTimeout': true, 'clearTimeout': true, 'Headers': true, 'Request': true, 'Response': true, 'FileReader': true, 'Blob': true, 'FormData': true, 'URLSearchParams': true, 'AbortController': true, 'XMLHttpRequest': true, 'self': true, 'performance': true, 'navigator': true, 'MessageChannel': true, 'requestAnimationFrame': true, 'cancelAnimationFrame': true, 'location': true, 'queueMicrotask': true, 'structuredClone': true, 'document': true, 'HTMLElement': true, 'MutationObserver': true, 'getComputedStyle': true, 'SharedArrayBuffer': true } } ] ] }; };
2. 抑制eval相关警告
eval警告多来自Reanimated等库的worklet代码,通过Metro配置压缩器忽略该类警告:
- 修改
metro.config.js:
const { getDefaultConfig } = require('expo/metro-config'); const config = getDefaultConfig(__dirname); config.transformer.minifierConfig = { compress: { drop_console: true, warnings: false, pure_funcs: ['eval'] // 标记eval为纯函数,消除警告 }, mangle: { keep_fnames: true } }; module.exports = config;
- 确保Reanimated版本与Expo 51兼容,建议升级到
~3.10.1
3. 清理缓存重新构建
旧缓存可能导致配置不生效,执行以下命令彻底清理后重新构建:
npx expo start --clear cd android && ./gradlew clean && ./gradlew assembleRelease
4. 移除冗余测试代码
日志中出现jest相关警告,检查项目中是否残留测试文件或配置,移除Release构建不需要的测试代码。
验证
重新执行./gradlew assembleRelease,确认警告数量减少或完全消失,成功生成Release安装包。
内容的提问来源于stack exchange,提问作者VARUNKUMAR V

