正常通过yarn安装依赖后,执行如下命令:
npx react-native run-android
构建过程中报错,报错信息如下:
Failed to install the app. Command failed with exit code 1: ./gradlew app:installDebug -PreactNativeDevServerPort=8081 FAILURE: Build failed with an exception. * What went wrong: Could not determine the dependencies of task ':app:compileDebugJavaWithJavac'.
通过如下方法尝试修复此bug:
1. rm -rf node_modules // 删除依赖包
2. cd android
3. rm -rf .gradle // 删除项目本地 Gradle 配置缓存
4. rm -rf build/ // 删除构建输出目录
5. cd .. // 回到项目根目录
6. yarn install // 重新安装依赖
7. cd android // 进入到项目中的android文件夹中
8. ./gradlew clean // 清理构建产物
9. cd .. // 回到项目根目录
10. npx react-native run-android
如果进行到第八步报错,错误信息如下,请重新执行命令行:./gradlew --refresh-dependencies
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:externalNativeBuildCleanDebug'.
如果到这里,问题解决了,那真是太好了,但我还是接着遇到了bug。
在第十步重新构建android项目时依旧报错,错误信息如下:
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:checkDebugAarMetadata'.
> Could not resolve all files for configuration ':app:debugRuntimeClasspath'.
> Failed to transform react-android-0.76.1-debug.aar (com.facebook.react:react-android:0.76.1) to match attributes {artifactType=android-aar-metadata, com.android.build.api.attributes.BuildTypeAttr=debug, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-runtime}.
> Execution failed for JetifyTransform: /Users/sunways/.gradle/caches/modules-2/files-2.1/com.facebook.react/react-android/0.76.1/100f1d6599f807cbf57a4c51764b302eb2bd8b71/react-android-0.76.1-debug.aar.
最后重新尝试如下命令成功解决问题:
1. rm -rf node_modules
2. yarn install
3. cd android
4. ./gradlew --refresh-dependencies
5. cd ..
6. npx react-native run-android --verbose // 查看详细的输出