在运行Espresso测试时,Android Studio中找不到属性android:forceQueryable。

13

我使用Android Studio中的Record Espresso Test选项在Run菜单下录制了我的Android应用程序Espresso测试。在录制结束时,我使用自己的文件名保存了测试。

单击保存按钮后,IDE会自动将文件创建在app模块的AndroidTest目录中。我右键保存的文件并点击运行,然后它提示我以下错误:

/Users/dehanwijesekara/Documents/ProjectName/app/build/intermediates/packaged_manifests/debugAndroidTest/AndroidManifest.xml:24: AAPT: error: attribute android:forceQueryable not found.

以下是上述链接中的文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dehan.pizzago.test" >

<uses-sdk
    android:minSdkVersion="23"
    android:targetSdkVersion="29" />

<instrumentation
    android:name="androidx.test.runner.AndroidJUnitRunner"
    android:functionalTest="false"
    android:handleProfiling="false"
    android:label="Tests for com.dehan.pizzago"
    android:targetPackage="com.dehan.pizzago" />

<queries>
    <package android:name="androidx.test.orchestrator" />
    <package android:name="androidx.test.services" />
    <package android:name="com.google.android.apps.common.testing.services" />
</queries>

<uses-permission android:name="android.permission.REORDER_TASKS" />

<application
    android:debuggable="true"
    android:extractNativeLibs="false"
    android:forceQueryable="true" >
    <uses-library android:name="android.test.runner" />

    <activity
        android:name="androidx.test.core.app.InstrumentationActivityInvoker$BootstrapActivity"
        android:theme="@android:style/Theme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
    <activity
        android:name="androidx.test.core.app.InstrumentationActivityInvoker$EmptyActivity"
        android:theme="@android:style/Theme" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
    <activity
        android:name="androidx.test.core.app.InstrumentationActivityInvoker$EmptyFloatingActivity"
        android:theme="@android:style/Theme.Dialog" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
        </intent-filter>
    </activity>
</application>

我正在使用 Android Studio 4.1

请给予建议。


可能与 https://github.com/android/android-test/issues/743 相关。您可以尝试使用 AndroidX Test 库的 1.3.1-alpha02 版本(或更高版本)吗? - gosr
1
我不确定这是否有帮助。最近我也遇到了这个问题。当我在我的build.gradle(app)中使用androidTestImplementation 'androidx.test:rules:1.3.0-alpha03'而不是androidTestImplementation 'androidx.test:rules:1.3.1-alpha02'时,我成功解决了它。在新版本中,ActivityTestRule已被弃用。 - Suri
亲爱的@gosr,我尝试了但不起作用。我找到了一个解决方案并进行了检查。 - Dehan Wjiesekara
亲爱的@hypergogeta,我尝试了你的建议,但还是不行。我找到了一个解决方案并进行了检查。 - Dehan Wjiesekara
2个回答

10

这是我所做的事情。

在 Espresso 录制结束时,我注意到 Android Studio 会自动将以下库添加到应用程序级别的 Gradle build.xml 文件中。

androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0-alpha02'

这是我根据Google Android开发者文档中的Espresso设置指南手动添加的其他库。

androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'

然后我将上述库更新到最新版本,操作如下(因为Android Studio建议使用最新版本,所以我进行了更新)。

androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'

然后我将 espresso-contrib 的版本与 espresso-core 相同,如下所示

androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.3.0'

请注意,现在espresso-contribespresso-core的版本都是3.3.0

我将以下库从我的Gradle构建文件中移除,但没有检查它们如果继续存在会发生什么。因为我的目的不是测试而是作为一种机器人程序连续执行一个任务。

testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'

最终,它奏效了,我认为问题中出错的原因是版本不匹配。


1
我必须保留Junit库,但这个解决方案不起作用。希望在下一个稳定版本的Espresso中能够修复。 - Paolo Moschini
1
这对我有用,我使用的是Espresso 3.4.0-alpha02,将其设置为3.3.0后,问题就解决了。 - Codigo Morsa
@CodigoMorsa 谢谢您让大家知道。 - Dehan Wjiesekara

2
在我的情况下,这个配置是有效的。
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.4.0-alpha03'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha03'
androidTestImplementation 'androidx.test:runner:1.3.1-alpha03'
androidTestImplementation 'androidx.test:rules:1.3.1-alpha03'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接