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



我是一个新手我正在使用View - Kotlin跟踪Android房间,并在build.gradle文件上出现以下错误。

Build file 'C:Userssuraj.manjunathAndroidStudioProjectsRoomWordSamplebuild.gradle' line: 3
Could not compile build file 'C:Userssuraj.manjunathAndroidStudioProjectsRoomWordSamplebuild.gradle'.
> startup failed:
build file 'C:Userssuraj.manjunathAndroidStudioProjectsRoomWordSamplebuild.gradle': 3: only id(String), alias(Provider), or alias(ProviderConvertible) method calls allowed in plugins {} script block

这是我的构建。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'
}

这是我的构建。Gradle app level

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)

请帮忙解决这个问题。

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

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

项目级应该有这个buildscript(非plugins) block:

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

你得到的错误,因为你试图在插件块内定义ext.kotlin_version变量,它是不允许的。如果您遵循链接中的教程,这就是它告诉您如何设置的方法。你也可以查看解决方案仓库的完整项目级和模块级构建文件,如果你想看到完整的设置(只要记住他们使用的是更新的Gradle版本!)

正如cactustictacs所说,kotlin版本应该放在构建中。Gradle项目级文件,它必须放在plugins{}块之前:

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

最新更新