无法获取类型为org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler的对象的未知属性'okhttpVersion'。

3

在我的新的Kotlin项目中,为了实现Retrofit API调用,以下依赖项被添加到build.gradle文件中。

    implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
    implementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
    implementation ("com.squareup.retrofit2:retrofit:$retrofitVersion"){
        exclude module: 'okhttp'
    }
    implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"

同步后,会显示下面提到的错误。
Could not get unknown property 'okhttpVersion' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

Studio指向错误行是,

implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"

请提供任何与IT技术相关的解决方案。

okhttpVersion是如何定义/设置的? - undefined
@Nik,请不要将代码标签更改为引用以用于错误日志。 - undefined
2个回答

7

定义版本值的方式如下:

def okhttpVersion = "3.10.0"
def retrofitVersion = "2.4.0"
implementation "com.squareup.okhttp3:okhttp:$okhttpVersion"
implementation "com.squareup.okhttp3:logging-interceptor:$okhttpVersion"
implementation ("com.squareup.retrofit2:retrofit:$retrofitVersion"){
    exclude module: 'okhttp'
}
implementation "com.squareup.retrofit2:converter-gson:$retrofitVersion"

或者尝试添加硬编码版本的依赖关系

implementation "com.squareup.okhttp3:okhttp:3.10.0"
implementation "com.squareup.okhttp3:logging-interceptor:3.10.0"
implementation ("com.squareup.retrofit2:retrofit:2.4.0"){
    exclude module: 'okhttp'
}
implementation "com.squareup.retrofit2:converter-gson:2.4.0"

不要忘记清理并重新构建...



哦,谢谢Omi,我错过了版本号,这真的是我的错误。 - undefined
如果这个帮助解决了你的问题,请点赞并将其标记为接受答案,就像这个链接中的示例一样:https://meta.stackexchange.com/a/5235/376420 - undefined

1

如果有人感兴趣,这里提供另一种解决方案。

如果您能够确定版本号,可以在项目 build.gradle 文件中定义 okhttpVersion

我的版本是 room_version 而不是 version_room。这也很容易修复。

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接