Android Studio3.3中Cannot resolve symbol ActivityTestRule

      Recent looking at "Android Programming The Definitive Guide," under Windows10 installed Android Studio3.3, watching the exercises in the book compiled sample program, see Chapter 21, "" audio playback unit tests "in-depth study 21.12: Espresso when integration testing, a problem was found: ActivityTestRule not recognize this class, as shown below:

 

ActivityTestRule

Move the mouse to the location of ActivityTestRule class location, appears Can not resolve symbol 'ActivityTestRule' mistakes, access to the Android developer documentation,

 

Find the need to import android.support.test.rule.ActivityTestRule

import android.support.tst.rule.ActivityTestRule;

 

But after the import is still there Can not resolve symbol 'ActivityTestRule' wrong, and turn to Google, and finally found the class to be used ActivityTestRule light into android.support.test.rule.ActivityTestRule not, have to add library dependencies:

Add a dependency in two ways:

A method, add the following in dependent projects app build.gradle directory resides:

androidTestImplementation 'com.android.support.test:rules:1.0.2'

Method Two,:

在Android视图的app项中,选中app,单击鼠标右键,选中“Open Module Settings”,打开【Project Structure】对话框,在app项找到右侧的Dependencies,如下图所示:

选中最右上方的“+”号,添加新的“Library Dependency”,

在文本框中输入rules搜索,如下图所示:

单击OK按钮后,com.android.support.test:rules:1.0.2就导入成功了,

从上图可以看出,rules 1.0.2库默认Scope是Implementation的,如果需要进行测试如Unit Test或者是InstrumentedTest时,则需要修改对应的Scope选项,改成Test Implementation(针对整合测试)或者Unit Test Implementation(针对单元测试),其所在文件app/build.gradle也会相应的修改,如下图所示:

dependencies {
    implementation fileTree(inclu: ['*.jar'], dir: 'libs')
    implementation 'com.adroid.support:appcompat-v7:28.0.0'
    im最后,ActivityTestRule终于可以使用了,如下图所示:

 

参考了两个资源:

1、Cannot resolve symbol ‘ActivityTestRule’ - SOLVED

2、Why cannot I import AndroidJUnit4 and ActivityTestRule into my unit test class?

发布了107 篇原创文章 · 获赞 35 · 访问量 97万+

Guess you like

Origin blog.csdn.net/ccf19881030/article/details/87066639