【问题标题】:Android Annotations with Kotlin and build tools 2.3.0Android Annotations with Kotlin and build tools 2.3.0
【发布时间】:2017-10-25 11:42:14
【问题描述】:

要使用 2.3.0 之前的 Android 注释,人们会这样做:

dependencies {
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

要使用 Kotlin,您可以使用 kapt 而不是 apt (link)。

从 2.3.0 开始,人们需要使用 annotationProcessor 而不是 apt:

annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"

有谁知道要将 Kotlin 与 annotationProcessor 一起使用需要进行哪些更改?

我目前有一个非常简单的主要活动,我设置它使用@EActivity 来抓取布局。我在清单中声明了生成的文件.MainActivity_

在 java 中,这很好用。在 Kotlin 中:

@EActivity(R.layout.activity_main)
open class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
   }
}

我得到一个运行时错误:

进程:[...],PID:10018 java.lang.RuntimeException:无法实例化活动 ComponentInfo{[...].MainActivity_}:java.lang.ClassNotFoundException:在路径上找不到类“[...].MainActivity_”:DexPathList [[zip 文件“ /data/app/[...]-1/base.apk"],nativeLibraryDirectories=[/data/app/[...]-1/lib/x86, /vendor/lib, /system/lib]]

**** 更新 ****

所以我清理并重建了项目。似乎注释没有为 MainActivity 生成下划线文件。有道理,但我不知道为什么或如何解决它。

【问题讨论】:

    标签: android kotlin android-annotations


    【解决方案1】:

    如果您想使用 Kotlin 代码进行注释处理,则必须使用 kapt(和 apply plugin: 'kotlin-kapt')而不是 annotationProcessor(只有在不从 Kotlin 源代码生成任何代码时才能继续使用 annotationProcessor,例如在你的例子,你有用 Kotlin 编写的 MainActivity)

    【讨论】:

    • 你能指出我(或在你的答案中提供)究竟如何设置我的 gradle 文件来做到这一点吗?我试过apply plugin: 'kotlin-kapt',它说找不到插件?谢谢!
    • 你确定你的 gradle buildscript 中有 kotlin gradle 插件吗?你的buildscript.dependendencies 块中应该有classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$KOTLIN_VERSION"。有关 kapt 的更多信息 - kotlinlang.org/docs/reference/kapt.html 另外,请确保您已将 kotlin-androidkotlin-kapt 插件应用到您的应用程序模块(默认为 app 模块)
    • 感谢您的帮助!我已经走得更远了,我可以使用插件构建所有内容,但我需要告诉 AA 清单在哪里。我正在使用kapt { arguments { androidManifestFile variant.outputs[0].processResources.manifestFile } },这是我与apt 一起使用的(我刚刚更改为kapt)。但这不起作用。错误Error:(48, 0) No such property: processResources for class: com.android.build.gradle.internal.variant.ApkVariantOutputData <a href="openFile:/[...]/app/build.gradle">Open File</a>
    • 为什么要将androidManifestFile传递给kapt?你想达到什么目标?我认为当你配置 kapt 时变体还没有准备好,所以这就是它不起作用的原因
    • bitbucket.org/hvisser/android-apt - "AndroidAnnotations 需要知道你的 AndroidManifest.xml 在哪里。通过变体检索它允许为每种风格使用不同的 AndroidManfest.xml 文件。"
    猜你喜欢
    • 2022-12-02
    • 1970-01-01
    • 1970-01-01
    • 2020-05-25
    • 2021-07-30
    • 2022-12-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多