【问题标题】:Compilation issue due to method calls issue in gradle file由于 gradle 文件中的方法调用问题导致的编译问题
【发布时间】:2023-02-19 17:04:35
【问题描述】:

我是科特林的新手 我正在关注 Android Room with a View - 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)

请帮助解决问题。

【问题讨论】:

    标签: android kotlin gradle


    【解决方案1】:

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

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

    而项目级的应该有这个构建脚本(不是插件) 堵塞:

    buildscript {
        ext.kotlin_version = '1.5.31'
    
        ...
    }
    

    您收到错误是因为您试图在 a 中定义 ext.kotlin_version 变量插件块,那里是不允许的。如果您按照链接中的教程进行操作,这就是它告诉您进行设置的方式。如果您想查看完整设置,您还可以查看解决方案存储库的完整 project-levelmodule-level 构建文件(请记住,他们使用的是较新的 Gradle 版本!)

    【讨论】:

      【解决方案2】:

      正如 cactustictacs 所说,放置 kotlin 版本的地方是在 build.gradle 项目级文件中,它必须放在 plugins {} 块之前:

      buildscript {
      ext.kotlin_version = '1.5.31' 
      ......
      }
      plugins{
      .....
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-17
        相关资源
        最近更新 更多