android / android-test

An extensive framework for testing Android apps
https://android.github.io/android-test
Apache License 2.0
1.16k stars 314 forks source link

androidx.test:core-ktx doesn't work on a Java 6 project #292

Open ianhanniballake opened 5 years ago

ianhanniballake commented 5 years ago

Description

When using a project that uses Kotlin but uses Java 1.6 - my actual project is a library project that is not ready to force downstream apps to compile with Java 1.7+,

I cannot use ActivityScenario.launchActivity() since it appears the core-ktx module is compiled with Java 1.7 bytecode.

Steps to Reproduce

  1. Create a new project in Android Studio
  2. Add a dependency on androidTestImplementation 'androidx.test:core-ktx:1.1.0' (or any later version)
  3. Write a test that uses ActivityScenario.launchActivity:
    @Test
    fun withLaunchActivity() {
        // This line fails to compile
        with(launchActivity<MainActivity>()) {
            onActivity {
                assertTrue(it.lifecycle == Lifecycle.State.RESUMED)
            }
        }
    }

Expected Results

core-ktx is usable on projects that compile against Java 1.6.

Actual Results

Fails with an error:

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

AndroidX Test and Android OS Versions

'androidx.test:core-ktx:1.1.0' (or any later version)

Link to a public git repo demonstrating the problem:

https://github.com/ianhanniballake/TestCoreKtxIssue

marcinbak commented 5 years ago

Just to add another perspective: I've encountered same issue. Bumped up jvm target to 1.8, but Kotlin compiler uses Java 8 methods not available on Android below API 24 which results in runtime crashes (like Long.hashCode(), source).

I think API pre 24 has still significant market share and for the compatibility sake ktx libraries should target jvm 1.6.

ianhanniballake commented 5 years ago

@marcinbak - there's a separate issue tracking desugaring the Java 8 methods for older API levels, but I agree that using Kotlin shouldn't force targeting a newer version of the JVVM.

matpag commented 5 years ago

@ianhanniballake Can we use

kotlinOptions {
    jvmTarget = '1.8'
}

now that desugaring as been fixed in AGP 3.4.1?

Otherwise there is no way to use core-ktx in my current app without cutting off support for Android < N