Android studio plugin using kotlin PSI
Answered
I am making a plugin for android studio and need to use the kotlin PSI classes.
When trying to run the plugin, i get a linkage error during runtime:
Caused by: java.lang.LinkageError: loader constraint violation: loader com.intellij.ide.plugins.cl.PluginClassLoader @7b4ccd36 wants to load interface com.intellij.psi.PsiFile. A different interface with the same name was previously loaded by com.intellij.util.lang.UrlClassLoader @612679d6. (com.intellij.psi.PsiFile is in unnamed module of loader com.intellij.util.lang.UrlClassLoader @612679d6, parent loader 'platform').
In order to use the kotlin PSI classes, i am using the gradle dependency:
"org.jetbrains.kotlin:kotlin-compiler:1.3.70"
I am importing the PSI classes from: "com.intellij.psi.*".
I understand that each plugin in android studio gets its own class loader, is this the cause of my troubles? how can I fix this error?
Thanks,
Omri
Please sign in to leave a comment.
You can read more about the Plugin Class Loaders in our docs.
Do you have java module set as a dependency of your plugin? Please refer to the Java functionality extracted as a plugin post.
Thanks for the quick reply.
I do have the java module set as a dependency of my project.
In my XML, i tried both:
and:
Before reading the link you shared. but changing it did not solve the linkage error.
I have the following build.gradle file:
plugins {
id "org.jetbrains.intellij" version "0.4.21"
id 'org.jetbrains.kotlin.jvm' version '1.3.70'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.70"
implementation "org.jetbrains.kotlin:kotlin-compiler:1.3.70"
}
intellij {
plugins 'java'
version '2019.3.3'
alternativeIdePath '/home/omri/android-studio'
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
I do not have any other declared dependencies in my project.
Are there any other dependencies I need to declare?
Jakub Chrzanowski
What to do in case i want to support both and Java and kotlin? Here is the scenario - Let's say I want parse both Java files and kotlin files.
```
intellij {
plugins 'java','kotlin'
}
```
[SOLVED]
In the end the solution to my problem was fixing 2 things in the build.gradle file:
the first change was changing the imported kotlin compiler library to:
The second change was altering the import modifier to: compileOnly.
The full build.gradle file:
plugins {
id "org.jetbrains.intellij" version "0.4.21"
id 'org.jetbrains.kotlin.jvm' version '1.3.70'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
}
dependencies {
compileOnly "org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.70"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.3.70"
}
intellij {
setPlugins('java', 'android', 'Kotlin')
type = 'IC'
version = '2019.3.3'
alternativeIdePath = <ENTER THE PATH TO YOUR ANDROID STUDIO FOLDER HERE>
}
compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
patchPluginXml {
changeNotes """Add change notes here.<br><em>most HTML tags may be used</em>"""
}
In my META-INF file the required dependencies are:
Hopefully this will be helpful in solving related issues for others.