AbolfazlHaghpanah
AbolfazlHaghpanah

Reputation: 1

Gradle Sync Error: Please initialize at least one Kotlin target in 'app (:app)' when applying custom gradle plugin for kotlin multiplatform project

I'm trying to implement a custom gradle plugin for my app module in a kotlin multiplatform project, but i keep ran into this error when i apply my custom plugin and sync project.

Error says to initialize target in app module but i already initiated.

The Stacktrace:

> Configure project :app
mmd is Coming
com.android.application
e: Please initialize at least one Kotlin target in 'app (:app)'.
Read more https://kotl.in/set-up-targets

w: The following Kotlin source sets were configured but not added to any Kotlin compilation:
 * commonMain
 * commonTest
You can add a source set to a target's compilation by connecting it with the compilation's default source set using 'dependsOn'.
See https://kotl.in/connecting-source-sets

w: Missing 'androidTarget()' Kotlin target in multiplatform project 'app (:app)'.
The Android Gradle plugin was applied without creating a corresponding 'android()' Kotlin Target:

plugins {
id("com.android.application")
kotlin("multiplatform")
}

kotlin {
androidTarget() // \<-- please register this Android target
}

FAILURE: Build failed with an exception.

* What went wrong:
  com/android/build/api/dsl/ApplicationExtension

> com.android.build.api.dsl.ApplicationExtension

* Try:

> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.

* Exception is:
  java.lang.NoClassDefFoundError: com/android/build/api/dsl/ApplicationExtension
  at ApplicationGradlePlugin.apply(ApplicationGradlePlugin.kt:24)
  ...
  Caused by: java.lang.ClassNotFoundException: com.android.build.api.dsl.ApplicationExtension
  at org.gradle.internal.classloader.VisitableURLClassLoader$InstrumentingVisitableURLClassLoader.findClass(VisitableURLClassLoader.java:187)
  ... 215 more

CONFIGURE FAILED in 6s

My Custom Plugin in BuildSrc:

class ApplicationGradlePlugin : Plugin<Project> {
    override fun apply(target: Project) {
        with(target) {
            val libs = extensions.getByType<VersionCatalogsExtension>().named("libs")

            println("mmd is Coming")
            println(libs.findPlugin("androidApplication").get().get().pluginId)
            pluginManager.apply(libs.findPlugin("kotlinMultiplatform").get().get().pluginId)

            pluginManager.apply(libs.findPlugin("androidApplication").get().get().pluginId)

            extensions.getByType(ApplicationExtension::class.java)

            println("mmd done his job..")
        }
    }
}

build.gradle.kts file in app module:

import org.jetbrains.compose.desktop.application.dsl.TargetFormat

plugins {
    alias(libs.plugins.kotlinMultiplatform)
    alias(libs.plugins.composeMultiplatform)
    alias(libs.plugins.composeCompiler)
    alias(libs.plugins.kotlin.serialization)
    alias(libs.plugins.androidApplication)
    id("com.sample.myproject.application-plugin")// --> this is my custom plugin
}

kotlin {
    android {
        compilations.all {
            kotlinOptions.jvmTarget = "21"
        }
    }

    jvm("desktop")

    sourceSets {
        val desktopMain by getting

        androidMain.dependencies {
            // android dependencies
        }
        commonMain.dependencies {
            // common dependencies
        }
        desktopMain.dependencies {
           // desktop dependencies
        }
    }
}

android {
    namespace = "com.sample.myproject"
    compileSdk = libs.versions.android.compileSdk.get().toInt()

    defaultConfig {
        applicationId = "com.sample.myproject"
    // other configurations
}

I've emptied my plugin and added configurations step by step and synced every time, as i add this:

extensions.getByType(ApplicationExtension::class.java)

It gives me the error.

I've initiated targets in my plugins like this:

extensions.getByType(KotlinMultiplatformExtension::class).apply {
     androidTarget()
     jvm("desktop")
}

But it didn't make any difference.

Upvotes: 0

Views: 72

Answers (1)

Try to update Gradle to the 8.0 version (in gradle.properties) and try to add this dependency to your dependencies block. It has helped to me.

classpath("com.android.tools.build:gradle:8.1.4") 

Upvotes: 0

Related Questions