Android多模块配置Maven认证引入私有库Gradle报错解决
解决Android模块化改造中私有Maven仓库依赖解析失败问题
问题背景
我正在对Android项目进行模块化改造,原本在项目根build.gradle中配置的私有Maven仓库代码如下:
allprojects { repositories { google() jcenter() mavenCentral() maven { url "https://gitlab.com/api/xxx/-/packages/maven" credentials(HttpHeaderCredentials) { name = "Private-Token" value = gitlabToken } authentication { header(HttpHeaderAuthentication) } } } }
我将其替换为settings.gradle.kts中的如下代码:
val properties = java.util.Properties() val inputStream = java.io.FileInputStream("gradle.properties") properties.load(inputStream) val gitlabToken = properties.getProperty("gitlab_token") dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() jcenter() mavenCentral() maven { url = java.net.URI("https://gitlab.com/api/v4/xxxx/-/packages/maven") credentials(HttpHeaderCredentials::class) { name = "Private-Token" value = gitlabToken } authentication { create<HttpHeaderAuthentication>("header") } } } }
运行项目时出现如下错误:
FAILURE: Build completed with 8 failures. 1: Task failed with an exception. ----------- * What went wrong: Execution failed for task ':core:commons:generateDevDebugRFile'. > Could not resolve all files for configuration ':core:commons:devDebugCompileClasspath'. > Failed to transform mylib-1.0.0.aar (com.libs:mylib:1.0.0) to match attributes {artifactType=android-symbol-with-package-name, org.gradle.category=library, org.gradle.dependency.bundling=external, org.gradle.libraryelements=aar, org.gradle.status=release, org.gradle.usage=java-api}. > Could not find mylib-1.0.0.aar (com.libs:mylib:1.0.0). Searched in the following locations: https://gitlab.com/api/xxx//-/packages/maven/com/libs/mylib/1.0.0/mylib-1.0.0.aar * Try: > Run with --info or --debug option to get more log output. > Run with --scan to get full insights. * Exception is: org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':core:commons:generateDevDebugRFile'. ...
解决方案
从错误日志可见,Gradle请求的仓库URL出现重复斜杠(api/xxx//-/packages),且新旧配置的仓库URL存在不一致,需做以下修改:
修正私有仓库URL
错误日志中的请求URL存在拼写问题,对比原build.gradle的URL(https://gitlab.com/api/xxx/-/packages/maven),新settings.gradle.kts中写成了https://gitlab.com/api/v4/xxxx/-/packages/maven,需统一为正确地址,确保路径无多余字符和重复斜杠:url = java.net.URI("https://gitlab.com/api/v4/xxx/-/packages/maven")简化属性读取逻辑
KTS中可直接通过settings.properties读取配置,替代手动文件流操作:val gitlabToken: String by settings.properties统一认证配置写法
调整认证代码,和原Groovy配置逻辑保持一致,避免认证类型不匹配:credentials(HttpHeaderCredentials::class) { name = "Private-Token" value = gitlabToken } authentication { header(HttpHeaderAuthentication::class) }清理缓存并同步
修改完成后执行命令清理缓存,再重新同步项目:./gradlew clean build --refresh-dependencies
最终修正后的settings.gradle.kts片段
val gitlabToken: String by settings.properties dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() jcenter() mavenCentral() maven { url = java.net.URI("https://gitlab.com/api/v4/xxx/-/packages/maven") credentials(HttpHeaderCredentials::class) { name = "Private-Token" value = gitlabToken } authentication { header(HttpHeaderAuthentication::class) } } } }
内容的提问来源于stack exchange,提问作者user3884677
相关产品推荐
相关产品推荐

