1

I create a new project with kotlin And make a file to execute a main fun, but got a ClassNotFoundException Did much research, but didn't get solution. Hearing from websites, it maybe android studio's bug. I don't know, so ask

Exception in thread "main" java.lang.ClassNotFoundException: com.jafir.teststepsensor.TestDelegation
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:107)

this is build.gradle in app

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.jafir.teststepsensor"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    api "org.jetbrains.kotlin:kotlin-stdlib:$kotlinVersion"

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

And this is build.gradle in project

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlinVersion = '1.1.51'

    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

gradle:

gradle: gradle-4.1-all

code:

object TestDelegation {
    @JvmStatic
    fun main(array: Array<String>) {
        println("main")
    }
}

I just simply create a object class, want to execute main fun like java It throws this.

But when I revert gradle to 3.3-all and gradle.build.tool to 2.3.3 It's ok I don't know why.. Maby it's related to gradle and gradle build tool's version

8
  • replace object keyword with class Commented Dec 20, 2017 at 9:19
  • I tried it, and not work.It is still like that Commented Dec 20, 2017 at 9:52
  • You are running this on Android? Because this is not an Android style of code. Android works on the basis of Activity and not based on Main function. To see how a android app looks start a new project in Android Studio and it will automatically create a Hello World app for you. Commented Dec 20, 2017 at 9:56
  • Yes you are right, it's a simple android project. I just want to add a kotlin file and test it. So, android project can't allow ? But look at the question I changed.When I revert to 2.3.3 and 3.3-all, it's ok, it seems that it's ok to execute a kotlin file in android project Commented Dec 20, 2017 at 10:03
  • Java program has a main method which gets executed automatically on the start of the program. Android will never have a main method. that is the reason you are getting that error. Commented Dec 20, 2017 at 10:07

1 Answer 1

1

This is the main function. There is no support for running main function in Android Studio for Kotlin. You can do that for Java, but not Kotlin.

You can use object or Class, but it wouldn't work because Android studio doesn't support it. There is a bug filed for this: https://issuetracker.google.com/issues/68021152

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I've seen this issue. It said it would be solved at alpha2, but not. And you guys all say, main of kotlin can't excecute under android studio, but how to explain that I execute it under gradle tool 3.0 version. like gradle tool 2.3.3 and gradle 3.3-all. I guess it may be the android studio 3.0's bug which related to gradle and gradle tool
The issue is clear about the problem being resolved for non-android modules. If you create a new module that does not reference the Android SDK, then you can run either Java or Kotlin classes on your local machine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.