【问题标题】:Kapt exception when creating XML file创建 XML 文件时出现 Kapt 异常
【发布时间】:2017-12-29 11:23:18
【问题描述】:

我在 Android Studio 上的 Kotlin 工作。

项目编译并运行良好,但是当我尝试创建新的 XML 文件时,我不断收到 kapt 异常“注释处理时异常”。

我尝试通过右键单击 res 文件夹 -> 新建 -> 布局资源文件来创建 XML 文件,我也尝试通过右键单击 res 文件夹 -> 新建 -> XML -> 布局 XML 文件。

这是我的完整项目 gradle:

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

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

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

这是我的完整应用程序 gradle:

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

android {
    compileSdkVersion 27
    buildToolsVersion '26.0.2'
    defaultConfig {
        applicationId "com.discodery.android.discoderyapp"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        vectorDrawables.useSupportLibrary = true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
        buildTypes.each {
            it.buildConfigField 'Integer', 'RESTAURANT_ID', '1'
        }
    }
    /*
    productFlavors {
        dev {

        }
        prod {

        }
    }*/
    dataBinding {
        enabled = true
    }
}

kapt {
    generateStubs = true
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"

    //Permissions

    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'io.reactivex:rxandroid:1.2.1'
    compile 'io.reactivex:rxjava:1.1.6'
    compile 'com.hwangjr.rxbus:rxbus:1.0.5'
    compile 'org.parceler:parceler-api:1.1.6'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'
    compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.1.0'
    compile 'com.squareup.okhttp3:okhttp:3.4.1'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'
    compile 'com.squareup.okhttp3:okhttp-urlconnection:3.4.1'
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:support-v4:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.google.dagger:dagger:2.7'
    compile 'com.facebook.fresco:fresco:1.0.0'
    compile 'com.squareup.picasso:picasso:2.3.2'
    compile 'com.nineoldandroids:library:2.4.0'
    compile 'com.daimajia.slider:library:1.1.5@aar'
    compile 'com.karumi:dexter:2.3.1'
    testCompile 'junit:junit:4.12'
    kapt 'com.android.databinding:compiler:2.3.0'
    kapt 'org.parceler:parceler:1.1.6'
    kapt 'com.google.dagger:dagger-compiler:2.7'
}
repositories {
    mavenCentral()
}

apply plugin: 'kotlin-android-extensions'

我尽力了,我在互联网上搜索以解决这个错误。 所以我的问题是: 如何安全地创建 XML 文件而不会出现此错误? 或者,如果这不可能,我该如何解决这个 Kapt 异常?

谢谢

【问题讨论】:

  • 以下是我注意到的一些可能与您的 gradle 问题有关或无关的事情: 1. 将 apply plugin: 'kotlin-android-extensions' 移动到您的应用程序 gradle 的顶部 2. 您有两个 compile 'com.android.support:appcompat-v7:25.3.1' 副本3.compile 'com.daimajia.slider:library:1.1.5@aar'我不确定你是否想要版本4中的@aar。我想你想要compile 'com.android.databinding:compiler:2.3.0'compile com.google.dagger:dagger-compiler:2.7

标签: android android-studio android-gradle-plugin kotlin kapt


【解决方案1】:
sourceSets {
  main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")]
}

您可以添加此源集并重试吗? Kapt 存根编译器允许从 Kotlin 引用“生成”的源代码,否则编译器将无法引用缺失的源代码。

编辑:在之后添加这个

kapt {
  generateStubs = true
}

更新:

尝试删除apply-plugin:'kotlin-kapt'

变化

kapt {
    generateStubs = true
    javacOptions {
        option("-Xmaxerrs", 500)
    }
}

【讨论】:

  • 我在kapt块之后的app gradle文件中添加了这个,正如你所说的,但我仍然有同样的错误:[kapt]发生异常:org.jetbrains.kotlin.kapt3.diagnostic。 KaptError:注释处理时的 Ecveption
  • 谢谢,它似乎有效,我现在有一些新的编译错误,我正在修复它,我会告诉它是否有效
  • 好的,花费的时间比预期的要长,我现在遇到“无法访问 NonExistentClass”错误。感谢您迄今为止提供的建议,我会与您保持联系。
  • 好吧,我建议此时注释掉任何 kap 依赖项 youtrack.jetbrains.com/issue/KT-10352
【解决方案2】:

嗯,

在经历了很多 kapt 异常并返回之前的提交来避免它们之后,我想我发现了一些可能出现此错误的原因。

  • 您在 XML 文件中使用了在您的相关 Kotlin 文件中不存在的变量(感谢复制/粘贴)
  • 您忘记关闭标签之一

这些答案来自我的经验,我将它们放在这里以防比我更新手(很难)得到这个错误,但我相信有更合理的答案。

感谢 Ege 的热心帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多