Android Kotlin KaptExecution错误集合

文件夹命名为interface导致

Module级别build.gradle下引入了apply plugin: 'kotlin-kapt',构建时出现下面的报错提示.

public abstract class BaseStateActivity<T extends androidx.databinding.ViewDataBinding, E extends xxxx.BaseVM> extends xxxxx.BaseVMActivity<T> implements xxxxx.IBindVm<E> {错误: 非法的类型开始
public abstract class BaseStateActivity<T extends androidx.databinding.ViewDataBinding, E extends xxxxx.BaseVM> extends xxxxx.BaseVMActivity<T> implements xxxxx.IBindVm<E> {错误: 需要';'
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)    
问题解决:

唉,这个问题困扰了我太久了.后来在stackoverflow上看到有个倒霉的家伙也踩进了这个坑中.他比我早踩了几天花了一周时间解决的,幸好他给了解决的办法.0.0.导致这个问题的原因是我项目中有个文件夹以interface命名,这个文件夹里有个在使用接口.interface这个关键字当作文件夹名字把注释器搞混了,最终在生成stub时候出错了.我后来把interface文件夹名字改成别的名字就好了.

来源

DataBinding语法导致

出现以下log一定要用鼠标点下Run with --stacktrace,自己看看详细的信息.

* What went wrong:
Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
   > java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

我的自定义View增加了双向绑定功能,但是写的时候疏忽了,写成了这样

app:y_change_content="@={}"

然后构建的时候log打印What went wrong,点击Run with --stacktrace得到下面的信息

Caused by: android.databinding.tool.util.LoggedErrorException: Found data binding error(s):
[databinding] {"msg":"Syntax error: no viable alternative at input \u0027\u003cEOF\u003e\u0027","file":"app\\src\\main\\res\\layout\\fragment_employment_info.xml","pos":[{"line0":54,"col0":41,"line1":54,"col1":44}]}

上面提示的很清除,语法错误,已经定位到了错误在哪一行了.

Java类的名字与Kotlin类的名字相同导致

Java类与Kotlin类的名字相同,有时Studio不会在目录里面出现红线提示,这时候你构建就又会出现一个KaptExecution,点击Run with --stacktrace得到下面的信息.

Caused by: org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: A failure occurred while executing ...
Caused by: java.lang.reflect.InvocationTargetException
Caused by: com.sun.tools.javac.util.ClientCodeException: java.lang.IllegalStateException: node.sym must not be null
Caused by: java.lang.IllegalStateException: node.sym must not be null

把类名改下再重新构建就好了.
来源


  • 2020/12/18更新

接口没有写清楚包名导致InvocationTargetException

  • 问题:引入apply plugin: 'kotlin-kapt'后,构建项目报错.我用koin做依赖注入,构建后发现找不到Retrofit创建的动态代理API对象.只要去除引入的kotlin-kapt就能正常的编译.
  • 原因:最终发现是因为被代理的接口类文件中没有包名,导致所有使用该接口的类编译时找不到该动态代理接口.但是去除kotlin-kapt后,没有包名也不会影响项目编译.
    例如:
// 包名.如果把这个去掉,我用的是koin进行依赖注入的,当引入'kotlin-kapt'编译就会提示EZLoanApiService这个接口找不到.移除就能正常编译.
package com.xxx.xxx.netxx
interface ApiService {
    
    
    @GET(CHECK_PHONE_EXIST)
    suspend fun checkPhone(@QueryMap mData: TreeMap<String, Any>): BResponse<CheckPhoneExist>
}

猜你喜欢

转载自blog.csdn.net/MoLiao2046/article/details/108322320