Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Use generated classes in Kotlin tests #2082

Description

@nenick

AndroidAnnotations version: 4.4.0

Android compile SDK version: 25

Annotated code: Empty Activity with @EActivity annotation

Actual generated code: Looks correct, no issues here.

We want to use AndroidAnnotation generated classes in tests (unit/instrumentation) but since Kotlin 1.1.3 this does not work anymore. Tests implemented in Java instead of Kotlin work fine however.

Easiest way to reproduce (unit test):

  • Kotlin app project with AndroidAnnotations
  • enhance an activity with annotation @EActivity
  • write Kotlin unit test which creates an AndroidAnnotation generated activity instance (ExampleActivity_) and call setContentView(null)
    • expected is a stacktrace which contains information about AndroidAnnotation generated activity class
    • result is stacktrace with direct call to base class (skipping the generated one)

Other way to reproduce (instrumentation test):

This issue occurs also within Espresso tests. Espresso test references generated class, but it uses the base activity during runtime.

@Rule
@JvmField
val mActivityRule = ActivityTestRule(MyActivity_::class.java)
java.lang.RuntimeException: Could not launch activity
at android.support.test.runner.MonitoringInstrumentation.startActivitySync(MonitoringInstrumentation.java:430)
at android.support.test.rule.ActivityTestRule.launchActivity(ActivityTestRule.java:332)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:431)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
...
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:58)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:375)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1932)
Caused by: java.lang.RuntimeException: Unable to resolve activity for: Intent { act=android.intent.action.MAIN flg=0x10000000 cmp=com.company.myapp/.ui.MyActivity }
at android.app.Instrumentation.startActivitySync(Instrumentation.java:396)
at android.support.test.runner.MonitoringInstrumentation.access$101(MonitoringInstrumentation.java:96)
at android.support.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:410)
at android.support.test.runner.MonitoringInstrumentation$4.call(MonitoringInstrumentation.java:407)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
...

Already tried:

  • use different Kotlin versions
    • with all Kotlin 1.1.2-* versions its works fine, with 1.1.3 this issue occurs
    • with Kotlin 1.1.51 the issue still exists
  • tools like dbflow looks working (possible not comparable)

Activity

  1. WonderCsabo commented on Nov 18, 2017

    @WonderCsabo
    Member

    I am afraid this is out of of the processor's reach, we cannot really do anything about this. But you can share the example project so we can take a look.

  2. nenick commented on Nov 19, 2017

    @nenick
    Author

    Here is an example project which shows the issue https://github.com/nenick/android-kotlin-androidannotations

    There are also other libraries included where I can access the generated classes in Kotlin based tests without any issue. Also the Java based tests work without any issue.

    Really strange behaviour I hope you have an idea.

  3. WonderCsabo commented on Nov 29, 2017

    @WonderCsabo
    Member

    @nenick i checked your project, it is really strange... For some reason MainActivity_::class returns the Kotlin class for MainActivity. But this is only happening in the instrumented test, not in the app. It is also happening in the unit test. You should report this error in the upstream project (i am not sure which), because we cannot do anything from AA-side. :(

  4. nenick commented on Dec 9, 2017

    @nenick
    Author

    @WonderCsabo do you know why all classes generated by AndroidAnnotations have the @Metadata(...) annotation? When i remove this annotation from generated classes then tests work fine.

    This line effects which class is loaded
    "Lde/nenick/example/MainActivity;",
    tests load the correct class when it gets changed to
    "Lde/nenick/example/MainActivity_;",

    Edit: @Metadata(...) is only added when enhanced class is written in kotlin. What works is to write an abstract enhanced kotlin class and extend it by an enhanced java class. But this results in uggly boilerplate code.

  5. nenick commented on Jan 22, 2018

    @nenick
    Author

    I found temporal acceptable workaround

    afterEvaluate {
        kaptDebugKotlin.doLast {
            ant.replaceregexp(match:'(}, d2 = \\{\\n    "L)(.*);",', replace:'\\1\\2_;",') {
                fileset(dir: 'build/generated/source/kapt/debug')
            }
        }
    }
    
  6. saschak42 commented on Mar 6, 2018

    @saschak42

    Same issue here. @nenick your workaround is working, thank you! When will there be an official fix for that?

  7. adammach commented on Aug 10, 2018

    @adammach

    Where in structure of gradle should I put this "fix"? Thanks.

  8. saschak42 commented on Aug 10, 2018

    @saschak42

    You can put it simply at the end of the build.gradle file.

  9. adammach commented on Aug 10, 2018

    @adammach

    Ok. kapt[buildVariant]Kotlin fixed the problem.

  10. dodgex commented on Oct 14, 2018

    @dodgex
    Member

    I have opened a ticket in kotlin issue tracker: https://youtrack.jetbrains.com/issue/KT-27571

    please vote for it and hope they'll fix this soon :)

  11. dodgex commented on Oct 18, 2018

    @dodgex
    Member

    I just got feedback and it seems that after all this is an issue within android annotations as we copy annotations from the enhanced class and those include the kotlin.Metadata.

    I'll prepare a PR to fix this.

  12. WonderCsabo commented on Oct 20, 2018

    @WonderCsabo
    Member

    Finally fixed, thanks @dodgex!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions