AndroidTestCompile dependencies not recognized in

2019-02-18 08:05发布

问题:

Actually, my project has unit tests. All of them are configured in /src/test/java/ Recently I needed to add instrumentation tests in /src/androidTest/java. To do that I have added espresso dependencies in build.gradle.

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'
    }
}

After that I chose on the Build Variants -> Test Artifact -> Android Instrumentation Tests.

But when I start to code, none of the dependencies are recognized:

"Cannot resolve symbol onView", "Cannot resolve symbol ViewInteraction", etc...

This is my activity test:

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();
    }
}

Why can't Android Studio 1.2 resolve these dependencies? Have I forgotten to configure something?

回答1:

You might need to rebuild the project.

In Android Studio:

Build -> Rebuild project.

If it doesn't help run the following gradle task (given that you have a wrapper and your module name is "app"):

./gradlew app:dependencies

and make sure your androidTest tasks contain espresso dependencies.

Update:

Sometimes rebuilding the project doesn't solve the problem and the only solution is to rebuild the test apk manually by executing gradle assembleAndroidTest task.