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

React Native Gradle无法解析本地Maven仓库问题求助

解决React Native Android包本地Maven仓库依赖解析问题

我正在构建一个Android端使用.aar文件的React Native包,该.aar仅内部使用,未上传至公共Maven仓库。我在my-library/android目录下创建了本地Maven仓库,但构建示例应用时出现无法解析该库的错误。

现有配置

my-library/android/build.gradle文件内容:

buildscript {
...
  repositories {
    google()
    mavenCentral()
    maven {
      url = uri("${rootProject.projectDir}/maven-repo")
    }
  }
}
...
repositories {
  mavenCentral()
  google()
  maven {
    url = uri("${rootProject.projectDir}/maven-repo")
  }
}

dependencies {
  implementation("com.mylibrary:localoperationsdk:1.0.0@aar")
}

...

报错信息

Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'.
   > Could not find com.mylibrary:localoperationsdk:1.0.0.
     Searched in the following locations:
       - https://oss.sonatype.org/content/repositories/snapshots/com/mylibrary/localoperationsdk/1.0.0/localoperationsdk-1.0.0.pom
       - https://repo.maven.apache.org/maven2/com/mylibrary/localoperationsdk/1.0.0/localoperationsdk-1.0.0.pom
       - example/node_modules/jsc-android/dist/com/mylibrary/localoperationsdk/1.0.0/localoperationsdk-1.0.0.pom
       - https://dl.google.com/dl/android/maven2/com/mylibrary/localoperationsdk/1.0.0/localoperationsdk-1.0.0.pom
       - https://www.jitpack.io/com/mylibrary/localoperationsdk/1.0.0/localoperationsdk-1.0.0.pom
     Required by:

解决方案

  • 扩展本地仓库配置范围
    你当前仅在my-library/android/build.gradle中配置了本地仓库,但React Native示例应用(example目录)的Gradle脚本未包含该地址。需要在示例应用的**项目级android/build.gradle和模块级android/app/build.gradle**中添加本地仓库配置:
    项目级android/build.gradle示例:

    allprojects {
      repositories {
        // 保留已有仓库配置
        maven {
          url = uri("../../my-library/android/maven-repo") // 根据实际相对路径调整
        }
      }
    }
    
  • 检查本地Maven仓库结构
    确保maven-repo目录符合Maven规范结构,缺一不可:

    maven-repo/
    └── com
        └── mylibrary
            └── localoperationsdk
                └── 1.0.0
                    ├── localoperationsdk-1.0.0.aar
                    └── localoperationsdk-1.0.0.pom
    

    若缺少.pom文件,可手动生成基础版本:

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.mylibrary</groupId>
      <artifactId>localoperationsdk</artifactId>
      <version>1.0.0</version>
      <packaging>aar</packaging>
    </project>
    
  • 调整依赖声明格式
    去掉@aar后缀,同时开启依赖传递(若aar有内部依赖):

    implementation("com.mylibrary:localoperationsdk:1.0.0") {
      transitive = true
    }
    
  • 清理缓存并重新构建
    执行命令清理Gradle缓存,避免旧配置干扰:

    ./gradlew clean build --refresh-dependencies
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 00:01:19