找不到参数的方法 kapt()

O.A*_*set 70 android gradle kotlin android-gradle-plugin android-instant-apps

我现在面临一个问题超过 3 天,我无法解决。自从我开始在 Android 上使用 Kotlin 以来,我停止使用“annotationProcessor”并开始使用“kapt”,在我开始构建 Android 即时应用程序之前,所有事情都在 kapt 上运行良好,当我将“kapt”添加到任何依赖项(如 Glide)时或 ButterKnife Gradle 总是显示找不到方法 kapt() 的错误

Could not find method kapt() for arguments [com.github.bumptech.glide:compiler:4.7.1] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.
Run Code Online (Sandbox Code Playgroud)

这是我的项目 Gradle 文件

项目gradle文件

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.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
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

应用模块gradle文件

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

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "com.demo.instantapptest.app"
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"


    }

    buildTypes {
       release {
           minifyEnabled false
           proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
       }
    }

}

dependencies {
    implementation project(':feature')
    implementation project(':base')
}
Run Code Online (Sandbox Code Playgroud)

基本模块gradle文件

apply plugin: 'com.android.feature'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    baseFeature true
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    api 'com.android.support:appcompat-v7:28.0.0'
    api 'com.android.support.constraint:constraint-layout:1.1.3'
    application project(':app')
    feature project(':feature')


    implementation "android.arch.lifecycle:extensions:1.1.1"
    kapt "android.arch.lifecycle:compiler:1.1.1"

    implementation 'com.github.bumptech.glide:glide:4.7.1'
    kapt 'com.github.bumptech.glide:compiler:4.7.1'

    implementation 'com.google.dagger:dagger:2.15.0'
    kapt 'com.google.dagger:dagger-compiler:2.15.0'

    implementation 'com.jakewharton:butterknife:9.0.0-rc2'
    kapt 'com.jakewharton:butterknife-compiler:9.0.0-rc2'

}
Run Code Online (Sandbox Code Playgroud)

功能模块gradle文件

apply plugin: 'com.android.feature'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28
    defaultConfig {
        minSdkVersion 17
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation project(':base')
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

}
Run Code Online (Sandbox Code Playgroud)

Instantapp gradle 文件

apply plugin: 'com.android.instantapp'

dependencies {
    implementation project(':feature')
    implementation project(':base')
}
Run Code Online (Sandbox Code Playgroud)

==========

解决了

通过将 kotlin-android 插件添加到基本模块

apply plugin: 'kotlin-android'
Run Code Online (Sandbox Code Playgroud)

Mih*_*hai 164

就我而言,我失踪了

apply plugin: 'kotlin-kapt'
Run Code Online (Sandbox Code Playgroud)

希望它能帮助像我这样的盲人。

  • `id 'kotlin-kapt'`。 (4认同)

Md.*_*lla 48

这是最新版本的 android studio 的工作解决方案,而不是id 'kotlin-kapt'在插件中应用插件使用。

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
} 
Run Code Online (Sandbox Code Playgroud)


The*_*ner 27

如果有人在Room 库中遇到困难:

我尝试了以上所有技巧,但没有任何帮助,除非我改变这个悲惨的:

ksp("androidx.room:room-compiler:$roomVersion")
Run Code Online (Sandbox Code Playgroud)

进入:

annotationProcessor("androidx.room:room-compiler:$roomVersion")
Run Code Online (Sandbox Code Playgroud)

一开始我也改变了:

val roomVersion = "2.4.2"
Run Code Online (Sandbox Code Playgroud)

进入:

def roomVersion = "2.4.2"
Run Code Online (Sandbox Code Playgroud)

这终于完成了任务!


小智 18

当我尝试在我的 android 项目中添加 Room 库时,我遇到了同样的错误。截至 2022 年 8 月 17 日,我可以通过在build.gradle(:app)中添加以下行来解决问题。

在我添加的插件块中;

id 'kotlin-kapt'
Run Code Online (Sandbox Code Playgroud)

依赖项块中,我添加了;

def room_version = "2.4.3"

implementation "androidx.room:room-runtime:$room_version"
annotationProcessor "androidx.room:room-compiler:$room_version"
// To use Kotlin annotation processing tool (kapt)
kapt "androidx.room:room-compiler:$room_version"
Run Code Online (Sandbox Code Playgroud)

这样做,我可以告别“无法找到参数的方法 kapt()”。希望有人可以节省一些时间阅读这篇文章。


小智 11

所以你还忘记添加

apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
Run Code Online (Sandbox Code Playgroud)

在应用程序级别的 Gradel 文件中。好的。


Say*_*ooj 5

我遇到过这个

找不到参数的 kapt() 方法

当我尝试dagger在我的项目中添加依赖项时,出现错误。这是最新版本 Android Studio 的工作解决方案。

plugins {
   id 'com.android.application'
   id 'org.jetbrains.kotlin.android'
   id 'kotlin-kapt'   //Add this line in the plugins block usually seen at the top of build.gradle
}
Run Code Online (Sandbox Code Playgroud)