android - Errors in build.gradle files in new kotlin multiplatform project - Stack Overflow

admin2025-05-02  1

I was having issues with my build.gradle file in a kotlin multiplatform project, and as part of debugging I created a new kmp project and the errors were still there. I can still build and run the project, but the errors still show.

The error for the 'kotlin' tag is the following:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:

public fun DependencyHandler. kotlin(module: String, version: String? = ...): Any defined in org. gradle. kotlin. dsl

public fun PluginDependenciesSpec. kotlin(module: String): PluginDependencySpec defined in org. gradle. kotlin. dsl

Build.gradle (shared)

import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
}

kotlin {
    androidTarget {
        compilations.all {
            compileTaskProvider.configure {
                compilerOptions {
                    jvmTarget.set(JvmTarget.JVM_1_8)
                }
            }
        }
    }
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true
        }
    }

    sourceSets {
        commonMain.dependencies {
            //put your multiplatform dependencies here
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

android {
    namespace = "com.example.textx"
    compileSdk = 34
    defaultConfig {
        minSdk = 24
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

Build.gradle (androidApp)

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.kotlinAndroid)
    alias(libs.pluginsposepiler)
}

android {
    namespace = "com.example.textx.android"
    compileSdk = 34
    defaultConfig {
        applicationId = "com.example.textx.android"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
    }
    buildFeatures {
        compose = true
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation(projects.shared)
    implementation(libspose.ui)
    implementation(libspose.ui.tooling.preview)
    implementation(libspose.material3)
    implementation(libs.androidx.activitypose)
    debugImplementation(libspose.ui.tooling)
}

libs.versions

[versions]
agp = "8.7.3"
kotlin = "2.0.0"
compose = "1.5.4"
compose-material3 = "1.1.2"
androidx-activityCompose = "1.8.0"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
compose-ui = { module = "androidxpose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidxpose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidxpose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidxpose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidxpose.material3:material3", version.ref = "compose-material3" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinCocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.pluginpose", version.ref = "kotlin" }

Any help would be greatly appreciated

I was having issues with my build.gradle file in a kotlin multiplatform project, and as part of debugging I created a new kmp project and the errors were still there. I can still build and run the project, but the errors still show.

The error for the 'kotlin' tag is the following:

Unresolved reference. None of the following candidates is applicable because of receiver type mismatch:

public fun DependencyHandler. kotlin(module: String, version: String? = ...): Any defined in org. gradle. kotlin. dsl

public fun PluginDependenciesSpec. kotlin(module: String): PluginDependencySpec defined in org. gradle. kotlin. dsl

Build.gradle (shared)

import org.jetbrains.kotlin.gradle.dsl.JvmTarget

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.androidLibrary)
}

kotlin {
    androidTarget {
        compilations.all {
            compileTaskProvider.configure {
                compilerOptions {
                    jvmTarget.set(JvmTarget.JVM_1_8)
                }
            }
        }
    }
    
    listOf(
        iosX64(),
        iosArm64(),
        iosSimulatorArm64()
    ).forEach {
        it.binaries.framework {
            baseName = "shared"
            isStatic = true
        }
    }

    sourceSets {
        commonMain.dependencies {
            //put your multiplatform dependencies here
        }
        commonTest.dependencies {
            implementation(libs.kotlin.test)
        }
    }
}

android {
    namespace = "com.example.textx"
    compileSdk = 34
    defaultConfig {
        minSdk = 24
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
}

Build.gradle (androidApp)

plugins {
    alias(libs.plugins.androidApplication)
    alias(libs.plugins.kotlinAndroid)
    alias(libs.plugins.compose.compiler)
}

android {
    namespace = "com.example.textx.android"
    compileSdk = 34
    defaultConfig {
        applicationId = "com.example.textx.android"
        minSdk = 24
        targetSdk = 34
        versionCode = 1
        versionName = "1.0"
    }
    buildFeatures {
        compose = true
    }
    packaging {
        resources {
            excludes += "/META-INF/{AL2.0,LGPL2.1}"
        }
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    implementation(projects.shared)
    implementation(libs.compose.ui)
    implementation(libs.compose.ui.tooling.preview)
    implementation(libs.compose.material3)
    implementation(libs.androidx.activity.compose)
    debugImplementation(libs.compose.ui.tooling)
}

libs.versions

[versions]
agp = "8.7.3"
kotlin = "2.0.0"
compose = "1.5.4"
compose-material3 = "1.1.2"
androidx-activityCompose = "1.8.0"

[libraries]
kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" }
androidx-activity-compose = { module = "androidx.activity:activity-compose", version.ref = "androidx-activityCompose" }
compose-ui = { module = "androidx.compose.ui:ui", version.ref = "compose" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling", version.ref = "compose" }
compose-ui-tooling-preview = { module = "androidx.compose.ui:ui-tooling-preview", version.ref = "compose" }
compose-foundation = { module = "androidx.compose.foundation:foundation", version.ref = "compose" }
compose-material3 = { module = "androidx.compose.material3:material3", version.ref = "compose-material3" }

[plugins]
androidApplication = { id = "com.android.application", version.ref = "agp" }
androidLibrary = { id = "com.android.library", version.ref = "agp" }
kotlinAndroid = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
kotlinMultiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
kotlinCocoapods = { id = "org.jetbrains.kotlin.native.cocoapods", version.ref = "kotlin" }
compose-compiler = { id = "org.jetbrains.kotlin.plugin.compose", version.ref = "kotlin" }

Any help would be greatly appreciated

Share Improve this question asked Jan 2 at 12:31 olistocks98olistocks98 5932 gold badges5 silver badges10 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

just experienced the same issue. There was a patch for Android Studio-2024.2.1.12 that i downloaded just now and it solved it for me. But for some reason the Gradle was really slow with updating after installation. But at least it worked

转载请注明原文地址:http://www.anycun.com/QandA/1746120340a91949.html