最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Gradle Groovy to Kotlin Compose compiler migration error - Stack Overflow

programmeradmin3浏览0评论

I am trying to migrate my Android project to use the new Compose compiler and switched from Groovy syntax to Kotlin. Gradle sync now fails with this error:

Error resolving plugin [id: 'com.android.application', version: '8.8.1', apply: false]
The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked.

I already tried different combinations of using alias, getId and apply false, but none of them helped.

My build.gradle.kts - module:

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    alias(libs.pluginsposepiler) apply false
}

android {
    namespace = "com.kaufmannmarek.dochazsys"
    compileSdk = 35

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_19
        targetCompatibility = JavaVersion.VERSION_19
    }

    kotlinOptions {
        jvmTarget = "19"
    }

    defaultConfig {
        applicationId = "com.kaufmannmarek.dochazsys"
        minSdkVersion = 23
        targetSdkVersion = 35
        versionCode = 23
        versionName = "2502031045"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

composeCompiler {
    enableStrongSkippingMode = true
}

dependencies {
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.constraintlayout)
    implementation(libs.androidx.legacy.support.v4)
    implementation(libs.androidx.core.ktx)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.runner)
    androidTestImplementation(libs.androidx.espresso.core)
    implementation(libs.okhttp)
    implementation(libs.okhttp.urlconnection)
    implementation(libs.jasypt)
    implementation(libs.kotlin.stdlib)
    implementation(libs.androidx.navigation.fragment.ktx)
    implementation(libs.androidx.ui)
    implementation(libs.androidx.runtime)
    implementation(libs.kotlinposepiler.plugin.embeddable)
    implementation(libs.kotlinposepiler.plugin)
}

My libs.version.toml:

[versions]
appcompat = "1.7.0"
coreKtx = "1.15.0"
espressoCore = "3.6.1"
agp = "8.8.1"
gradle = "8.8.1"
jasypt = "1.9.3"
junit = "4.13.2"
kotlinComposeCompilerPlugin = "2.0.21"
kotlinGradlePlugin = "2.0.21"
kotlinStdlib = "2.0.21"
legacySupportV4 = "1.0.0"
material = "1.12.0"
constraintlayout = "2.2.0"
navigationFragmentKtx = "2.8.7"
okhttp = "2.7.5"
runner = "1.6.2"
runtime = "1.7.8"
ui = "1.7.8"
kotlin = "2.0.21"

[libraries]
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" }
androidx-legacy-support-v4 = { module = "androidx.legacy:legacy-support-v4", version.ref = "legacySupportV4" }
androidx-navigation-fragment-ktx = { module = "androidx.navigation:navigation-fragment-ktx", version.ref = "navigationFragmentKtx" }
androidx-runner = { module = "androidx.test:runner", version.ref = "runner" }
androidx-runtime = { module = "androidxpose.runtime:runtime", version.ref = "runtime" }
androidx-ui = { module = "androidxpose.ui:ui", version.ref = "ui" }
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
jasypt = { module = ".jasypt:jasypt", version.ref = "jasypt" }
junit = { module = "junit:junit", version.ref = "junit" }
kotlin-compose-compiler-plugin = { module = ".jetbrains.kotlin:kotlin-compose-compiler-plugin", version.ref = "kotlinComposeCompilerPlugin" }
kotlin-compose-compiler-plugin-embeddable = { module = ".jetbrains.kotlin:kotlin-compose-compiler-plugin-embeddable" }
kotlin-gradle-plugin = { module = ".jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlinGradlePlugin" }
kotlin-stdlib = { module = ".jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlinStdlib" }
material = { module = "com.google.android.material:material", version.ref = "material" }
okhttp = { module = "com.squareup.okhttp:okhttp", version.ref = "okhttp" }
okhttp-urlconnection = { module = "com.squareup.okhttp:okhttp-urlconnection", version.ref = "okhttp" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = ".jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler = { id = ".jetbrains.kotlin.pluginpose", version.ref = "kotlin" }

My build.gradle.kts - project:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        mavenCentral()
        
    }
    dependencies {
        classpath(libs.gradle)
        classpath(libs.kotlin.gradle.plugin)
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    layout.buildDirectory.set(layout.projectDirectory.dir("out"))
}

I am trying to migrate my Android project to use the new Compose compiler and switched from Groovy syntax to Kotlin. Gradle sync now fails with this error:

Error resolving plugin [id: 'com.android.application', version: '8.8.1', apply: false]
The request for this plugin could not be satisfied because the plugin is already on the classpath with an unknown version, so compatibility cannot be checked.

I already tried different combinations of using alias, getId and apply false, but none of them helped.

My build.gradle.kts - module:

plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    alias(libs.pluginsposepiler) apply false
}

android {
    namespace = "com.kaufmannmarek.dochazsys"
    compileSdk = 35

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_19
        targetCompatibility = JavaVersion.VERSION_19
    }

    kotlinOptions {
        jvmTarget = "19"
    }

    defaultConfig {
        applicationId = "com.kaufmannmarek.dochazsys"
        minSdkVersion = 23
        targetSdkVersion = 35
        versionCode = 23
        versionName = "2502031045"
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

composeCompiler {
    enableStrongSkippingMode = true
}

dependencies {
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation(libs.androidx.appcompat)
    implementation(libs.material)
    implementation(libs.androidx.constraintlayout)
    implementation(libs.androidx.legacy.support.v4)
    implementation(libs.androidx.core.ktx)
    testImplementation(libs.junit)
    androidTestImplementation(libs.androidx.runner)
    androidTestImplementation(libs.androidx.espresso.core)
    implementation(libs.okhttp)
    implementation(libs.okhttp.urlconnection)
    implementation(libs.jasypt)
    implementation(libs.kotlin.stdlib)
    implementation(libs.androidx.navigation.fragment.ktx)
    implementation(libs.androidx.ui)
    implementation(libs.androidx.runtime)
    implementation(libs.kotlinposepiler.plugin.embeddable)
    implementation(libs.kotlinposepiler.plugin)
}

My libs.version.toml:

[versions]
appcompat = "1.7.0"
coreKtx = "1.15.0"
espressoCore = "3.6.1"
agp = "8.8.1"
gradle = "8.8.1"
jasypt = "1.9.3"
junit = "4.13.2"
kotlinComposeCompilerPlugin = "2.0.21"
kotlinGradlePlugin = "2.0.21"
kotlinStdlib = "2.0.21"
legacySupportV4 = "1.0.0"
material = "1.12.0"
constraintlayout = "2.2.0"
navigationFragmentKtx = "2.8.7"
okhttp = "2.7.5"
runner = "1.6.2"
runtime = "1.7.8"
ui = "1.7.8"
kotlin = "2.0.21"

[libraries]
androidx-appcompat = { module = "androidx.appcompat:appcompat", version.ref = "appcompat" }
androidx-constraintlayout = { module = "androidx.constraintlayout:constraintlayout", version.ref = "constraintlayout" }
androidx-core-ktx = { module = "androidx.core:core-ktx", version.ref = "coreKtx" }
androidx-espresso-core = { module = "androidx.test.espresso:espresso-core", version.ref = "espressoCore" }
androidx-legacy-support-v4 = { module = "androidx.legacy:legacy-support-v4", version.ref = "legacySupportV4" }
androidx-navigation-fragment-ktx = { module = "androidx.navigation:navigation-fragment-ktx", version.ref = "navigationFragmentKtx" }
androidx-runner = { module = "androidx.test:runner", version.ref = "runner" }
androidx-runtime = { module = "androidxpose.runtime:runtime", version.ref = "runtime" }
androidx-ui = { module = "androidxpose.ui:ui", version.ref = "ui" }
gradle = { module = "com.android.tools.build:gradle", version.ref = "gradle" }
jasypt = { module = ".jasypt:jasypt", version.ref = "jasypt" }
junit = { module = "junit:junit", version.ref = "junit" }
kotlin-compose-compiler-plugin = { module = ".jetbrains.kotlin:kotlin-compose-compiler-plugin", version.ref = "kotlinComposeCompilerPlugin" }
kotlin-compose-compiler-plugin-embeddable = { module = ".jetbrains.kotlin:kotlin-compose-compiler-plugin-embeddable" }
kotlin-gradle-plugin = { module = ".jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlinGradlePlugin" }
kotlin-stdlib = { module = ".jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlinStdlib" }
material = { module = "com.google.android.material:material", version.ref = "material" }
okhttp = { module = "com.squareup.okhttp:okhttp", version.ref = "okhttp" }
okhttp-urlconnection = { module = "com.squareup.okhttp:okhttp-urlconnection", version.ref = "okhttp" }

[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
jetbrains-kotlin-android = { id = ".jetbrains.kotlin.android", version.ref = "kotlin" }
compose-compiler = { id = ".jetbrains.kotlin.pluginpose", version.ref = "kotlin" }

My build.gradle.kts - project:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        mavenCentral()
        
    }
    dependencies {
        classpath(libs.gradle)
        classpath(libs.kotlin.gradle.plugin)
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

tasks.register("clean", Delete::class) {
    layout.buildDirectory.set(layout.projectDirectory.dir("out"))
}
Share Improve this question edited Feb 17 at 11:11 tyg 15.6k4 gold badges35 silver badges48 bronze badges asked Feb 17 at 10:36 Alltair4747Alltair4747 875 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 0

Try removing AGP from buildscript block since you are using the plugins block in your build.gradle.kts you do not need to define AGP in buildscript.dependencies

dependencies {
         //remove classpath(libs.gradle) 
        classpath(libs.gradle)
        classpath(libs.kotlin.gradle.plugin)
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }

Assuming what you have labelled as your "project" build.gradle.kts is your root build.gradle.kts and "module" is your single subproject, then your setup looks a bit unusual:

  1. Using apply false on plugins is a device that can be useful in the root project when a plugin is applied in multiple subprojects. Here this is not an issue and you are doing this in your subproject build.gradle.kts. I suggest removing the apply false parts (or you will not have the plugins for use!).

  2. In the root build.gradle.kts you include the line

    classpath(libs.kotlin.gradle.plugin)

    This, together with the application of the Kotlin Android plugin in the subproject, causes the clash. Adding it here is not necessary as the same artifact is available to the build via the plugin. Therefore I suggest removing this line.

You need to use the plugins in the root gradle file, without applying them. Only actually apply them in the modules where they are needed (you apparently only have one module, so they are all applied there).

You should also move the repository configuration to the settings.gradle.kts. You didn't provide what you have right now, but it should look something like this:

pluginManagement {
    repositories {
        google {
            content {
                includeGroupByRegex("com\\.android.*")
                includeGroupByRegex("com\\.google.*")
                includeGroupByRegex("androidx.*")
            }
        }
        mavenCentral()
        gradlePluginPortal()
    }
}
dependencyResolutionManagement {
    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "your project name"
include(":app")

Then your root level gradle file simply contains this:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    alias(libs.plugins.android.application) apply false
    alias(libs.plugins.jetbrains.kotlin.android) apply false
    alias(libs.pluginsposepiler) apply false
}

I'm not sure what the purpose is of the clean task you register, but if you need it you can keep it here.

Then your module level gradle file just uses the plugins like this:

plugins {
    alias(libs.plugins.android.application)
    alias(libs.plugins.jetbrains.kotlin.android)
    alias(libs.pluginsposepiler)
}

// rest of the gradle file

There are some other issues in this gradle file, the most prominent being:

  • You need to replace minSdkVersion with minSdk and targetSdkVersion with targetSdk
  • You need to enable Compose by adding buildFeatures { compose = true }
  • The ProGuard configuration is missing, you probably want to enable that for your release build.
  • You never actually declare any Compose dependencies (except the compiler dependencies that can all be removed from the version catalog's [libraries] section). You will want to use at least these:
    • androidxpose:compose-bom
    • androidxpose.ui:ui
    • androidxpose.material3:material3

A good starting point would be to just create a new project and look at how the gradle files look there. Then adapt the gradle files of your project to work accordingly.

发布评论

评论列表(0)

  1. 暂无评论