由于gradle文件中的方法调用问题而导致编译问题

cfh9epnr  于 2022-12-19  发布在  其他
关注(0)|答案(1)|浏览(249)

我是一个新手在Kotlin我下面的Android房间查看- Kotlin和有以下错误出现在build.gradle文件。

Build file 'C:\Users\suraj.manjunath\AndroidStudioProjects\RoomWordSample\build.gradle' line: 3

Could not compile build file 'C:\Users\suraj.manjunath\AndroidStudioProjects\RoomWordSample\build.gradle'.
> startup failed:
  build file 'C:\Users\suraj.manjunath\AndroidStudioProjects\RoomWordSample\build.gradle': 3: only id(String), alias(Provider), or alias(ProviderConvertible) method calls allowed in plugins {} script block

这是我的build.gradle(项目级别)

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
    ext.kotlin_version = '1.5.31'
    id 'com.android.application' version '7.3.1' apply false
    id 'com.android.library' version '7.3.1' apply false
    id 'org.jetbrains.kotlin.android' version '1.7.20' apply false

}
ext {
    activityVersion = '1.4.0'
    appCompatVersion = '1.4.0'
    constraintLayoutVersion = '2.1.2'
    coreTestingVersion = '2.1.0'
    coroutines = '1.5.2'
    lifecycleVersion = '2.4.0'
    materialVersion = '1.4.0'
    roomVersion = '2.3.0'
    // testing
    junitVersion = '4.13.2'
    espressoVersion = '3.4.0'
    androidxJunitVersion = '1.1.3'
}

这是我的build.gradle应用级别

plugins {
    id 'com.android.application'
    id 'org.jetbrains.kotlin.android'
    id 'kotlin-kapt'

}

android {
    namespace 'com.example.roomwordsample'
    compileSdk 32


    defaultConfig {
        applicationId "com.example.roomwordsample"
        minSdk 21
        targetSdk 32
        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
    }
    packagingOptions {
        exclude 'META-INF/atomicfu.kotlin_module'
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

dependencies {
    implementation "androidx.appcompat:appcompat:$rootProject.appCompatVersion"
    implementation "androidx.activity:activity-ktx:$rootProject.activityVersion"

    // Dependencies for working with Architecture components
    // You'll probably have to update the version numbers in build.gradle (Project)

    // Room components
    implementation "androidx.room:room-ktx:$rootProject.roomVersion"
    kapt "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:$rootProject.lifecycleVersion"
    implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"

    // Kotlin components
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-core:$rootProject.coroutines"
    api "org.jetbrains.kotlinx:kotlinx-coroutines-android:$rootProject.coroutines"

    // UI
    implementation "androidx.constraintlayout:constraintlayout:$rootProject.constraintLayoutVersion"
    implementation "com.google.android.material:material:$rootProject.materialVersion"

    // Testing
    testImplementation "junit:junit:$rootProject.junitVersion"
    androidTestImplementation "androidx.arch.core:core-testing:$rootProject.coreTestingVersion"
    androidTestImplementation ("androidx.test.espresso:espresso-core:$rootProject.espressoVersion", {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation "androidx.test.ext:junit:$rootProject.androidxJunitVersion"
}

我将行ext.kotlin_version = '1.5.31'更改为id "ext.kotlin_version "version '1.5.31',这导致错误

* Exception is:
org.gradle.api.plugins.UnknownPluginException: Plugin [id: 'ext.kotlin_version', version: '1.5.31'] was not found in any of the following sources:

- Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
- Plugin Repositories (could not resolve plugin artifact 'ext.kotlin_version:ext.kotlin_version.gradle.plugin:1.5.31')
  Searched in the following repositories:
    Gradle Central Plugin Repository
    Google
    MavenRepo
    at org.gradle.plugin.use.internal.DefaultPluginRequestApplicator.resolveToFoundResult(DefaultPluginRequestApplicator.java:243)

请帮助解决此问题。

62lalag4

62lalag41#

你从哪里得到这个设置?模块级build.gradle应该有这个插件块:

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
}

项目级的应该有这个buildscript(不是 plugins)块:

buildscript {
    ext.kotlin_version = '1.5.31'

    ...
}

你得到这个错误是因为你试图在一个 plugins 块中定义ext.kotlin_version变量,而这是不允许的。如果你按照链接中的教程,这就是它告诉你如何设置的。你也可以查看解决方案存储库的完整的project-levelmodule-level构建文件。如果您想查看完整设置(请记住,他们使用的是更新的Gradle版本!)

相关问题