mockito / mockito-kotlin

Using Mockito with Kotlin
MIT License
3.09k stars 198 forks source link

Version 5.0.0 + Android fails with: Cannot inline bytecode built with JVM target 11 #488

Open bvschaik opened 11 months ago

bvschaik commented 11 months ago

Android projects still use JVM target 1.8 due to device constraints; the gradle file therefore contains:

android {
    ...
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}

Upgrading mockito-kotlin from 4.1.0 to 5.0.0 causes the following compile error on any mock/whenever/etc call:

Cannot inline bytecode built with JVM target 11 into bytecode that is being built with JVM target 1.8. Please specify proper '-jvm-target' option

JVM target 11 is nowhere to be found in the Gradle files.

Steps to reproduce:

  1. Clone minimal example project: https://github.com/bvschaik/mockito-kotlin-error
  2. Try to run unit tests: ./gradlew testDebugUnitTest
  3. Note compile errors

If you downgrade mockito-kotlin to 4.1.0, the project compiles and runs successfully.

If mockito-kotlin no longer supports JVM 1.8, that should've been noted in the changelog.

argenkiwi commented 9 months ago

Does this mean we are stuck with v4.x for Android development for the foreseeable future?

TimvdLippe commented 9 months ago

The drop of compatibility is listed on mockito-core in its release notes combined with the rationale: https://github.com/mockito/mockito/releases/tag/v5.0.0

If you still need to support Java 8, you can keep on using Mockito 4. Nothing should break there.

naitaku commented 9 months ago

I was unaware that Mockito 5 no longer offers support for Java 8. Does this imply that for Android projects, the jvmTarget should now be set to VERSION_11? It seems to be supported with Android Gradle Plugin 7.0. https://developer.android.com/build/releases/past-releases/agp-7-0-0-release-notes#java-11

If that's the case, would it be advisable to include a note in the Readme.md specifically for Android developers regarding this change?

TimvdLippe commented 9 months ago

Yes that is indeed now required. Feel free to send me a PR with a README update on the version similar to what we have in the README of mockito-core.

wiryadev commented 9 months ago

i have set jvmtarget and compatibility in compileoptions to Java 11 but the problem still exists

naitaku commented 9 months ago

@wiryadev, would it be possible for you to share an example project that demonstrates the issue you're encountering?

The project linked at the beginning of this issue, https://github.com/bvschaik/mockito-kotlin-error, was resolved by adjusting the compile options to use JVM 11.

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }
wiryadev commented 9 months ago

@wiryadev, would it be possible for you to share an example project that demonstrates the issue you're encountering?

The project linked at the beginning of this issue, https://github.com/bvschaik/mockito-kotlin-error, was resolved by adjusting the compile options to use JVM 11.

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_11
        targetCompatibility JavaVersion.VERSION_11
    }
    kotlinOptions {
        jvmTarget = '11'
    }

unfortunately it is not possible currently to create sample project for that nor that it is possible to share my office work project. I choose to just downgrade to 4.1.0. My current assumption is this happens because of some dependency that i use is compiled against Java 8.