You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建带依赖的React Native Android模块时构建失败问题咨询

问题

我正在解决Android Studio、Gradle与React Native环境下的构建报错问题。按照《Native Modules NPM Package Setup》文档创建支持iOS和Android的React Native模块,执行命令npx create-react-native-library@latest react-native-awesome-module后,CLI成功生成示例应用与react-native-awesome-module模块。

由于模块需要封装私有Maven仓库中的custom-dependency依赖,我在build.gradle (Module :react-native-awesome-module)中添加了对应仓库配置与依赖声明:

repositories {
  mavenCentral()
  google()
  maven { // <-- 此代码块
    credentials {
      username = "USERNAME"
      password = "PASSWORD"
    }
    setUrl("MAVEN_URL")
  }
}

dependencies {
  // For < 0.71, this will be from the local maven repo
  // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
  //noinspection GradleDynamicVersion
  implementation "com.facebook.react:react-native:+"
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
  implementation "it.mycompany.custom-dependency:1.0.0" // <-- 此行代码
}

但构建示例应用时出现报错:

Could not determine the dependencies of task ':app:processDebugResources'.
> Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'.
   > Could not find it.mycompany.custom-dependency:1.0.0.
     Searched in the following locations:
       - https://oss.sonatype.org/content/repositories/snapshots/PATH_TO_POM.pom
       - https://repo.maven.apache.org/maven2/PATH_TO_POM.pom
       - file:/PATH_TO_PROJECT/example/node_modules/jsc-android/dist/PATH_TO_POM.pom
       - https://dl.google.com/dl/android/maven2/PATH_TO_POM.pom
       - https://www.jitpack.io/PATH_TO_POM.pom
     Required by:
         project :app > project :react-native-awesome-module

目前只能通过在build.gradle (Module :app)中添加相同的仓库与依赖配置解决,想请教出现此问题的原因是什么?

原因分析

  • Gradle依赖解析规则限制:当react-native-awesome-module作为依赖被:app模块引入时,Gradle会在消费方(:app模块)的仓库列表中查找所有依赖,包括模块自身声明的间接依赖。模块build.gradle中配置的仓库仅在模块独立构建时生效,不会被消费方继承。
  • React Native模块集成机制特性:React Native通过自动链接或手动配置将你的模块作为子项目引入主应用的Gradle构建,但不会自动同步模块内的仓库配置到主应用构建脚本。主应用:app模块解析依赖时,只会使用自身build.gradle中声明的仓库,因此无法找到私有仓库中的custom-dependency。
  • 依赖传递的局限性:虽然implementation类型的依赖会传递给消费方,但仓库配置不属于依赖传递的范畴。仓库是构建级别的独立配置,消费方不会自动继承依赖模块的仓库设置。

内容的提问来源于stack exchange,提问作者Lorenzo B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 15:07:51