带有Kotlin和构建工具2.3.0的Android注释 [英] Android Annotations with Kotlin and build tools 2.3.0

查看:90
本文介绍了带有Kotlin和构建工具2.3.0的Android注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要在2.3.0之前的版本中使用Android注解,请执行以下操作:

To use Android Annotations prior to 2.3.0 a person would do this:

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

要使用Kotlin,您应该使用kapt而不是apt(链接).

to use Kotlin you would use kapt instead of apt (link).

从2.3.0版本开始,一个人需要使用注解处理器而不是apt:

Since 2.3.0 a person needs to use the annotationProcessor instead of apt:

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

有人知道为了将Kotlin与注解处理器一起使用需要改变什么吗?

Does anyone know what needs to change in order to use Kotlin with the annotationProcessor?

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

I currently have a very simple, main activity that I set up to use @EActivity to grab the layout. I declared the generated file in the manifest, .MainActivity_.

在Java中,这可以正常工作.在科特林:

In java, this works fine. In 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:在路径:DexPathList [[zip file"/data/app/[...]-1/base.apk"],nativeLibraryDirectories=[/data/app/[...]-1/lib/x86、/vendor/lib、/system/lib]]

Process: [...], PID: 10018 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{[...].MainActivity_}: java.lang.ClassNotFoundException: Didn't find class "[...].MainActivity_" on path: DexPathList[[zip file "/data/app/[...]-1/base.apk"],nativeLibraryDirectories=[/data/app/[...]-1/lib/x86, /vendor/lib, /system/lib]]

****更新****

**** UPDATE ****

因此,我清理并重建了该项目.注释似乎没有为MainActivity生成下划线文件.是有道理的,但我不知道为什么或如何解决.

So I cleaned and rebuilt the project. It seems annotations isn't generating the underscore file for MainActivity. Makes sense but I don't know why or how to fix it.

推荐答案

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

You must use kapt (and apply plugin: 'kotlin-kapt') instead annotationProcessor if you want to use Kotlin code for annotation processing (you can continue use annotationProcessor only if you don't generate any code from Kotlin sources, like in your example, where you have MainActivity written in Kotlin)

这篇关于带有Kotlin和构建工具2.3.0的Android注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆