基于 React Native 中文网教程 编译并运行 React Native 应用
在项目 (AwesomeProject) 根目录下运行命令 yarn rect-native run-android
或 yarn android
出现如下错误
D:\code\AwesomeProject> yarn react-native run-android PS D:\code\AwesomeProject> yarn rect-native run-android yarn run v1.22.11 error Command "rect-native" not found. Did you mean "react-native"? info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command. PS D:\code\AwesomeProject> yarn react-native run-android yarn run v1.22.11 $ D:\code\AwesomeProject\node_modules\.bin\react-native run-android info Running jetifier to migrate libraries to AndroidX. You can disable it using "--no-jetifier" flag. Jetifier found 870 file(s) to forward-jetify. Using 16 workers... info Starting JS server... * daemon not running; starting now at tcp:5037 * daemon started successfully info Launching emulator... info Successfully launched emulator. info Installing the app... Could not unzip C:\Users\w\.gradle\wrapper\dists\gradle-6.9-all\dooywd8nv05k16orzxge2b1bs\gradle-6.9-all.zip to C:\Users\w\.gradle\wrapper\dists\gradle-6.9-all\dooywd8nv05k16orzxge2b1bs. Reason: error in opening zip file Exception in thread "main" java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:228) at java.util.zip.ZipFile.<init>(ZipFile.java:157) at java.util.zip.ZipFile.<init>(ZipFile.java:171) at org.gradle.wrapper.Install.unzip(Install.java:239) at org.gradle.wrapper.Install.access$900(Install.java:27) at org.gradle.wrapper.Install$1.call(Install.java:81) at org.gradle.wrapper.Install$1.call(Install.java:48) at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:69) at org.gradle.wrapper.Install.createDist(Install.java:48) at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:107) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:63) error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081 Exception in thread "main" java.util.zip.ZipException: error in opening zip file at java.util.zip.ZipFile.open(Native Method) at java.util.zip.ZipFile.<init>(ZipFile.java:228) at java.util.zip.ZipFile.<init>(ZipFile.java:157) at java.util.zip.ZipFile.<init>(ZipFile.java:171) at org.gradle.wrapper.Install.unzip(Install.java:239) at org.gradle.wrapper.Install.access$900(Install.java:27) at org.gradle.wrapper.Install$1.call(Install.java:81) at org.gradle.wrapper.Install$1.call(Install.java:48) at org.gradle.wrapper.ExclusiveFileAccessManager.access(ExclusiveFileAccessManager.java:69) at org.gradle.wrapper.Install.createDist(Install.java:48) at org.gradle.wrapper.WrapperExecutor.execute(WrapperExecutor.java:107) at org.gradle.wrapper.GradleWrapperMain.main(GradleWrapperMain.java:63) at makeError (D:\code\AwesomeProject\node_modules\execa\index.js:174:9) at D:\code\AwesomeProject\node_modules\execa\index.js:278:16 at processTicksAndRejections (internal/process/task_queues.js:93:5) at async runOnAllDevices (D:\code\AwesomeProject\node_modules\@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:106:5) at async Command.handleAction (D:\code\AwesomeProject\node_modules\@react-native-community\cli\build\index.js:192:9) info Run CLI with --verbose flag for more details. error Command failed with exit code 1. info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
报错提示没有正确安装 Android 开发环境,以及文件打开错误
Could not unzip C:\Users\w\.gradle\wrapper\dists\gradle-6.9-all\dooywd8nv05k16orzxge2b1bs\gradle-6.9-all.zip to C:\Users\w\.gradle\wrapper\dists\gradle-6.9-all\dooywd8nv05k16orzxge2b1bs. Reason: error in opening zip file Exception in thread "main" java.util.zip.ZipException: error in opening zip file error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup. Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081 Exception in thread "main" java.util.zip.ZipException: error in opening zip file
解决方式
在文件资源管理器中找到 C:\Users\w\.gradle\wrapper\dists
目录,删除里面的 gradle-6.9-all.zip
文件, 重新运行 yarn android
命令重新安装 Android 相关环境就好了
版本信息
- 系统:Windows 11
- React Native: 0.66
- Java: 1.8.0_312
- Javac: 1.8.0_312