androidandroid-espresso-recorder

Espresso test fail's to build with "Conflict with dependency"


I just wanted to start integrating some basics test of my APP using the "Record Espresso test" in Android Studio version 3.2.1. I can record the test. Afterwards, Studio prompts me, that some dependencies are missing and if it should add them. Here I select yes. Gradle want to sync again, and that process completes without any issues. If I now want to run one of the tests, the build process kicks in and start. It fail's with

Conflict with dependency 'androidx.annotation:annotation' in project ':app'. Resolved versions for app (1.0.0-rc01) and test app (1.0.0) differ. See https://d.android.com/r/tools/test-apk-dependency-conflicts.html for details.

I looked at the link provided, but it does not really get me any further.

Can anyone guide me in the right direction, what I am doing wrong ?

The result of the "androidDependencies" is too long to append to my question, due to the 30000 char limit.

My Android app Gradle file

apply plugin: 'com.android.application'

project.ext.appPlayStoreVersion = 68
project.ext.appVersion = '1.8.2'
project.ext.appLabel = 'FreightWeight'

project.ext.compileSdk = 28
project.ext.minSdk = 21
project.ext.targetSdk = 28

project.ext.firebase_version = '16.0.1'
project.ext.firebase_ui_version = '2.1.1'
project.ext.support_library_version = '27.1.1'
project.ext.buildTools = '28.0.3'
project.ext.multidex_version = '1.0.1'
project.ext.glide_version = '3.7.0'
project.ext.constraints_version = '1.0.2'

// Create a variable called keystorePropertiesFile, and initialize it to your
// keystore.properties file, in the rootProject folder.
def keystorePropertiesFile = rootProject.file("keystore.properties")

// Initialize a new Properties() object called keystoreProperties.
def keystoreProperties = new Properties()

// Load your keystore.properties file into the keystoreProperties object.
keystoreProperties.load(new FileInputStream(keystorePropertiesFile))


android {
    signingConfigs {
        configRelease {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile file(keystoreProperties['storeFile'])
            storePassword keystoreProperties['storePassword']
        }
    }
    compileSdkVersion compileSdk
    defaultConfig {
        applicationId "de.mobacomp.android.freightweight"
        minSdkVersion minSdk
        targetSdkVersion targetSdk
        versionCode appPlayStoreVersion
        versionName "$appVersion"
        // Enabling multidex support.
        multiDexEnabled false
        resValue "string", "app_version_name", versionName
        signingConfig signingConfigs.configRelease
        testInstrumentationRunner = 'android.support.test.runner.AndroidJUnitRunner'
    }
    buildTypes {
        release {
            minifyEnabled true
            resValue "string", "app_name", appLabel
            resValue "string", "mobile_ads_id", "ca-app-pub-xyz"
            resValue "string", "ad_unit_banner_1", "ca-app-pub-xyz"
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.configRelease
            resValue "string", "APP_FILEPROVIDER", defaultConfig.applicationId + ".release" + ".fileprovider"
            // lets generate a proper file name
            applicationVariants.all { variant ->
                variant.outputs.all { output ->
                    def fileName = applicationId + "-v" + appVersion + "_" + versionCode + ".apk"
                    outputFileName = new File("release", fileName)

                }
            }
        }
        debug {
            minifyEnabled false
            resValue "string", "app_name", "FW-debug"
            resValue "string", "mobile_ads_id", "ca-app-pub-xyz"
            resValue "string", "ad_unit_banner_1", "ca-app-pub-xyz"
            applicationIdSuffix ".debug"
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            signingConfig signingConfigs.configRelease
            resValue "string", "APP_FILEPROVIDER", defaultConfig.applicationId + ".debug" + ".fileprovider"
        }
    }
    dexOptions {
        javaMaxHeapSize "4g"
        jumboMode true
    }
    productFlavors {
    }
    buildToolsVersion '28.0.3'
}

dependencies {
    implementation fileTree(include: ['*.jar'], dir: 'libs')
    testImplementation 'junit:junit:4.12'

    // GLide
    implementation "com.github.bumptech.glide:glide:$glide_version"

    // Firebase stuff
    implementation "com.firebaseui:firebase-ui-database:4.2.1"
    implementation "com.firebaseui:firebase-ui-auth:4.2.1"
    implementation "com.google.firebase:firebase-core:16.0.5"
    implementation "com.google.firebase:firebase-database:16.0.5"
    implementation "com.google.firebase:firebase-storage:16.0.5"
    implementation "com.google.firebase:firebase-auth:16.0.5"

    implementation 'com.google.firebase:firebase-ml-vision:18.0.1'
    implementation 'com.google.firebase:firebase-ml-vision-image-label-model:17.0.2'

    // Google Play stuff
    implementation "com.google.android.gms:play-services-auth:16.0.1"
    implementation "com.google.android.gms:play-services-base:16.0.1"
    implementation "com.google.android.gms:play-services-analytics:16.0.5"
    implementation "com.google.android.gms:play-services-cast-framework:16.1.0"
    implementation 'com.google.gms:google-services:4.1.0'

    implementation "com.android.support:preference-v14:28.0.0"
    implementation "com.android.support:mediarouter-v7:28.0.0"
    implementation "com.android.support:design:28.0.0"
    implementation "com.android.support:customtabs:28.0.0"
    implementation "com.android.support:cardview-v7:28.0.0"
    implementation "com.android.support:recyclerview-v7:28.0.0"
    implementation "com.android.support:support-v4:28.0.0"
    implementation "com.android.support:appcompat-v7:28.0.0"
    implementation "com.android.support:palette-v7:28.0.0"
    implementation "com.android.support.constraint:constraint-layout:1.1.3"
    implementation 'com.android.support:recyclerview-v7:28.0.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
    androidTestImplementation 'androidx.test:rules:1.1.0'
}



apply plugin: 'com.google.gms.google-services'

And last, a short test I recorded (tried longer ones too, same error)

package de.mobacomp.android.freightweight;


import androidx.test.espresso.ViewInteraction;
import androidx.test.filters.LargeTest;
import androidx.test.rule.ActivityTestRule;
import androidx.test.runner.AndroidJUnit4;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;

import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
import org.hamcrest.core.IsInstanceOf;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.Espresso.pressBack;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.scrollTo;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withClassName;
import static androidx.test.espresso.matcher.ViewMatchers.withContentDescription;
import static androidx.test.espresso.matcher.ViewMatchers.withId;
import static androidx.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;

@LargeTest
@RunWith(AndroidJUnit4.class)
public class MainFragmentActivityTest {

    @Rule
    public ActivityTestRule<MainFragmentActivity> mActivityTestRule = new ActivityTestRule<>(MainFragmentActivity.class);

    @Test
    public void mainFragmentActivityTest() {
        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(7000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction button = onView(
                allOf(withId(R.id.buttonContinueStartApp),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        0),
                                1),
                        isDisplayed()));
        button.check(matches(isDisplayed()));

        ViewInteraction button2 = onView(
                allOf(withId(R.id.buttonContinueStartApp),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.<View>instanceOf(android.widget.LinearLayout.class),
                                        0),
                                1),
                        isDisplayed()));
        button2.check(matches(isDisplayed()));

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction overflowMenuButton = onView(
                allOf(withContentDescription("More options"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.action_bar),
                                        2),
                                0),
                        isDisplayed()));
        overflowMenuButton.perform(click());

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction appCompatTextView = onView(
                allOf(withId(R.id.title), withText("Datenschutz Hinweise"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        appCompatTextView.perform(click());

        pressBack();

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction overflowMenuButton2 = onView(
                allOf(withContentDescription("More options"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.action_bar),
                                        2),
                                0),
                        isDisplayed()));
        overflowMenuButton2.perform(click());

        // Added a sleep statement to match the app's execution delay.
        // The recommended way to handle such scenarios is to use Espresso idling resources:
        // https://google.github.io/android-testing-support-library/docs/espresso/idling-resource/index.html
        try {
            Thread.sleep(250);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        ViewInteraction appCompatTextView2 = onView(
                allOf(withId(R.id.title), withText("Settings"),
                        childAtPosition(
                                childAtPosition(
                                        withId(R.id.content),
                                        0),
                                0),
                        isDisplayed()));
        appCompatTextView2.perform(click());

        ViewInteraction linearLayout = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.recycler_view),
                                childAtPosition(
                                        withId(android.R.id.list_container),
                                        0)),
                        1),
                        isDisplayed()));
        linearLayout.perform(click());

        ViewInteraction linearLayout2 = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.recycler_view),
                                childAtPosition(
                                        withId(android.R.id.list_container),
                                        0)),
                        3),
                        isDisplayed()));
        linearLayout2.perform(click());

        ViewInteraction linearLayout3 = onView(
                allOf(childAtPosition(
                        allOf(withId(R.id.recycler_view),
                                childAtPosition(
                                        withId(android.R.id.list_container),
                                        0)),
                        4),
                        isDisplayed()));
        linearLayout3.perform(click());
    }

    private static Matcher<View> childAtPosition(
            final Matcher<View> parentMatcher, final int position) {

        return new TypeSafeMatcher<View>() {
            @Override
            public void describeTo(Description description) {
                description.appendText("Child at position " + position + " in parent ");
                parentMatcher.describeTo(description);
            }

            @Override
            public boolean matchesSafely(View view) {
                ViewParent parent = view.getParent();
                return parent instanceof ViewGroup && parentMatcher.matches(parent)
                        && view.equals(((ViewGroup) parent).getChildAt(position));
            }
        };
    }
}

The output of the "androidDependencies" is to big, so here is a link to it. Link to the file on my webserver

Removed the "'com.google.gms:google-services:4.1.0'" as suggested, no change.


Solution

  • Just so I can mark it as complete:

    I already used the refactor to androidx in Studio a while ago. Just let it run again, and it found 11 more parts to convert, and the test now starts. I assume the Recording Espresso Test feature in Studio is not jet converted to androidx, thus creates conflicting configuration.

    So now I can record Espresso tests and compile fine.

    Thanks for the help.