【问题标题】:Build failed with an exception error in Kotlin在 Kotlin 中构建失败并出现异常错误
【发布时间】:2021-03-15 16:21:16
【问题描述】:

在 android studio 上更新我的 Kotlin 插件后出现此错误。

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.

当我运行堆栈跟踪时,这是我得到的响应:

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 (app) 文件

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(测试)

// 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 文件夹,并创建了新项目,但问题仍然存在。

【问题讨论】:

    标签: android-studio kotlin gradle build.gradle


    【解决方案1】:

    这告诉您,您创建了一个 Android 项目,但 Android 应用程序使用 Android 生命周期来运行,而不是主生命周期。

    如果您想执行普通的 kotlin 代码,一种选择是使用 File -> New Module -> Kotlin 将 kotlin 库添加到项目中,然后在其中添加 main 方法

    【讨论】:

    • 它可以工作,但奇怪的是我现在必须创建一个模块才能成功运行我的 kotlin 代码。在我更新 Kotlin 插件之前,它曾经运行良好。谢谢
    • stackoverflow.com/a/54219287/7952086 - 这是我之前能够做到的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 2020-03-25
    • 2020-05-09
    • 1970-01-01
    • 2023-03-22
    相关资源
    最近更新 更多