https://kotlinlang.org logo
#gradle
Title
# gradle
c

CLOVIS

03/16/2023, 8:07 PM
I'm getting when applying the Kotlin Multiplatform plugin as well as the Android library plugin…
Copy code
Could not create an instance of type org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget.
> Could not generate a decorated class for type KotlinAndroidTarget.
   > com/android/build/gradle/api/BaseVariant
The error message doesn't help me at all, where should I look for to fix this?
m

mbonnin

03/16/2023, 8:14 PM
There should be more to it like causes and so on if you run with --stacktrace
Usually it's a NoSuchMethod exception because some wrong dependency was put in your classloader
c

CLOVIS

03/16/2023, 8:16 PM
Copy code
org.gradle.api.reflect.ObjectInstantiationException: Could not create an instance of type org.jetbrains.kotlin.gradle.plugin.mpp.KotlinAndroidTarget.
Caused by: org.gradle.internal.instantiation.ClassGenerationException: Could not generate a decorated class for type KotlinAndroidTarget.
Caused by: java.lang.NoClassDefFoundError: com/android/build/gradle/api/BaseVariant
Caused by: java.lang.ClassNotFoundException: com.android.build.gradle.api.BaseVariant
It seems you're right, how can I find out why it's behaving this way?
m

mbonnin

03/16/2023, 8:16 PM
Mmm looks more like no AGP is loaded in your classpath
Did you apply the android plugin?
c

CLOVIS

03/16/2023, 8:17 PM
com.android.library
, yes
m

mbonnin

03/16/2023, 8:17 PM
Interesting... Maybe it's not loaded in the same classpath as your Kotlin plugin
Make sure to put them one next to the other
If you're applying AGP in the root build.gradle.kts, put KGP next
Same if your in buildSrc, or buildscript {}
c

CLOVIS

03/16/2023, 8:19 PM
I tried Kotlin-then-AGP, and the opposite, same thing It's a regular project, not the root project nor buildSrc, I am referring to both plugins using a version catalog though
m

mbonnin

03/16/2023, 8:19 PM
Note that by applying, I mean "load the jar" so
Copy code
plugins {
  id("com.android.application").version(foo).apply(false)
}
is also loading the jar
Apply AGP first ?
Maybe it's a simple ordering issue
c

CLOVIS

03/16/2023, 8:20 PM
Doesn't seem to change anything
Ah, I added the plugin with apply(false) to the root project and now it's fine
m

mbonnin

03/16/2023, 8:24 PM
Welcome to classloader hell
c

CLOVIS

03/16/2023, 8:25 PM
🎉
418 Views