0

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 Answer 1

2

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

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

2 Comments

It works, However it is bizarre that I now have to create a module to successfully run my kotlin code. It used to work fine before I updated the Kotlin plugin. Thank you
stackoverflow.com/a/54219287/7952086 - This is what I was able to do before

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.