已迁移到 Kotlin .kts
,现在我遇到了一些构建问题。
以下是一些错误:
> Could not resolve all files for configuration ':app:stagingNewDesignDebugRuntimeClasspath'.
> Could not resolve com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5.
Required by:
project :app
> Could not resolve com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5.
> Could not get resource 'https://nexus.central.tech/repository/nexus-releases/com/pierfrancescosoffritti/androidyoutubeplayer/core/10.0.5/core-10.0.5.pom'.
> Could not HEAD 'https://nexus.central.tech/repository/nexus-releases/com/pierfrancescosoffritti/androidyoutubeplayer/core/10.0.5/core-10.0.5.pom'. Received status code 401 from server: Unauthorized
> Could not resolve com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5.
> Could not get resource 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/pierfrancescosoffritti/androidyoutubeplayer/core/10.0.5/core-10.0.5.pom'.
我不明白的是,依赖项被附加到我们的 Nexus 服务器。
Could not resolve com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5.
> Could not get resource 'https://nexus.central.tech/repository/nexus-releases/com/pierfrancescosoffritti/androidyoutubeplayer/core/10.0.5/core-10.0.5.pom
我们的 Nexus 存储库是https://nexus.central.tech/repository/nexus-releases
但是 /com/pierfrancescosoffritti/androidyoutubeplayer/core/10.0.5/core-10.0.5.pom
不断被附加到它。这就是为什么它找不到完整路径并且构建会失败。
androidyoutubeplayer
不是来 self 们的 Nexus,所以我不明白为什么它试图从那里获取它。
// build.gradle.kts
repositories {
google()
mavenCentral()
maven {
url = uri(properties["nexusRepositoryUrl"].toString())
credentials {
username = properties["nexusRepositoryUsername"].toString()
username = properties["nexusRepositoryPassword"].toString()
}
}
maven { url = uri("https://www.jitpack.io") }
maven { url = uri("https://s3.amazonaws.com/salesforcesos.com/android/maven/release") }
maven { url = uri("https://dl.bintray.com/objectbox/objectbox") }
maven { url = uri("https://sdk.uxcam.com/android/") }
}
更新——更正用户名/密码后
Could not determine the dependencies of task ':app:compileUatDebugKotlin'.
> Could not resolve all files for configuration ':app:stagingNewDesignDebugRuntimeClasspath'.
> Could not resolve com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5.
Required by:
project :app
> Skipped due to earlier error
> Could not resolve com.pierfrancescosoffritti.androidyoutubeplayer:core:10.0.5.
> Could not get resource 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/pierfrancescosoffritti/androidyoutubeplayer/core/10.0.5/core-10.0.5.pom'.
> Could not HEAD 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/pierfrancescosoffritti/androidyoutubeplayer/core/10.0.5/core-10.0.5.pom'. Received status code 403 from server: Forbidden
查看 androidyoutubeplayer 的 git 存储库后。它应该从 mavenCentral() 获取该依赖项。所以不确定为什么要查看上面的内容。
===更新2=== jcenter() 最初已被删除,但我已将其放回原处,因为我们有一些旧的库尚未找到 mavenCentral() 的方式。这就是为什么无法找到某些依赖项的原因。于是继续搜索其他仓库。
这是build.gradle.kts
repositories {
google()
jcenter()
mavenCentral()
maven {
url = uri(properties["nexusRepositoryUrl"].toString())
credentials {
username = properties["nexusRepositoryUsername"].toString()
password = properties["nexusRepositoryPassword"].toString()
/* TODO: Remove after debugging */
println("USERNAME: $username")
println("PASSWORD: $password")
println("URL: $url")
}
}
maven { url = uri("https://sdk.uxcam.com/android/") }
maven { url = uri("https://www.jitpack.io") }
maven { url = uri("https://s3.amazonaws.com/salesforcesos.com/android/maven/release") }
maven { url = uri("https://dl.bintray.com/objectbox/objectbox") }
}
这是build.gradle
(工作正常)
repositories {
google()
jcenter()
mavenCentral()
maven {
url nexusRepositoryUrl
credentials {
username nexusRepositoryUsername
password nexusRepositoryPassword
}
}
maven { url 'https://sdk.uxcam.com/android/'}
maven { url "https://www.jitpack.io" }
maven { url 'https://s3.amazonaws.com/salesforcesos.com/android/maven/release' }
maven { url "https://dl.bintray.com/objectbox/objectbox" }
}
现在的错误有所不同。这是我在构建时得到的结果:
.gradle/caches/transforms-2/files-2.1/8fc5090e854d3eadc13743a6b4942213/jetified-kotlin-stdlib-1.6.0.jar!/META-INF/kotlin-stdlib.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.2.
.gradle/caches/transforms-2/files-2.1/bdaa895e8cd4dd25cc3b271fb1f2fc8e/jetified-kotlin-stdlib-jdk7-1.6.0.jar!/META-INF/kotlin-stdlib-jdk7.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.2.
.gradle/caches/transforms-2/files-2.1/cebf6956c831e2c0ba58bc90c4d45ce7/jetified-kotlin-stdlib-common-1.6.0.jar!/META-INF/kotlin-stdlib-common.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.2.
.gradle/caches/transforms-2/files-2.1/e0c09723c14595670dad4b40acbe2360/jetified-core-11.0.1-api.jar!/META-INF/core_release.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.2.
.gradle/caches/transforms-2/files-2.1/ec9cde959f0fd7b7d9fe2427414ce447/jetified-kotlin-stdlib-jdk8-1.6.0.jar!/META-INF/kotlin-stdlib-jdk8.kotlin_module: Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.6.0, expected version is 1.4.2.
这是我的项目级别 build.gradle.kts
buildscript {
val kotlin_version = "1.4.32"
val navigationVersion = "2.3.5"
val objectboxVersion = "2.9.1"
val jacocoVersion = "0.8.5"
val sonarVersion = "3.0"
val newRelicVersion = "5.28.1"
repositories {
google()
mavenCentral()
}
dependencies {
classpath("com.android.tools.build:gradle:4.2.2")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:")
classpath("org.jetbrains.kotlin:kotlin-noarg:$kotlin_version")
classpath("org.jacoco:org.jacoco.core:$jacocoVersion")
classpath("org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:$sonarVersion")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:$navigationVersion")
classpath("com.google.gms:google-services:4.3.8")
classpath("com.google.firebase:perf-plugin:1.3.4")
classpath("com.google.firebase:firebase-appdistribution-gradle:2.0.1")
classpath("com.google.firebase:firebase-crashlytics-gradle:2.4.1")
classpath("io.objectbox:objectbox-gradle-plugin:$objectboxVersion")
classpath("com.newrelic.agent.android:agent-gradle-plugin:$newRelicVersion")
classpath("com.akaita.android:easylauncher:1.3.1")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
我不明白的是,迁移之前的build.gradle工作正常,并且所有版本的kotlin和依赖项都是相同的。因为我在另一个分支上有它。
运行以下命令时./gradlew assembleUat --info
Failed to get resource: GET. [HTTP HTTP/1.1 403 Forbidden: https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/google/firebase/firebase-messaging/maven-metadata.xml)]
Failed to get resource: GET. [HTTP HTTP/1.1 502 Bad Gateway: https://dl.bintray.com/objectbox/objectbox/com/google/firebase/firebase-messaging/maven-metadata.xml)]
Failed to get resource: GET. [HTTP HTTP/1.1 502 Bad Gateway: https://dl.bintray.com/objectbox/objectbox/com/google/firebase/firebase-messaging/maven-metadata.xml)]
Failed to get resource: GET. [HTTP HTTP/1.1 502 Bad Gateway: https://dl.bintray.com/objectbox/objectbox/com/google/firebase/firebase-messaging/maven-metadata.xml)]
Failed to get resource: GET. [HTTP HTTP/1.1 403 Forbidden: https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/google/android/gms/play-services-location/maven-metadata.xml)]
Failed to get resource: GET. [HTTP HTTP/1.1 403 Forbidden: https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/google/android/gms/play-services-ads-identifier/maven-metadata.xml)]
Failed to get resource: GET. [HTTP HTTP/1.1 403 Forbidden: https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/google/android/gms/play-services-base/maven-metadata.xml)]
Failed to get resource: GET. [HTTP HTTP/1.1 403 Forbidden: https://s3.amazonaws.com/salesforcesos.com/android/maven/release/com/akamai/android/aka-common/maven-metadata.xml)]
最佳答案
Gradle 无法在存储库中找到文件时出现的错误常常令人困惑。但重要的是它说
Received status code 401 from server: Unauthorized
.
Gradle 不知道什么工件存储在哪个存储库中,因此它会在所有存储库中搜索 .pom
定义 - 但由于用户名和密码不正确而失败。
我怀疑如果您添加一些调试日志语句,您会看到一些意想不到的东西。
// build.gradle.kts
credentials {
username = properties["nexusRepositoryUsername"].toString()
username = properties["nexusRepositoryPassword"].toString()
// ^^^ ^^^
println("repo user: $username")
println("repo pass: $password")
}
当然,不要将它们提交到您的存储库中!
改进
除了修复用户名/密码混淆之外,您还可以做更多事情来提供帮助。
使用repository content filtering告诉 Gradle 哪些工件存储在哪个存储库中。
找到需要过滤的依赖项组(我查看了 the Salesforce docs 来找到它们),然后在 Gradle 中配置过滤:
// build.gradle.kts repositories { maven { url = uri(properties["nexusRepositoryUrl"].toString()) content { // this repository *only* contains artifacts with the specified groups includeGroup("com.salesforce.service") includeGroup("com.salesforce.android") } } // also filter 'objectbox' dependencies maven("https://dl.bintray.com/objectbox/objectbox") { content { includeGroup("io.objectbox") } } }
现在 Gradle 将仅搜索该存储库中的 Salesforce 工件。 作为一个很好的副作用,这也将提高性能,因为 Gradle 不会费心在该存储库中搜索不相关的工件。
使用'named repository credentials'根据存储库名称自动设置用户名/密码。
See the answer here for a more thorough explanation of named repository credentials
// build.gradle.kts repositories { maven { url = uri(properties["nexusRepositoryUrl"].toString()) name = "nexusRepository" credentials(PasswordCredentials::class) } }
# $GRADLE_USER_HOME/gradle.properties nexusRepositoryUsername=my-username nexusRepositoryPassword=my-password
关于android-gradle-plugin - 依赖 url 不断附加到我们的 Nexus 存储库的末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72922746/