1
votes

In my Android Studio project's java module,I used to code java's main method is OK,But when i convert it to Kotlin code ,like this :

fun main(args: Array<String>) {
    println("i am kotlin")
}

When i executed it, the error happend:"error: can't find or load main class". I have tried to find some way to solve it ,for example:let the main method outside the class,But it does not work. I want to know whether it is bebcause that my configuration has some problems. When i use IntelliJ IDEA i build a kotlin project ,the main method is OK. so what is my problem in my Android studio's java module in kotlin code.

1
I have the same problem. Do you resolve it ?Tony

1 Answers

0
votes

In kotlin, the main function should be outside of a class which didn't work for you. So when you're trying to use the main function of a class it has to be wrapped up by companion object also be annotated by @JvmStatic. So the code block will look like this.

class AnythingYouWant {

    companion object {
       @JvmStatic
       fun main(args: Array<String>) {
          println("i am kotlin")
       }
    }
}

Another thing to remind you that,

The main method isn't the entry point in Android like in Java

because it uses completely different build configuration in Android Studio project than the IntelliJ project.