韩建飞
韩建飞

Reputation: 81

Android studio3.0 kotlin classNotFoundException

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

Upvotes: 1

Views: 1707

Answers (1)

Henry
Henry

Reputation: 17851

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

Upvotes: 1

Related Questions