出现场景
使用Capacitor运行App时,出现以下提示。
adodeMacBook-Pro:ionic_app ado$ npx cap run android ✖ copy android - failed! [error] Could not find the web assets directory: ./build. Please create it and make sure it has an index.html file. You can change the path of this directory in capacitor.config.ts (webDir option). You may need to compile the web assets for your app (typically npm run build). More info: https://capacitorjs.com/docs/basics/workflow#sync-your-project ✔ Updating Android plugins in 26.08ms ✖ update android - failed! [error] Error: ENOENT: no such file or directory, open '/Users/ado/my/work/h5/ionic_app/android/app/src/main/assets/capacitor.plugins.json'
解决方案
这是因为还没有为app编译生成web资源文件,我们先使用ionic build
编译一次
adodeMacBook-Pro:ionic_app ado$ ionic build > react-scripts build Creating an optimized production build... Compiled successfully.
我们可以看到项目根目录出现了一个build文件夹,说明已经编译成功。
再次运行npx cap run android
或者npx cap run ios
即可成功运行。