将React Native 0.71应用转为Library输出AAR文件遇依赖问题
React Native 0.71转Android库输出AAR编译错误解决
问题现象
将现有React Native 0.71应用转为Android库(输出AAR)时,执行./gradlew assembleDebug命令出现以下错误:
* What went wrong: Could not determine the dependencies of task ':app:compileDebugAidl'. > Could not resolve all task dependencies for configuration ':app:debugCompileClasspath'. > Could not find com.facebook.react:react-android:. Required by: project :app
已完成操作
- 将
app/build.gradle中的apply plugin: "com.android.application"修改为apply plugin: "com.android.library" - 删除
defaultConfig块中的applicationId字段
解决方法
1. 明确指定react-android依赖版本
在app/build.gradle的dependencies块中,添加带具体版本号的react-android依赖,版本号需与项目使用的React Native 0.71对应版本一致(可查看项目package.json中的react-native版本):
dependencies { // 保留原有其他依赖 implementation "com.facebook.react:react-android:0.71.14" // 替换为你的RN实际版本 }
2. 确认仓库配置正确
确保项目能拉取到React Native的Android依赖:
- 若使用根目录
build.gradle,在allprojects.repositories中添加:
allprojects { repositories { google() mavenCentral() maven { url "$rootDir/../node_modules/react-native/android" } maven { url "$rootDir/../node_modules/jsc-android/dist" } // 其他仓库 } }
- 若使用
settings.gradle(RN 0.71默认结构),在dependencyResolutionManagement.repositories中添加:
dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() maven { url "../node_modules/react-native/android" } maven { url "../node_modules/jsc-android/dist" } } }
3. 清理并重新构建
执行以下命令清理旧构建文件并重新编译:
./gradlew clean ./gradlew assembleDebug
内容的提问来源于stack exchange,提问作者NITY
相关产品推荐
相关产品推荐

