Android Studio执行kotlin的main方法错误: 找不到或无法加载主类

问题描述

创建kotlin文件,编写main函数如:

image.png

点击左边三角形运行其main方法报错如下,google,baidu了很多方法都没说到点子上,更多的是答非所问。

image.png

解决方案

1、在根目录的build.gradle的buildscript中指定kotlin版本:

ext.kotlin_version = '1.3.71'
并在dependencies中添加依赖:
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
工程根目录下build.gradle整体代码如下:

buildscript {
    ext.kotlin_version = '1.3.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0'
        classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

    }
}

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

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

2、在app的build.gradle中apply:

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
并在其dependencies中添加依赖
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
app目录下build.gradle整体代码如下:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-android'
android {
    compileSdkVersion 29
    defaultConfig {
        applicationId "com.example.xxx.testapplication"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    
    
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"

}

同步工程后,再次运行结果如下:


image.png

你可能感兴趣的:(Android Studio执行kotlin的main方法错误: 找不到或无法加载主类)