Kapt在Android Studio 3.0中无法与AutoValue一起使用。

12

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"

获得此堆栈跟踪:

: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)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.execCompiler(CompileServiceImpl.kt:387)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$execCompiler(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:355)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1$2.invoke(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2$$special$$inlined$withValidClientOrSessionProxy$lambda$1.invoke(CompileServiceImpl.kt:784)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2$$special$$inlined$withValidClientOrSessionProxy$lambda$1.invoke(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.common.DummyProfiler.withMeasure(PerfUtils.kt:137)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.checkedCompile(CompileServiceImpl.kt:811)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$checkedCompile(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2.invoke(CompileServiceImpl.kt:783)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$doCompile$2.invoke(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive(CompileServiceImpl.kt:971)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive$default(CompileServiceImpl.kt:851)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.doCompile(CompileServiceImpl.kt:777)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.access$doCompile(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1.invoke(CompileServiceImpl.kt:354)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl$compile$1.invoke(CompileServiceImpl.kt:93)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive(CompileServiceImpl.kt:971)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.ifAlive$default(CompileServiceImpl.kt:851)
    at org.jetbrains.kotlin.daemon.CompileServiceImpl.compile(CompileServiceImpl.kt:327)
    at sun.reflect.GeneratedMethodAccessor168.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:346)
    at sun.rmi.transport.Transport$1.run(Transport.java:200)
    at sun.rmi.transport.Transport$1.run(Transport.java:197)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
    at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:568)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:826)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:683)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:682)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我认为 Kotlin 完全不需要 AutoValue,因为它有数据类 https://kotlinlang.org/docs/reference/data-classes.html - Anton Holovin
尝试使用kapt代替annotationProcessor。 - quiro
2
@AntonGolovin 这是一个遗留项目。 - ar-g
@quiro 这将会产生相同的输出。 - ar-g
请将新的错误提交到我们的问题跟踪器 http://kotl.in/issue。同时,提供样例项目将不胜感激。 - yanex
我记得有一个问题。解决方法是一次性将所有自动值类转换为Kotlin数据类。 - tynn
4个回答

14

@Geng Jiawen的回答给出了正确的方向,我把所有东西都改成了kapt

provided "com.google.auto.value:auto-value:1.4.1"
kapt "com.google.auto.value:auto-value:1.4.1"
kaptTest "com.google.auto.value:auto-value:1.4.1"
compile 'com.google.dagger:dagger:2.9'
kapt 'com.google.dagger:dagger-compiler:2.9'
kaptTest 'com.google.dagger:dagger-compiler:2.9'

但后来我遇到了问题

错误:执行 ':presentation:javaPreCompileProdDebug' 任务失败。

现在必须显式声明注释处理器。 在编译类路径上找到以下依赖项包含注释处理器。 请将它们添加到 annotationProcessor 配置中。

- auto-value-1.4.1.jar
- auto-value-gson-0.4.6.jar
- auto-service-1.0-rc2.jar   

或者,设置

android.defaultConfig.javaCompileOptions.annotationProcessorOptions .includeCompileClasspath = true

以继续之前的行为。请注意,此选项已弃用,并将在将来删除。有关更多详细信息,请参见https://developer.android.com/r/tools/annotation-processor-error-message.html

我添加了

defaultConfig {
    javaCompileOptions {
      annotationProcessorOptions {
        includeCompileClasspath false
      }
    }
  }

只有这样它才能正常工作。

我想提一下,使用kapt会增加8-10%的构建时间


这对我有用,但我担心被弃用的警告。我会不会在未来自讨苦吃? - AutonomousApps
使用kapt会增加8-10%的构建时间。有证据吗? - Alexey Nikitin
由于“provided”现已被弃用,因此我使用kapt和annotationProcessor配对使用,如下所示: annotationProcessor 'com.google.auto.value:auto-value-annotations:1.7' kapt 'com.google.auto.value:auto-value-annotations:1.7' - Aegir

4

我也遇到了这个问题。解决方法很简单,只需要将AutoValue注释处理器更改为kapt。从我的观察中看来,kapt会生成java文件而不是kotlin文件。因此原始的注释处理器是不必要的。另外,在更改后记得清理你的项目。

compile 'com.google.dagger:dagger:2.9'
kapt 'com.google.dagger:dagger-compiler:2.9'

provided "com.google.auto.value:auto-value:1.4.1"
kapt "com.google.auto.value:auto-value:1.4.1"

将目前的项目迁移到Kotlin并不容易,但让我们继续努力:)


0
defaultConfig {
javaCompileOptions {
  annotationProcessorOptions {
    includeCompileClasspath true
  }
}

}
在模块构建 gradle 中


0

我刚刚通过从我的依赖项中删除provided 'org.glassfish:javax.annotation:10.0-b28'来解决了同样的问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接