Android项目中找不到名为'kapt'的配置

53
我正在尝试在我的项目中使用Realm数据库,我添加了它。
classpath "io.realm:realm-gradle-plugin:5.9.0"

build.gradle

并且

apply plugin: 'realm-android'

在模块的 build.gradle 文件中,点击同步后我收到了以下错误信息:
Configuration with name 'kapt' not found.

build.gradle 的内容:

buildscript {
    ext.kotlin_version = '1.3.21'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.jakewharton.hugo:hugo-plugin:1.2.1'
        classpath 'com.novoda:bintray-release:0.9'
        classpath "io.realm:realm-gradle-plugin:5.9.0"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

build.gradle 模块内容:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.jakewharton.hugo'
apply plugin: 'com.novoda.bintray-release'
apply plugin: 'realm-android'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 28

    defaultConfig {
        applicationId "xx.xxxxx.xxxxxxxx"
        minSdkVersion 21
        versionCode 1
        versionName "1"
        vectorDrawables.useSupportLibrary = true
        multiDexEnabled = true
        javaCompileOptions {
            annotationProcessorOptions {
                includeCompileClasspath false
            }
        }
    }

    dataBinding {
        enabled = true
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/rxjava.properties'
    }

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

/* IMPORTANT : 
 * Be careful when update dependencies, different version library may caused error */

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    //noinspection GradleCompatible
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    implementation 'com.android.support:support-v13:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.android.support:support-vector-drawable:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    // google maps library ------------------------------------------------------------------------
    implementation 'com.google.android.gms:play-services:12.0.1'

    // google gson --------------------------------------------------------------------------------
    implementation 'com.google.code.gson:gson:2.8.4'
    // third party dependencies 
    implementation 'com.github.smart-fun:TabStacker:1.0.4'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'org.jetbrains:annotations-java5:15.0'      // add this line
    implementation 'com.fatboyindustrial.gson-jodatime-serialisers:gson-jodatime-serialisers:1.2.0'

    implementation 'com.github.armcha:AutoLinkTextView:0.3.0'
    implementation "com.github.bumptech.glide:glide:4.8.0"
    annotationProcessor "com.github.bumptech.glide:compiler:4.8.0"

    implementation 'io.reactivex.rxjava2:rxandroid:2.1.0'
    implementation 'io.reactivex.rxjava2:rxjava:2.2.4'
    implementation 'com.squareup.retrofit2:adapter-rxjava:2.2.0'
    implementation 'com.google.dagger:dagger:2.15'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
    implementation 'com.google.dagger:dagger-android:2.15'
    implementation 'com.google.dagger:dagger-android-support:2.10'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.10'
    implementation 'com.google.code.gson:gson:2.8.4'
    implementation 'com.jakewharton.timber:timber:4.7.1'
    implementation('io.socket:socket.io-client:1.0.0') {
        exclude group: 'org.json', module: 'json'
    }
    implementation 'com.birbit:android-priority-jobqueue:2.0.1'
    implementation 'com.github.onehilltech.concurrent:concurrent-android:0.8.1'
    implementation 'com.balysv.materialmenu:material-menu:2.0.0'
    implementation 'com.squareup.picasso:picasso:2.71828'
    implementation 'com.android.support:multidex:1.0.3'
    implementation 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
    implementation 'com.blankj:utilcode:1.22.10'
    implementation 'com.ncapdevi:frag-nav:3.0.0'
    implementation 'com.daimajia.easing:library:1.0.0@aar'
    implementation 'com.daimajia.androidanimations:library:1.1.2@aar'
    implementation 'com.nineoldandroids:library:2.4.0'
    implementation 'org.parceler:parceler-api:1.1.8'
    annotationProcessor 'org.parceler:parceler:1.1.8'
    implementation 'com.squareup.retrofit2:retrofit:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.3.0'
    implementation 'com.pnikosis:materialish-progress:1.7'
    implementation 'org.greenrobot:eventbus:3.1.1'
    implementation project(path: ':bottomsheet-commons')
    implementation 'org.jsoup:jsoup:1.9.2'

    kapt "io.realm:realm-annotations-processor:5.9.0"
}

kapt {
    correctErrorTypes = true
}

repositories {
    mavenCentral()
}

configurations {
    cleanedAnnotations
    compile.exclude group: 'org.jetbrains', module: 'annotations'
}
4个回答

117

我曾经遇到过同样的问题,但是我缺少了这行代码。

apply plugin: 'kotlin-kapt'
尝试后,我发现在apply plugin: 'realm-android'之前添加以下代码对我有用。
在我的情况下,我的working build.gradle的顶部部分如下:
apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'

因此,请尝试以下操作:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.jakewharton.hugo'
apply plugin: 'com.novoda.bintray-release'
apply plugin: 'realm-android'

如果那不起作用,尝试将该行移动到“apply realm plugin”行的正上方(即在Hugo和Bintray插件下方)。


5
Realm.io应该在它们的网站上提供这个指南。我们不应该在这里寻找答案和解决方案,这是毫无意义的。请帮忙转达此意。 - Seop Yoon
1
我喜欢那个 before 词 :P - Pratik Butani
1
没有注意到并使用了apply plugin: 'com.android.library',这破坏了我的构建,请确保如果是应用程序,则使用apply plugin: 'com.android.application' - WilliamNHarvey
1
使用较新版本的Android Studio,请将其放置在插件部分内。插件 { id 'com.android.application' id 'kotlin-android' id 'kotlin-kapt' } - ShresthaGanesh

3

尝试输入:

apply plugin: 'kotlin-kapt'

before:

apply plugin: 'realm-android'

我遇到了同样的问题,这个方法对我有效。


2
如果您正在使用Android Studio ArcticFox的某个版本,则有一种新的声明这些插件的方法。在您的build.gradle(应用程序级别)上,它应该像这样:
plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-kapt'
    id 'realm-android'
}

android {
    ....
}

realm {
    syncEnabled = true
}

dependencies {
    ....
}

别忘了 build.gradle (项目级别)。

buildscript {
ext.kotlin_version = "1.6.0"
ext.realm_version = '10.8.1'
repositories {
    google()
    mavenCentral()
    maven { url 'https://jitpack.io' }
    jcenter()
}
dependencies {
    classpath "com.android.tools.build:gradle:7.0.3"
    classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    classpath "io.realm:realm-gradle-plugin:$realm_version"

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

}


0

我曾经遇到过同样的问题,我的项目在git上,我只是执行了pull操作,然后就出现了Kapt()未找到的问题。

解决方法:清除缓存并重新启动/在顶部添加apply plugin: 'kotlin-kapt'/同步项目


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接