创建XML文件时的Kapt异常

时间:2017-12-29 11:23:18

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

我在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
}

这是我的完整应用程序:

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例外?

由于

2 个答案:

答案 0 :(得分:0)

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)
    }
}

答案 1 :(得分:0)

那么,

在经历了很多kapt异常并回到之前的承诺之后,我想我发现了一些可能出现此错误的原因。

  • 您正在XML文件中使用相对Kotlin文件中不存在的变量(感谢复制/粘贴)
  • 您忘记关闭其中一个代码

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

感谢Ege的帮助。