【问题标题】:Could not find method kapt() for arguments找不到参数的方法 kapt()
【发布时间】:2019-05-18 07:20:08
【问题描述】:

我现在面临一个问题超过 3 天,但我无法解决。 自从我开始在 Android 上使用 Kotlin,我停止使用“annotationProcessor”并开始使用“kapt”,直到我开始构建一个 Android Instant App,当我将“kapt”添加到像 Glide 这样的任何依赖项中时,所有的东西都可以很好地使用 kapt或 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.

这是我的项目 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
}

应用模块 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')
}

基础模块 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'

}

功能模块 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'

}

instantapp gradle 文件

apply plugin: 'com.android.instantapp'

dependencies {
    implementation project(':feature')
    implementation project(':base')
}

===========

已解决

通过在基础模块中添加 kotlin-android 插件

apply plugin: 'kotlin-android'

【问题讨论】:

  • 不应该也申请apply plugin: 'kotlin-android'吗?
  • 谢谢,它现在可以工作了,我没有注意到基础模块中没有应用 kotlin-android 插件。
  • 最好回答您的问题并将其标记为已接受,以便更好地了解解决方案/修复。

标签: android gradle kotlin android-gradle-plugin android-instant-apps


【解决方案1】:
Use plugin with this order,

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
apply plugin:  'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'

【讨论】:

    【解决方案2】:

    所以你也忘了加

    apply plugin: 'kotlin-kapt'
    apply plugin: 'dagger.hilt.android.plugin'
    

    在应用级别的gradel文件中。不错。

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        就我而言,我错过了

        apply plugin: 'kotlin-kapt'
        

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

        【讨论】:

        • 是的,它奏效了。这在我的旧项目中丢失了,并且在迁移到 AndroidX 时遇到了同样的错误。谢谢。
        • 我在添加 Hilt 时发生了这种情况。上述解决方案有效
        • id 'kotlin-kapt'.
        • 确保它位于基本模块 gradle 文件的顶部。放在底部是不行的。
        猜你喜欢
        • 2019-03-05
        • 2019-09-29
        • 1970-01-01
        • 2017-01-23
        • 2017-03-22
        • 2018-05-24
        • 2021-11-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多