Android Espresso 测试:非法 class 访问:'androidx.test.core.app.ListFuture' 试图访问 'androidx.concurrent.futures.DirectExecutor'

Android Espresso Test: Illegal class access: 'androidx.test.core.app.ListFuture' attempting to access 'androidx.concurrent.futures.DirectExecutor'

我有以下 UI 测试文件和我正在制作的应用程序的扩展文件:

package com.realtomjoney.pyxlmoose.activities.`2_canvas`

import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.*
import androidx.test.ext.junit.rules.ActivityScenarioRule
import androidx.test.filters.LargeTest
import com.realtomjoney.pyxlmoose.R
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
import com.realtomjoney.pyxlmoose.AndroidTestUtilityFunctions
import com.realtomjoney.pyxlmoose.activities.canvas.CanvasActivity

@LargeTest
@RunWith(AndroidJUnit4ClassRunner::class)
class CanvasActivityTestsOnFindAndReplaceButtonTap {
    @get:Rule
    var activityTestRule = ActivityScenarioRule(CanvasActivity::class.java)

    @Test fun uitest_doneButton_isNotDisplayed_after_findAndReplaceButton_isPressed() {
        AndroidTestUtilityFunctions.goToFindAndReplaceFragment()
        onView(withId(R.id.activityCanvas_doneButton)).check(matches(withEffectiveVisibility(Visibility.GONE)))
    }

    @Test fun uitest_colorPickerRecyclerView_isNotDisplayed_after_findAndReplaceButton_isPressed() {
        AndroidTestUtilityFunctions.goToFindAndReplaceFragment()
        onView(withId(R.id.activityCanvas_colorPickerRecyclerView)).check(matches(withEffectiveVisibility(Visibility.GONE)))
    }

    @Test fun uitest_titleTextView_isNotDisplayed_after_findAndReplaceButton_isPressed() {
        AndroidTestUtilityFunctions.goToFindAndReplaceFragment()
        onView(withId(R.id.activityCanvas_canvasTitleEditText)).check(matches(withEffectiveVisibility(Visibility.GONE)))
    }

    @Test fun uitest_colorSwapButton_isNotDisplayed_after_findAndReplaceButton_isPressed() {
        AndroidTestUtilityFunctions.goToFindAndReplaceFragment()
        onView(withId(R.id.activityCanvas_colorSwapButton)).check(matches(withEffectiveVisibility(Visibility.GONE)))
    }

    @Test fun uitest_colorPrimarySelected_isNotDisplayed_after_findAndReplaceButton_isPressed() {
        AndroidTestUtilityFunctions.goToFindAndReplaceFragment()
        onView(withId(R.id.activityCanvas_colorPrimaryView)).check(matches(withEffectiveVisibility(Visibility.GONE)))
    }

    @Test fun uitest_colorSecondarySelected_isNotDisplayed_after_findAndReplaceButton_isPressed() {
        AndroidTestUtilityFunctions.goToFindAndReplaceFragment()
        onView(withId(R.id.activityCanvas_colorSecondaryView)).check(matches(withEffectiveVisibility(Visibility.GONE)))
    }
}
object AndroidTestUtilityFunctions {
    private const val defaultProjectName = "Unnamed Project"
    private const val defaultProjectSpanCount = 5

    fun goToFindAndReplaceFragment() {
        onView(withId(R.id.activityCanvas_canvasToolsScrollView)).perform(swipeLeft())
        onView(withId(R.id.activityCanvas_findAndReplaceButton)).perform(click())
    }

    fun createNewProject(projectName: String = defaultProjectName, spanCount: Int = defaultProjectSpanCount) {
        onView(withId(R.id.activityMain_newProjectButton)).perform(click())
        onView(withId(R.id.fragmentNewCanvas_projectTitleTextInputEditText)).perform(replaceText(projectName))
        onView(withId(R.id.fragmentNewCanvas_spanCountTextInputEditText)).perform(replaceText(spanCount.toString()))
        onView(withId(R.id.fragmentNewCanvas_doneButton)).perform(click())
    }
}

出于某种原因,当我 运行 进行 UI 测试时,出现了以下恼人的异常:

java.lang.IllegalAccessError: Illegal class access: 'androidx.test.core.app.ListFuture' attempting to access 'androidx.concurrent.futures.DirectExecutor' (declaration of 'androidx.test.core.app.ListFuture' appears in /data/app/~~xOjrSE9BCeINue7sOBFVJw==/com.realtomjoney.pyxlmoose.test-VqvV0FRJD_fIuyk2Jiuyyw==/base.apk)
    at androidx.test.core.app.ListFuture.directExecutor(ListFuture.java:139)
    at androidx.test.core.app.ListFuture.init(ListFuture.java:101)
    at androidx.test.core.app.ListFuture.<init>(ListFuture.java:84)
    at androidx.test.core.app.DeviceCapture$forceRedrawGlobalWindowViews.run(DeviceCapture.kt:151)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7842)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)

我不知道为什么会这样,我已经尝试了几个星期来解决它但没有运气 - 每当我尝试执行我的 UI 测试时我仍然遇到同样的异常.这非常糟糕,因为我实际上定期提交错误代码!我真的需要解决这个问题。

我也有同样的问题。我花了很多时间研究它,最终发现你的问题源于此:https://issuetracker.google.com/issues/204506297

从本质上讲,该版本的 Espresso 库会尝试使用它没有范围访问权限的另一个库中的枚举。他们已经解决了这里的问题:https://github.com/android/android-test/pull/1174

不幸的是,在撰写本文时,3.5.0-alpha03 是最新版本。因此,您可以等待 alpha04/beta01 或降级回 3.4.0,直到问题得到解决。