错误:(1, 0) 未找到 id 为“kotlin-android-extensions”的插件

问题描述 投票:0回答:5
apply plugin: 'kotlin-android-extensions'.

当我在 android studio 预览中添加此扩展时,出现此错误 “错误:(1, 0) 未找到 ID 为 'kotlin-android-extensions' 的插件。”。

我的构建gradle

   apply plugin: 'com.android.application'

android {
    compileSdkVersion 26
    buildToolsVersion "26.0.1"
    defaultConfig {
        applicationId "com.example.mohamed_elbaz.myapplication"
        minSdkVersion 15
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.0.2'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
android-studio android-studio-3.0 kotlin-android-extensions
5个回答
84
投票

您发布的 build.gradle 片段看起来像应用程序模块内的配置。项目根目录中的 build.gradle 是什么样的?要添加 kotlin 插件依赖项,它应该如下所示:

buildscript {
    ext.kotlin_version = '1.6.10'
    repositories {
        jcenter()
        google()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.0-beta6'
        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 {
        jcenter()
        google()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

重要的部分是添加 kotlin 插件的行

classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"


67
投票

要使用该插件,您必须将其添加到根 build.gradle 文件中

buildscript {
ext.kotlin_version = '1.1.60'
    repositories {
        jcenter()
        maven {
            url 'https://maven.google.com'
        }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}

3
投票

在Android Studio中我们可以通过以下方式修复:_

App Level Gradle 中使用插件领域特定语言 (DSL):

plugins {
    id "org.jetbrains.kotlin.android.extensions" version "1.4.21"
}

这就是修复它的全部内容。

但是,如果您使用旧版插件应用程序,请在 App Level Gradle 中添加以下内容:

buildscript {
  repositories {
    maven {
      url "https://plugins.gradle.org/m2/"
    }
  }
  dependencies {
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.21"
  }
}

apply plugin: "org.jetbrains.kotlin.android.extensions"

希望对您有所帮助!


2
投票

将以下行放入 build.gradle 应用程序中

        apply plugin: 'com.android.application'
         apply plugin: 'kotlin-android'

还可以通过添加 kotlin 扩展和路径来更改构建 gradle //将ext放在依赖线上方//

    ext.kotlin_version = '1.4.31'
 dependencies {
    classpath "com.android.tools.build:gradle:4.2.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
}

0
投票

在 Android Studio Hedgehog 中 | 2023.1.1

build.gradle 项目:

// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.2.0' apply false
id "com.google.gms.google-services" version "4.3.14" apply false
id 'org.jetbrains.kotlin.android' version '1.9.10' apply false // add
}

build.gradle 应用程序:

plugins {
    id 'com.android.application'
    id 'com.google.gms.google-services'
    id 'org.jetbrains.kotlin.android'
}
android {
....

    kotlinOptions {
        jvmTarget = "1.8"
    }

}
dependencies {
 implementation("androidx.core:core-ktx:1.12.0")
}
© www.soinside.com 2019 - 2024. All rights reserved.