我正在研究相当大的多模块Android项目,该项目使用Data Binding,Dagger 2和Java与Kotlin混合使用.
在项目"代码"文件(.java和.kt文件包括活动,自定义类等)稍有变化之后,构建时间很长(最多2分钟).即使我在添加新行等更改或修改注释中的一个字符时,也会发生这种情况.
我确实使用--info参数运行了Gradle脚本,得到了以下提示,它在以下任务中"挂起":
构建任务'的缓存键:AppName:kaptDebugKotlin'是1a3a53e5f9b0934ab50a25c0133055f2最新检查任务':AppName:kaptDebugKotlin'耗时0.0秒.它不是最新的,因为:输入属性'source'文件/Users/username/Android/project-directory/AppName/build/generated/source/dataBinding/debug/android/databinding/layouts/DataBindingInfo.java已更改.输入属性'source'文件/Users/username/Android/project-directory/AppName/src/main/java/com/package/to/my/activity/SomeActivity.java已更改.
DataBindingInfo.java是生成的文件,其中只包含buildId:
package android.databinding.layouts;
import android.databinding.BindingBuildInfo;
@BindingBuildInfo(buildId="23567c57-d3c8-4999-bc79-6211351c7d89")
public class DataBindingInfo {}
Run Code Online (Sandbox Code Playgroud)
每次代码中的任何更改时,buildId哈希都会更改.
该项目使用了Crashlytics,但我为调试版本禁用了它.
可能是这种行为的原因是什么?
编辑:即使Android Studio关闭,我正在项目中重新生成buildId,我正在外部编辑器中进行更改并从命令行运行构建.
android android-gradle-plugin android-databinding kapt android-gradle-3.0
我用kapt编写了一个代码生成器,并在一个用maven编译kotlin的项目中使用它.
我发现在Kotlin的编译阶段之后调用了kapt生成器,这阻止了我在同一个项目中使用kotlin中生成的代码.
但是,如果我在同一个项目中从Java中引用生成的类,它可以正常工作.这是因为java编译阶段是在kotlin的生成阶段之后.
我已经在maven配置中的Kotlin编译目标之前指定了kapt目标(如文档中所述)但它似乎没有什么区别:
<plugin>
<artifactId>kotlin-maven-plugin</artifactId>
<groupId>org.jetbrains.kotlin</groupId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>kapt</id>
<goals>
<goal>kapt</goal>
</goals>
<configuration>
<sourceDirs>
<sourceDir>src/main/java</sourceDir>
</sourceDirs>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>lang.taxi</groupId>
<artifactId>taxi-annotation-processor</artifactId>
<version>${taxi.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</execution>
<execution>
<id>compile</id>
<goals> <goal>compile</goal> </goals>
</execution>
<execution>
<id>test-compile</id>
<goals> <goal>test-compile</goal> </goals>
<configuration>
<sourceDirs>
<sourceDir>${project.basedir}/src/test/kotlin</sourceDir>
<sourceDir>${project.basedir}/src/test/java</sourceDir>
</sourceDirs>
</configuration>
</execution>
</executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)
是否可以配置Kotlin以允许我在同一个项目中使用Kotlin生成的代码?
Gradle依赖项:
compile 'com.google.dagger:dagger:2.9'
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
kapt 'com.google.dagger:dagger-compiler:2.9'
provided "com.google.auto.value:auto-value:1.4.1"
annotationProcessor "com.google.auto.value:auto-value:1.4.1"
Run Code Online (Sandbox Code Playgroud)
得到这个堆栈跟踪:
:presentation:kaptDevDebugKotlin
e: @AutoValue public abstract class Categories implements CategoriesModel{
e: ^
e: symbol: class CategoriesModel
e: path/Categories.java:13: error: cannot find symbol
w: warning: The following options were not recognized by any processor: '[kapt.kotlin.generated]'
w:
e: org.jetbrains.kotlin.kapt3.diagnostic.KaptError: Error while annotation processing
at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing(annotationProcessing.kt:90)
at org.jetbrains.kotlin.kapt3.AnnotationProcessingKt.doAnnotationProcessing$default(annotationProcessing.kt:42)
at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:149)
at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:76)
at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM$analyzeFilesWithJavaIntegration$2.invoke(TopDownAnalyzerFacadeForJVM.kt:89)
at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration(TopDownAnalyzerFacadeForJVM.kt:99)
at org.jetbrains.kotlin.resolve.jvm.TopDownAnalyzerFacadeForJVM.analyzeFilesWithJavaIntegration$default(TopDownAnalyzerFacadeForJVM.kt:76)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler$analyze$1.analyze(KotlinToJVMBytecodeCompiler.kt:347)
at org.jetbrains.kotlin.cli.common.messages.AnalyzerWithCompilerReport.analyzeAndReport(AnalyzerWithCompilerReport.kt:104)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.analyze(KotlinToJVMBytecodeCompiler.kt:336)
at org.jetbrains.kotlin.cli.jvm.compiler.KotlinToJVMBytecodeCompiler.compileModules(KotlinToJVMBytecodeCompiler.kt:131)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:165)
at org.jetbrains.kotlin.cli.jvm.K2JVMCompiler.doExecute(K2JVMCompiler.kt:54)
at org.jetbrains.kotlin.cli.common.CLICompiler.exec(CLICompiler.java:178) …
Run Code Online (Sandbox Code Playgroud) 我一直在 Gradle 中安装依赖项,但我不明白为什么有时我需要将 kapt 用于生命周期和房间数据库等库以使用@Something
注释。但是在像 Retrofit2 和 Gson 这样的一些库中,我不需要使用 kapt,我可以使用诸如@SerializedName
?
我正在使用 Kotlin、Databinding 和 Room 开发一个 android 项目。有时构建会失败并显示错误消息,其中不包含有关究竟出了什么问题的信息,除了它与注释处理器有关(这可能有很多原因......)。
缩短示例:
org.gradle.workers.internal.DefaultWorkerExecutor$WorkExecutionException: 执行 org.jetbrains 时发生故障
[更多堆栈跟踪行]
引起:org.jetbrains.kotlin.kapt3.base.util.KaptBaseError: Error while annotation processing
[更多堆栈跟踪行]
在 org.jetbrains.kotlin.kapt3.base.Kapt.kapt(Kapt.kt:45)
... 32 更多
找到原因,然后就意味着耗时的回溯我的步骤(可能使用git stash
)和猜测,当最后的 32 条隐藏线似乎可能包含一些关于实际出错的有用信息时。
所以问题是:如何显示完整的堆栈跟踪?
我尝试-Xmaxerrs 500
在我build.gradle
这里设置https://kotlinlang.org/docs/reference/kapt.html#java-compiler-options以及它的各种变体,我在 SE 上发现(对不起,不记得哪个确切地)。没有任何区别。也许我把块放在错误的部分?(尝试模块级别,android -> defaultConfig -> kapt)
首先,
我非常清楚这里已经发布了很多关于此错误的问题,但似乎没有一个有适当的解决方案,尤其是我需要的解决方案。
我被以下错误困扰了一个多星期。
我正在开发一个使用 Kotlin、MVVM、Clean Arch 和导航组件构建的 android 项目。
我最近添加了领域数据库,为此我不得不添加以下插件。
apply plugin: 'kotlin-kapt'
apply plugin: 'realm-android'
Run Code Online (Sandbox Code Playgroud)
我认为真正的问题是从这里开始的。
(之后我添加了一个 DatabaseManager 类,它使用了我编写的一些领域扩展函数来进行数据库操作。)
在此之后编译项目时,整体出现以下错误。
A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
Run Code Online (Sandbox Code Playgroud)
连同以下
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)
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan …
Run Code Online (Sandbox Code Playgroud) 我在 Android Studio 中以及在./gradlew assembleDebug
带有 Apple M1 Max 芯片的新 Macbook 上的命令行上运行时都遇到此故障。在我的旧 Intel Mac 上构建项目没有问题。
> Task :app:kaptGenerateStubsDebugKotlin FAILED
e: java.lang.IllegalAccessError: class org.jetbrains.kotlin.kapt3.base.KaptContext (in unnamed module @0x4d1ecff7) cannot access class com.sun.tools.javac.util.Context (in module jdk.compiler) because module jdk.compiler does not export com.sun.tools.javac.util to unnamed module @0x4d1ecff7
at org.jetbrains.kotlin.kapt3.base.KaptContext.<init>(KaptContext.kt:28)
at org.jetbrains.kotlin.kapt3.KaptContextForStubGeneration.<init>(KaptContextForStubGeneration.kt:40)
at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.contextForStubGeneration(Kapt3Extension.kt:287)
at org.jetbrains.kotlin.kapt3.AbstractKapt3Extension.analysisCompleted(Kapt3Extension.kt:171)
at org.jetbrains.kotlin.kapt3.ClasspathBasedKapt3Extension.analysisCompleted(Kapt3Extension.kt:102)
Run Code Online (Sandbox Code Playgroud)
类 org.jetbrains.kotlin.kapt3.base.KaptContext (在未命名模块 @0x4d1ecff7 中)无法访问类 com.sun.tools.javac.util.Context (在模块 jdk.compiler 中),因为模块 jdk.compiler 不导出 com. sun.tools.javac.util 到未命名模块@0x4d1ecff7
谷歌搜索了一下,错误消息看起来很像这里的消息:“ Kapt 与 JDK 16+ 不兼容”。但事实是,我使用的是 …
构建后我收到此警告:
“kapt.use.worker.api”已弃用,并计划在 Kotlin 1.8 版本中删除。
知道我的 gradle.properties 中有:
kapt.use.worker.api=true
Run Code Online (Sandbox Code Playgroud)
在我的模块级别 build.gradle 中:
apply plugin: 'kotlin-kapt'
Run Code Online (Sandbox Code Playgroud)
那么,除了已弃用的线路之外,还有什么替代方案呢?
将 android studio 从 electricEel 更新为 Flamingo 插件后id 'kotlin-kapt'
导致错误
Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.
> 'compileDebugJavaWithJavac' task (current target is 1.8) and 'kaptGenerateStubsDebugKotlin' task (current target is 17) jvm target compatibility should be set to the same Java version.
Consider using JVM toolchain: https://kotl.in/gradle/jvm/toolchain
* Try:
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
* Exception is:
org.gradle.api.tasks.TaskExecutionException: Execution failed for task ':app:kaptGenerateStubsDebugKotlin'.
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.lambda$executeIfValid$1(ExecuteActionsTaskExecuter.java:149)
at …
Run Code Online (Sandbox Code Playgroud) 我正在为Kotlin开发注释处理器,因为处理过的元素是用Java编写的,我没有收到?
带有@Nullable
注释的nullables ,这很好,但是我遇到了在类型和高阶函数中接收空参数的问题,对于正常参数.
var someNullField: String? = ""
Run Code Online (Sandbox Code Playgroud)
我将在其注释java.lang.String
过程@org.jetbrains.annotations.Nullable
中收到.
但是List<String?>
例如将返回我java.util.List<java.lang.String>
没有任何注释不在主元素中而不在类型参数中导致未知的可空性状态
我尝试使用javax.lang.model.util.Types
找到某种结果,但没有.
我现在使用的一些代码:
val utils = processingEnvironment.typeUtils
val type = fieldElement.asType()
if (type is DeclaredType) {
val typeElement = utils.asElement(type)
type.typeArguments
.forEach {
//Trying different ways and just printing for possible results
val capture = utils.capture(it)
val erasure = utils.erasure(it)
val element = utils.asElement(it)
printMessage("element: $element isNullable: ${element.isNullable()} isNotNull: ${element.isNotNull()}\ncapture: $capture isNullable: ${capture.isNullable()} isNotNull: ${capture.isNotNull()}\nerasure: $erasure isNullable: ${erasure.isNullable()} …
Run Code Online (Sandbox Code Playgroud)