简体   繁体   中英

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. 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. So far gradle was syncing and building successfully. 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:

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. I've also tried changing my Kotlin version and my Gradle version but it still didn't work. For reference, here's my buildSrc build.gradle.kts

plugins {
    `kotlin-dsl`
}
repositories {
    jcenter()
}

and my Dependencies.kt inside the buildSrc module:

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:

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:

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. So, in the end, it does actually work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM