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

 

4 comments
Comment actions Permalink

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.

1
Comment actions Permalink

Thanks for the quick reply.

I do have the java module set as a dependency of my project.

In my XML, i tried both:


    <depends>com.intellij.modules.java</depends>

and:

    <depends>com.intellij.java</depends>

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?

 

0
Comment actions Permalink

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'
}
```

0
Comment actions Permalink

[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:

org.jetbrains.kotlin:kotlin-compiler-embeddable:1.3.70

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:

    <depends>com.intellij.modules.java</depends>
<depends>org.jetbrains.kotlin</depends>

Hopefully this will be helpful in solving related issues for others.

1

Please sign in to leave a comment.