Android Studio中kapt调试annotation processor的一些注意事项

首先按照以下三张图所示新建一个用来debug annotation processor的configuration

然后在terminal中输入命令
 ./gradlew --no-daemon -Dkotlin.daemon.jvm.options="-Xdebug,-Xrunjdwp:transport=dt_socket\,address=5005\,server=y\,suspend=n" :clean assemble
开始编译

gradle任务执行到:app:kaptDebugKotlin的时候点击debug按钮,然后会提示kapt debug这个configuration 成功地connected to the target VM,  之后便能走到断点处。





格外值得注意的是,调试过程中发现annotation processor的process()方法会执行多次,并且每次调用roundEnvironment.getElementsAnnotatedWith(XXX)的结果都可能不同。由于process()方法中一般要写文件,可能造成写文件的时候抛出“Attempt to recreate a file for type com.XXX.XXX.XXX”的异常,因此写文件时要try catch住异常。捕获了异常不该用mMessager.printMessage(Diagnostic.Kind.ERROR, XXX),这会终止编译进程。



评论

此博客中的热门博文

关于Google Play新打包格式aab(Android App Bundle)的调研简报

homebrew安装旧版本的formula