导入中无法识别 AndroidTestCompile 依赖项

AndroidTestCompile dependencies not recognized in the imports

实际上,我的项目有单元测试。都是在/src/test/java/里配置的,最近需要在/src/androidTest/java里加instrumentation测试。为此,我在 build.gradle 中添加了 espresso 依赖项。

dependencies {
    compile files('libs/pixlui-1-0-5.jar')
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    compile('com.fortysevendeg.swipelistview:swipelistview:1.0-SNAPSHOT@aar') {
        transitive = true
    }
    compile 'com.google.code.gson:gson:2.2.4'
    compile 'com.android.support:appcompat-v7:20.+'
    compile 'com.google.android.gms:play-services-maps:7.3.0'
    compile 'com.google.android.gms:play-services-location:7.3.0'
    compile 'com.google.android.gms:play-services-gcm:7.3.0'

    compile 'com.loopj.android:android-async-http:1.4.5'
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    compile 'com.android.support:support-v4:20.+'
    compile 'ch.acra:acra:4.5.0'
    compile 'com.nostra13.universalimageloader:universal-image-loader:1.9.2'
    compile 'com.squareup.picasso:picasso:2.3.4'
    provided 'com.squareup.dagger:dagger-compiler:1.2.+'
    compile 'com.squareup.dagger:dagger:1.2.+'
    compile 'com.google.guava:guava:15.0'
    compile 'com.facebook.android:facebook-android-sdk:3.23.0'
    compile 'com.mixpanel.android:mixpanel-android:4.5.3'
    compile 'com.google.maps.android:android-maps-utils:0.3+'

    // Testing dependencies
    testCompile 'junit:junit:4.12'
    testCompile "org.mockito:mockito-core:1.9.5"

    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
    androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
    androidTestCompile ('com.android.support.test.espresso:espresso-contrib:2.0') {
        exclude module: 'support-annotations'
    }
}

之后,我选择了 Build Variants -> Test Artifact -> Android Instrumentation Tests。

但是当我开始编码时,none 的依赖项被识别:

"Cannot resolve symbol onView"、"Cannot resolve symbol ViewInteraction"、等等...

这是我的 activity 测试:

import android.support.test.espresso.Espresso.onView;
import android.test.ActivityInstrumentationTestCase2;

import com.wiffinity.easyaccess.R;

/**
 * Created by Javier on 05/06/2015.
 */
public class EntryActivityTest extends ActivityInstrumentationTestCase2<EntryActivity> {

    public EntryActivityTest() {
        super(EntryActivity.class);
    }

    @Override
    protected void setUp() throws Exception
    {
        super.setUp();
        getActivity();
    }

    public void testLoginButtonClicked(){
        onView();

        ViewInteraction entryBtn;
        entryBtn = onView(withId(R.id.entry_button));

        entryBtn.performClick();
    }
}

为什么 Android Studio 1.2 不能解决这些依赖关系?我是不是忘记配置什么了?

您可能需要重建项目。

在 Android 工作室中:

构建 -> 重建项目。

如果它没有帮助 运行 下面的 gradle 任务(假设你有一个包装器并且你的模块名称是 "app"):

./gradlew app:dependencies

并确保您的 androidTest 任务包含 espresso 依赖项。

更新:

有时重建项目并不能解决问题,唯一的解决办法是通过执行 gradle assembleAndroidTest 任务手动重建测试 apk。