I'm confronting a weird issue using gradlew(4.10.2) + dagger(2.18).

问题是当我打电话时:

./gradlew :app:compileDebugAndroidTestKotlin 

The build fails with:

任务:常见:CompiledBugJavaWithJavaC失败

/CommonModule_提供SonFactory.java:6:error:package javax.注释.处理不存在

/CommonModule_ProvidesGsonFactory.java:8: error: cannot find symbol @Generated(

但如果我运行task from Android Studio,任务就会成功,dagger生成的类中就会出现@Generated is not.

Do you have some clue to avoid the @Generated annotation using ./gradlew?

推荐答案

Dagger uses auto-common's GeneratedAnnotations to figure out which @Generated annotation to use. It does this based on the classpath.

What this means is that while the annotation processor is running, javax.annotation.processing.Generated is available and used in the generator, but when the resulting file is compile in another task, it is no longer on the classpath.

潜在原因可能是代码是使用较新版本的JDK生成的,增量构建无效,需要完整的"清理和重建",或者java语言级别存在问题,其中注释处理器在java 9+上运行,但android编译为java 8或更低版本.

Using ./gradlew compileDebugAndroidTestKotlin -Dorg.gradle.java.home=<Android studio jre path> solved the problem.

Kotlin相关问答推荐

如何在Kotlin中模拟www.example.com()?

在Kotlin中将String转换为T

collectAsState 未从存储库接收更改

按钮无法在 Android Studio 上打开新活动

第二个协程永远不会执行

异常传播如何在 CoroutineScope.async 上工作?

使用 Kotlin 协程时 Room dao 类出错

Picasso 回调

如何在 kotlin @Parcelize 中使用 null

判断 AAR 元数据值时发现的一个或多个问题:

将 Gradle 子元素与 Kotlin 多平台一起使用

将 Completable 转换为 Single 的规范方法?

Foo::class.java 和 Foo::javaClass 有什么区别?

如何在 Kotlin 中创建一个打开新活动(Android Studio)的按钮?

Kotlin 具体化的泛型不会按计划保持类型

使用 Paging 3 时保存并保留 LazyColumn 滚动位置

Kotlin中的下划线名称是为什么保留的?

Kotlin 与 C# 中的标志枚举变量具有相似效果的方式是什么

在Kotlin中将列表转换为对的惯用方法

将 androidx.constraintlayout:constraintlayout lib 更新到 2.0.2 版本后出现崩溃 isRtl () null 引用