问题
Got this error after updating my Kotlin plugin on android studio.
package com.example.test
fun main(args: Array<String>) {
println("Hello world")
}
This is the error I receive:
* 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.
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 (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
}
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.
回答1:
This is telling you that you created an Android project but that Android applications use the Android lifecycle to run, not main.
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
来源:https://stackoverflow.com/questions/65129582/build-failed-with-an-exception-error-in-kotlin