Gradle未检索自定义Maven仓库:React Native依赖配置问题排查
我在维护一个名为react-native-expofp的React Native库,尝试集成CrowdConnected模块。按照官方文档要求,已经在dependencies中添加了implementation 'com.expofp:crowdconnected:4.2.12',并在repositories里配置了maven { url "https://maven2.crowdconnected.net/" }。但构建时出现依赖解析失败的错误:
* What went wrong: Could not determine the dependencies of task ':app:processDebugResources'. > Could not resolve all task dependencies for configuration ':app:debugRuntimeClasspath'. > Could not find net.crowdconnected.android.core:android-core:1.3.6. Searched in the following locations: - https://oss.sonatype.org/content/repositories/snapshots/net/crowdconnected/android/core/android-core/1.3.6/android-core-1.3.6.pom - https://repo.maven.apache.org/maven2/net/crowdconnected/android/core/android-core/1.3.6/android-core-1.3.6.pom - file:/Users/arystankaliakparov/code/amego/mobile/node_modules/jsc-android/dist/net/crowdconnected/android/core/android-core/1.3.6/android-core-1.3.6.pom - https://dl.google.com/dl/android/maven2/net/crowdconnected/android/core/android-core/1.3.6/android-core-1.3.6.pom - https://www.jitpack.io/net/crowdconnected/android/core/android-core/1.3.6/android-core-1.3.6.pom Required by: project :app > project :react-native-expofp > com.expofp:crowdconnected:4.2.12 > Could not find net.crowdconnected.android.ips:android-ips:1.3.6. Searched in the following locations: - https://oss.sonatype.org/content/repositories/snapshots/net/crowdconnected/android/ips/android-ips/1.3.6/android-ips-1.3.6.pom - https://repo.maven.apache.org/maven2/net/crowdconnected/android/ips/android-ips/1.3.6/android-ips-1.3.6.pom - file:/Users/arystankaliakparov/code/amego/mobile/node_modules/jsc-android/dist/net/crowdconnected/android/ips/android-ips/1.3.6/android-ips-1.3.6.pom - https://dl.google.com/dl/android/maven2/net/crowdconnected/android/ips/android-ips/1.3.6/android-ips-1.3.6.pom - https://www.jitpack.io/net/crowdconnected/android/ips/android-ips/1.3.6/android-ips-1.3.6.pom Required by: project :app > project :react-native-expofp > com.expofp:crowdconnected:4.2.12 > Could not find net.crowdconnected.android.geo:android-geo:1.3.6. Searched in the following locations: - https://oss.sonatype.org/content/repositories/snapshots/net/crowdconnected/android/geo/android-geo/1.3.6/android-geo-1.3.6.pom - https://repo.maven.apache.org/maven2/net/crowdconnected/android/geo/android-geo/1.3.6/android-geo-1.3.6.pom - file:/Users/arystankaliakparov/code/amego/mobile/node_modules/jsc-android/dist/net/crowdconnected/android/geo/android-geo/1.3.6/android-geo-1.3.6.pom - https://dl.google.com/dl/android/maven2/net/crowdconnected/android/geo/android-geo/1.3.6/android-geo-1.3.6.pom - https://www.jitpack.io/net/crowdconnected/android/geo/android-geo/1.3.6/android-geo-1.3.6.pom Required by: project :app > project :react-native-expofp > com.expofp:crowdconnected:4.2.12
从日志能看出,Gradle完全没去我配置的https://maven2.crowdconnected.net/仓库查找依赖,这很反常。
附上我的库的build.gradle配置:
buildscript { // Buildscript is evaluated before everything else so we can't use getExtOrDefault def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["Expofp_kotlinVersion"] repositories { google() mavenCentral() } dependencies { classpath "com.android.tools.build:gradle:7.2.1" // noinspection DifferentKotlinGradleVersion classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" } } def isNewArchitectureEnabled() { return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true" } apply plugin: "com.android.library" apply plugin: "kotlin-android" if (isNewArchitectureEnabled()) { apply plugin: "com.facebook.react" } def getExtOrDefault(name) { return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["Expofp_" + name] } def getExtOrIntegerDefault(name) { return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["Expofp_" + name]).toInteger() } def supportsNamespace() { def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.') def major = parsed[0].toInteger() def minor = parsed[1].toInteger() // Namespace support was added in 7.3.0 return (major == 7 && minor >= 3) || major >= 8 } android { if (supportsNamespace()) { namespace "com.expofp" sourceSets { main { manifest.srcFile "src/main/AndroidManifestNew.xml" } } } compileSdkVersion getExtOrIntegerDefault("compileSdkVersion") defaultConfig { minSdkVersion getExtOrIntegerDefault("minSdkVersion") targetSdkVersion getExtOrIntegerDefault("targetSdkVersion") } buildTypes { release { minifyEnabled false } } lintOptions { disable "GradleCompatible" } compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } repositories { maven { url "https://maven2.crowdconnected.net" } google() mavenCentral() } allprojects { repositories { maven { url "https://maven2.crowdconnected.net" } } } def kotlin_version = getExtOrDefault("kotlinVersion") 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 'com.expofp:common:4.2.12' implementation 'com.expofp:fplan:4.2.12' implementation 'com.expofp:crowdconnected:4.2.12' }
1. 在主项目根目录的build.gradle中添加仓库配置
React Native项目中,模块库的仓库配置有时不会被自动继承到主app的依赖解析流程中。需要在主项目根目录的build.gradle的allprojects.repositories块里添加CrowdConnected的maven仓库:
allprojects { repositories { // 其他已有仓库... maven { url "https://maven2.crowdconnected.net/" } } }
2. 修正库中仓库地址的拼写
检查库的build.gradle中仓库地址是否完整,建议加上末尾的斜杠,和官方文档保持一致:
maven { url "https://maven2.crowdconnected.net/" }
3. 清理Gradle缓存并重新构建
缓存可能导致依赖解析异常,执行以下命令清理缓存并刷新依赖:
./gradlew clean build --refresh-dependencies
或者手动删除项目根目录下的.gradle/caches文件夹,再重新构建。
4. 确认依赖传递的有效性
com.expofp:crowdconnected:4.2.12确实依赖net.crowdconnected.android.core:android-core:1.3.6等包,这些包仅存在于CrowdConnected的私有maven仓库中,确保仓库配置正确后,Gradle就能找到这些依赖。
内容的提问来源于stack exchange,提问作者Арыстан Калиакпаров

