React Native集成Detox构建失败,求解决方案
React Native集成Detox构建失败排查方案
问题描述
我在React Native项目中集成Detox,按配置步骤操作后构建环节失败(截图:
)。但单独执行命令
cd android ; ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug ;可正常运行,以下是我的.detoxrc.js配置代码:
/** @type {Detox.DetoxConfig} */ module.exports = { testRunner: { args: { '$0': 'jest', config: 'e2e/jest.config.js' }, jest: { setupTimeout: 120000 } }, apps: { 'ios.debug': { type: 'ios.app', binaryPath: 'ios/build/Build/Products/Debug-iphonesimulator/YOUR_APP.app', build: 'xcodebuild -workspace ios/YOUR_APP.xcworkspace -scheme YOUR_APP -configuration Debug -sdk iphonesimulator -derivedDataPath ios/build' }, 'ios.release': { type: 'ios.app', binaryPath: 'ios/build/Build/Products/Release-iphonesimulator/YOUR_APP.app', build: 'xcodebuild -workspace ios/YOUR_APP.xcworkspace -scheme YOUR_APP -configuration Release -sdk iphonesimulator -derivedDataPath ios/build' }, 'android.debug': { type: 'android.apk', binaryPath: 'android/app/build/outputs/apk/debug/app-debug.apk', build: 'cd android ; ./gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug ; cd -', /*reversePorts: [ 8081 ]*/ }, 'android.release': { type: 'android.apk', binaryPath: 'android/app/build/outputs/apk/release/app-release.apk', build: 'cd android ; ./gradlew assembleRelease assembleAndroidTest -DtestBuildType=release ; cd -' } }, devices: { simulator: { type: 'ios.simulator', device: { type: 'iPhone 12' } }, attached: { type: 'android.attached', device: { adbName: '.*' } }, emulator: { type: 'android.emulator', device: { avdName: 'Pixel_3a_API_30' } } }, configurations: { 'ios.sim.debug': { device: 'simulator', app: 'ios.debug' }, 'ios.sim.release': { device: 'simulator', app: 'ios.release' }, 'android.att.debug': { device: 'attached', app: 'android.debug' }, 'android.att.release': { device: 'attached', app: 'android.release' }, 'android.emu.debug': { device: 'emulator', app: 'android.debug' }, 'android.emu.release': { device: 'emulator', app: 'android.release' } } };
解决方案
- 调整Android构建命令:将
.detoxrc.js中android.debug和android.release的build字段修改为直接调用Gradle,避免路径切换问题:build: './android/gradlew assembleDebug assembleAndroidTest -DtestBuildType=debug' - 确保在项目根目录执行命令:运行
detox build时必须处于项目根目录,不要进入android子目录执行。 - 替换配置中的占位符:将iOS配置里的
YOUR_APP替换为实际的项目名称,避免配置解析异常。 - 启用端口反向代理:如果项目依赖本地Metro服务,取消
reversePorts: [8081]的注释,确保测试设备能访问Metro。 - 清理构建缓存:执行以下命令清理Gradle缓存后重新构建:
cd android && ./gradlew clean && cd .. detox build --configuration android.emu.debug
内容的提问来源于stack exchange,提问作者Shirin
相关产品推荐
相关产品推荐


)。但单独执行命令