构建失败,并在Kotlin中出现异常错误 [英] Build failed with an exception error in Kotlin
问题描述
在android studio上更新我的Kotlin插件后收到此错误.
Got this error after updating my Kotlin plugin on android studio.
package com.example.test
fun main(args: Array<String>) {
println("Hello world")
}
这是我收到的错误:
* Where:
Initialization script '/private/var/folders/bn/st769wd16nv1d5_wccw8_tm00000gn/T/Test_main__2.gradle' line: 27
* What went wrong:
A problem occurred configuring project ': app'.
> Could not create task ':app:Test.main()'.
> SourceSet with name 'main' not found.
运行堆栈跟踪时,这是我得到的响应:
When I run a stack trace, this is the response I get:
Caused by: org.gradle.api.internal.tasks.DefaultTaskContainer$TaskCreationException: Could not create task ':app:Test.main()'.
Caused by: org.gradle.api.UnknownDomainObjectException: SourceSet with name 'main' not found.
这是我的build.gradle(应用程序)文件
Here is my build.gradle (app) file
plugins {
id 'com.android.application'
id 'kotlin-android'
}
android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.test"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = '1.8'
}
}
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.2'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.1'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
build.Gradle(测试)
build.Gradle (test)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.4.20"
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:4.1.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// 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
}
我已经更新了所有依赖项,使缓存无效并重新启动,尝试删除.idea和.gradle文件夹,并创建了新项目,但是问题仍然存在.
I have updated all my dependencies, invalidated caches and restarted, tried to delete my .idea and .gradle folder, and created new projects but the issue still persists.
推荐答案
这是告诉您,您创建了一个Android项目,但是Android应用程序使用Android生命周期运行,而不是main生命周期.
This is telling you that you created an Android project but that Android applications use the Android lifecycle to run, not main.
如果您要执行普通的kotlin代码,一种选择是使用File->向项目添加kotlin库.新模块-> Kotlin并在其中添加主要方法
If you want to execute plain kotlin code one option is to add a kotlin library to the project using File -> New Module -> Kotlin and add the main method there
这篇关于构建失败,并在Kotlin中出现异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!