duplicate entry: org/junit/ClassRule.class in Android

434 views Asked by At

I wan to run an espresso test written by Record Espresso Test in Andorid. The test is written in this way

Test

import android.support.test.espresso.ViewInteraction;
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
import android.test.suitebuilder.annotation.LargeTest;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.closeSoftKeyboard;
import static android.support.test.espresso.action.ViewActions.replaceText;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withParent;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static org.hamcrest.Matchers.allOf;
import android.support.multidex.MultiDex;

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

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

    @Test
    public void carParki_SignIn_Test() {
        ViewInteraction appCompatButton = onView(
                allOf(withId(R.id.id_signIn), withText("SIGN IN"),
                        withParent(allOf(withId(R.id.buttonsLayout),
                                withParent(withId(R.id.content_login_or_signup)))),
                        isDisplayed()));
        appCompatButton.perform(click());

        ViewInteraction appCompatEditText = onView(
                allOf(withId(R.id.field_email),
                        withParent(withId(R.id.email_password_fields)),
                        isDisplayed()));
        appCompatEditText.perform(replaceText("[email protected]"), closeSoftKeyboard());

        ViewInteraction appCompatEditText2 = onView(
                allOf(withId(R.id.field_password),
                        withParent(withId(R.id.email_password_fields)),
                        isDisplayed()));
        appCompatEditText2.perform(replaceText("444444"), closeSoftKeyboard());

        ViewInteraction appCompatButton2 = onView(
                allOf(withId(R.id.email_sign_in_button), withText("Sign In"),
                        withParent(withId(R.id.email_password_buttons)),
                        isDisplayed()));
        appCompatButton2.perform(click());

    }

}

On running, it gives;

Error:Execution failed for task ':app:transformClassesWithJarMergingForDebugAndroidTest'. com.android.build.api.transform.TransformException: java.util.zip.ZipException: duplicate entry: org/junit/ClassRule.class

My gradle file is;

apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "25.0.1"
    defaultConfig {
        applicationId "com.example.fyp_awais.carparki_o"
        minSdkVersion 16
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        multiDexEnabled true
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations , '
    })
    androidTestCompile('com.android.support.test:testing-support-lib:0.1')
    compile 'com.android.support:appcompat-v7:24.2.1'
    compile 'com.android.support:design:24.2.1'
    compile 'com.google.firebase:firebase-database:10.0.1'
    compile 'com.google.android.gms:play-services:10.0.1'
    testCompile 'junit:junit:4.12'
}



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

Any wrong here?

1

There are 1 answers

7
Raut Darpan On

When you added the com.android.support:multidex dependency you actually added some dependencies that collide with other dependencies.

I solved it by:

  1. searching for the class, in you case the "RequestWeakReference.class" (in AndroidStudio just hit Ctrl+N on Windows or CMD-O on Mac)
  2. See which jar contains it - Android Studio will write it in the popup.
  3. Exclude it from all builds, for example:

     android {
     configurations{
        all*.exclude module: 'servlet-api'
    }