在真实设备上测试时,uses-library com.android.nfc_extras会破坏api 26及以上版本的AndroidTest

And*_*sik 7 android nfc kotlin android-testing android-studio

我突然想通了这个宣言:

    <uses-library
        android:name="com.android.nfc_extras"
        android:required="false"/>
Run Code Online (Sandbox Code Playgroud)

<application>我的内部范围AndroidManifest.xml使仪器测试失败:

java.lang.NoSuchMethodError: No static method allOf(Lorg/hamcrest/Matcher;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher; in class Lorg/hamcrest/core/AllOf; or its super classes (declaration of 'org.hamcrest.core.AllOf' appears in /system/framework/com.android.nfc_extras.jar)
at org.hamcrest.Matchers.allOf(Matchers.java:33)
at android.support.test.espresso.Espresso.<clinit>(Espresso.java:187)
at android.support.test.espresso.Espresso.onView(Espresso.java:75)
at com.example.abusik.espressotest.InstrumentedTest.changeText_sameActivity(InstrumentedTest.kt:34)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
Run Code Online (Sandbox Code Playgroud)

我现在想出的是:

  • 为您创建了一个演示存储库,以便能够快速重现问题;
  • 只有API> = 26 的真实设备才会受到影响(例如三星SM-G935F,华为P20 lite);
  • 测试通过我尝试的任何API的仿真器;
  • 测试通过API <26的实际设备;
  • 看起来缺少类(以及他所有的静态方法)都包含在AndroidTest apk中,但在运行时Android尝试从com.android.nfc_extras.jar中获取此类.
  • 此行为可能取决于手机的NFC功能;
  • 对我来说,它看起来不像MultiDex或混淆问题 - 演示存储库没有混淆和单一dex.

Apk分析截图


我想知道是否有人遇到这个问题,为什么会发生这种情况,我怎样才能保持我的AndroidTests工作并仍然使用这个库?


我的InstrumentedTest.kt档案:

package com.example.abusik.espressotest

import android.support.test.espresso.Espresso.onView
import android.support.test.espresso.action.ViewActions.*
import android.support.test.espresso.assertion.ViewAssertions.matches
import android.support.test.espresso.matcher.ViewMatchers.withId
import android.support.test.espresso.matcher.ViewMatchers.withText
import android.support.test.filters.LargeTest
import android.support.test.rule.ActivityTestRule
import android.support.test.runner.AndroidJUnit4
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
@LargeTest
class InstrumentedTest {

    private lateinit var stringToBetyped: String

    @get:Rule
    var activityRule: ActivityTestRule<MainActivity> = ActivityTestRule(MainActivity::class.java)

    @Before
    fun initValidString() {
        // Specify a valid string.
        stringToBetyped = "Espresso"
    }

    @Test
    fun changeText_sameActivity() {
        // Type text and then press the button.
        onView(withId(R.id.et)) //EXCEPTION THROWN HERE
            .perform(typeText(stringToBetyped), closeSoftKeyboard())
        onView(withId(R.id.btn)).perform(click())

        // Check that the text was changed.
        onView(withId(R.id.et))
            .check(matches(withText(stringToBetyped)))
    }
}
Run Code Online (Sandbox Code Playgroud)

测试日志:

Testing started at 0:09 ...

12/17 00:09:32: Launching InstrumentedTest
$ adb push /Users/a.busik/EspressoTest/app/build/outputs/apk/debug/app-debug.apk /data/local/tmp/com.example.abusik.espressotest
$ adb shell pm install -t -r "/data/local/tmp/com.example.abusik.espressotest"
Success
APK installed in 3 s 235 ms
$ adb push /Users/a.busik/EspressoTest/app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk /data/local/tmp/com.example.abusik.espressotest.test
$ adb shell pm install -t -r "/data/local/tmp/com.example.abusik.espressotest.test"
Success
APK installed in 2 s 684 ms
Running tests

$ adb shell am instrument -w -r   -e debug false -e class 'com.example.abusik.espressotest.InstrumentedTest' com.example.abusik.espressotest.test/android.support.test.runner.AndroidJUnitRunner
Client not ready yet..
Started running tests

java.lang.NoSuchMethodError: No static method allOf(Lorg/hamcrest/Matcher;Lorg/hamcrest/Matcher;)Lorg/hamcrest/Matcher; in class Lorg/hamcrest/core/AllOf; or its super classes (declaration of 'org.hamcrest.core.AllOf' appears in /system/framework/com.android.nfc_extras.jar)
at org.hamcrest.Matchers.allOf(Matchers.java:33)
at android.support.test.espresso.Espresso.<clinit>(Espresso.java:187)
at android.support.test.espresso.Espresso.onView(Espresso.java:75)
at com.example.abusik.espressotest.InstrumentedTest.changeText_sameActivity(InstrumentedTest.kt:34)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at android.support.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:527)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.-wrap0(Unknown Source:0)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at android.support.test.runner.AndroidJUnit4.run(AndroidJUnit4.java:101)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:126)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.-wrap0(Unknown Source:0)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:384)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2106)

Tests ran to completion.
Run Code Online (Sandbox Code Playgroud)

我能够找到一个nfc_extras.jar,它似乎不包含org.hamcrest的任何引用.*

nfc_extras.jar源代码概述

Mar*_*ler 4

无法在运行 Android 8.0.0 的物理 MotoX4 上重现。

a)com.android.nfc_extras.jar可能有org.hamcrest.Matcher测试应用程序之外的另一个版本 - 理论上,从libs目录加载它时可以排除它。模拟器不关心的情况可能来自于既不存在BT也不NFC可用。

main/Manifest.xml缺乏NFC许可;添加它可以消除投诉:

<uses-permission android:name="android.permission.NFC" />
Run Code Online (Sandbox Code Playgroud)

b)甚至可以nfc_extras.jar用以下命令完全摆脱androidTest/Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.abusik.espressotest">
    <application>
        <uses-library
            android:name="com.android.nfc_extras"
            android:required="false"
            tools:node="remove"/>
    </application>
</manifest>
Run Code Online (Sandbox Code Playgroud)