创建XML文件时出现卡顿exception

我正在Android Studio上的Kotlin工作。

该项目编译和工作完美,但后来当我尝试创建一个新的XML文件,我不断得到一个kaptexception“注释处理exception”。

我试图创建XML文件,右键单击res文件夹 – >新建 – >布局资源文件,我也尝试右键单击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文件,没有得到这个错误? 或者,如果这是不可能的,我该如何解决这个Kaptexception?

谢谢

 sourceSets { main.java.srcDirs += [file("$buildDir/generated/source/kapt/main")] } 

你可以添加这个源集,然后再试一次吗? Kapt stubs编译器允许从Kotlin引用“生成”源,否则编译器将无法引用缺失的源。

编辑:添加后

 kapt { generateStubs = true } 

更新:

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

和改变

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

好,

经过大量的kaptexception,回到之前的提交以避免它们,我想我发现了一些可以有这个错误的原因。

  • 您在您的XML文件中使用了相关Kotlin文件中不存在的variables(谢谢复制/粘贴)
  • 你忘了关闭你的一个标签

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

谢谢Ege的帮助。