0
votes

I have an Android app written in Java and I am trying to write a new feature in Kotlin. So I added a new Kotlin file and IntelliJ offered to setup the project for Kotlin.

The issue is that when trying to create a Kotlin object in Java, compilation fails with

error: cannot find symbol constructor MyClassKt()

My Kotlin file (MyClass.kt):

 val SCREEN = 1;

 class MyClass() {
     fun hello(view: View) {
     } 
 }

In my app module:

implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

In my project module:

 classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

Invocation in Java:

 new MyClassKt();
3
your kotlin class seems to be MyClass and not MyObject ?!! - SebastienRieu
Did you apply Kotlin plugin? The apply plugin: 'kotlin-android' on the top of your app module. - cycki
Could you add how you are invoking it from Java? - Sebastian Pakieła
Thanks all! Updated the question, added the invocation, fixed the typos. - netcyrax

3 Answers

1
votes

Make sure you have these plugin on top in the app gradle file

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

And inside dependencies, should be like

dependencies {
def kotlin_version= "2.2.1"
implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"       
}
0
votes

Remove brackets from kotlin class definition.

class MyClassXD {
    fun hello(view: View) {
    }
}

Probably Java got wild when it saw brackets in classname.

0
votes

The problem was that Kotlin was creating a MyClassKt for the file (i.e. for accessing the SCREEN variable).

I just needed to use the normal class name and do new MyClass() (no Kt postfix).

Thanks all!