[英]CacheOpenException when trying to migrate Android Gradle project from Groovy to Kotlin
So I'm trying to migrate my build.gradle files in a simple toy app from Groovy to the new Kotlin dsl for Gradle.所以我试图将我的 build.gradle 文件在一个简单的玩具应用程序中从 Groovy 迁移到适用于 Gradle 的新 Kotlin dsl。 I have been able to successfully migrate my project build.gradle file and I've also created a buildSrc directory that has all the version numbers and such in a Kotlin files.我已经能够成功迁移我的项目 build.gradle 文件,并且我还创建了一个 buildSrc 目录,该目录包含 Kotlin 文件中的所有版本号等。 So far gradle was syncing and building successfully.到目前为止,gradle 已成功同步和构建。 But then when I tried to convert my app build.gradle to Kotlin (by changing the file extension and changing all the function calls and maps and such to follow the Kotlin syntax) I got an error in my gradle sync that says:但是,当我尝试将我的应用程序 build.gradle 转换为 Kotlin(通过更改文件扩展名并更改所有函数调用和映射等以遵循 Kotlin 语法)时,我的 gradle 同步中出现错误:
org.gradle.internal.exceptions.LocationAwareException: A problem occurred configuring project ':app'.
Caused by: org.gradle.api.ProjectConfigurationException: A problem occurred configuring project ':app'.
Caused by: org.gradle.cache.CacheOpenException: Could not open cache directory er2fj44y4paf4xwhwuc5jhit0 (C:\Users\Youssef Shoaib\.gradle\caches\5.6.4\gradle-kotlin-dsl-accessors\er2fj44y4paf4xwhwuc5jhit0).
Caused by: java.util.zip.ZipException: zip file is empty
I've tried rebuilding and cleaning the build but it still doesn't work.我试过重建和清理构建,但它仍然不起作用。 When I change my app build.gradle back to groovy the error disappears and everything works, but I really wanna use the Kotlin DSL for Gradle.当我将我的应用程序 build.gradle 改回 groovy 时,错误消失并且一切正常,但我真的想将 Kotlin DSL 用于 Gradle。 I've also tried changing my Kotlin version and my Gradle version but it still didn't work.我也尝试过更改我的 Kotlin 版本和 Gradle 版本,但它仍然不起作用。 For reference, here's my buildSrc build.gradle.kts作为参考,这是我的 buildSrc build.gradle.kts
plugins {
`kotlin-dsl`
}
repositories {
jcenter()
}
and my Dependencies.kt inside the buildSrc module:和我在 buildSrc 模块中的 Dependencies.kt:
object Versions {
const val version_core = "1.1.0"
const val version_coroutine = "1.1.1"
const val version_constraint_layout = "1.1.3"
const val version_gradle = "3.5.3"
const val version_kotlin = "1.3.61"
const val version_lifecycle_extensions = "2.1.0"
const val version_navigation = "2.1.0"
const val version_room = "2.2.2"
}
and my project build.gradle.kts file:和我的项目 build.gradle.kts 文件:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath("com.android.tools.build:gradle:${Versions.version_gradle}")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${Versions.version_kotlin}")
classpath("androidx.navigation:navigation-safe-args-gradle-plugin:${Versions.version_navigation}")
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
tasks.register<Delete>("clean") {
delete(rootProject.buildDir)
}
And my app build.gradle.kts file:我的应用程序 build.gradle.kts 文件:
plugins{
id("com.android.application")
kotlin("android")
kotlin("android.extensions")
kotlin("kapt")
id("androidx.navigation.safeargs.kotlin")
}
android {
compileOptions {
sourceCompatibility( JavaVersion.VERSION_1_8)
targetCompatibility( JavaVersion.VERSION_1_8)
}
kotlinOptions {
jvmTarget("1.8")
}
compileSdkVersion(29)
defaultConfig {
applicationId = "com.example.android.trackmysleepquality"
minSdkVersion(19)
targetSdkVersion(29)
versionCode = 1
versionName = "1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables.useSupportLibrary = true
javaCompileOptions {
annotationProcessorOptions {
arguments = mapOf("room.incremental" to "true")
}
}
}
buildTypes {
getByName("release") {
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
// Enables data binding.
dataBinding {
enabled = true
}
}
dependencies {
implementation(fileTree(mapOf("dir" to "libs"), "include:" to listOf("*.jar")))
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:${Versions.version_kotlin}")
// Support libraries
implementation("androidx.constraintlayout:constraintlayout:${Versions.version_constraint_layout}")
// Android KTX
implementation("androidx.core:core-ktx:${Versions.version_core}")
// Room and Lifecycle dependencies
implementation("androidx.room:room-runtime:${Versions.version_room}")
implementation("androidx.room:room-ktx:${Versions.version_room}")
kapt("androidx.room:room-compiler:${Versions.version_room}")
implementation("androidx.lifecycle:lifecycle-extensions:${Versions.version_lifecycle_extensions}")
implementation("com.github.hadilq.liveevent:liveevent:1.0.1")
// Coroutines
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:${Versions.version_coroutine}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:${Versions.version_coroutine}")
// Navigation
implementation("androidx.navigation:navigation-fragment-ktx:${Versions.version_navigation}")
implementation("androidx.navigation:navigation-ui-ktx:${Versions.version_navigation}")
// Testing
testImplementation("junit:junit:4.12")
androidTestImplementation("androidx.test.ext:junit:1.1.1")
androidTestImplementation("androidx.arch.core:core-testing:2.1.0")
androidTestImplementation("androidx.test.espresso:espresso-core:3.2.0")
androidTestImplementation("org.mockito:mockito-core:3.1.0")
}
I finally found a solution.我终于找到了解决方案。 I just deleted the whole .gradle directory inside my user folder, which, I think, invalidated all the caches that Android Studio might have made for the accessors for the Kotlin dsl.我刚刚删除了我的用户文件夹中的整个 .gradle 目录,我认为这使 Android Studio 可能为 Kotlin dsl 的访问器制作的所有缓存失效。 So, in the end, it does actually work所以,最后,它确实有效
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.