Android Annotations with Kotlin and build tools 2.

2019-09-16 03:50发布

问题:

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"
}

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

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"

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

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_.

In java, this works fine. In Kotlin:

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

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

I get a runtime error:

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 ****

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.

回答1:

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)