Android:为什么Espresso无法在compileSdkVersion 27上运行?

时间:2018-10-29 11:38:54

标签: android testing android-espresso

我开始测试我的应用程序,但出现了一些错误,但设法修复了这些错误并成功运行了测试。

问题是:为什么此Espresso测试不能在compileSdkVersion 27上运行?我尝试使用“旧”依赖项,但每次都会失败。

这是build.gradle(Module:app)依赖项:

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
//testImplementation 'junit:junit:4.12'
//androidTestImplementation 'com.android.support.test:runner:1.0.2'
//androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//androidTestImplementation 'com.android.support.test:rules:1.0.2'

// Room components
implementation "android.arch.persistence.room:runtime:$rootProject.roomVersion"
annotationProcessor "android.arch.persistence.room:compiler:$rootProject.roomVersion"
androidTestImplementation "android.arch.persistence.room:testing:$rootProject.roomVersion"

// Lifecycle components
implementation "android.arch.lifecycle:extensions:$rootProject.archLifecycleVersion"
annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archLifecycleVersion"

// ANDROID X

// Core library
androidTestImplementation 'androidx.test:core:1.0.0'

// AndroidJUnitRunner and JUnit Rules
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'

// Assertions
androidTestImplementation 'androidx.test.ext:junit:1.0.0'
androidTestImplementation 'androidx.test.ext:truth:1.0.0'
androidTestImplementation 'com.google.truth:truth:0.42'

// Espresso dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-contrib:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-intents:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-accessibility:3.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-web:3.1.0'
androidTestImplementation 'androidx.test.espresso.idling:idling-concurrent:3.1.0'

androidTestImplementation 'androidx.test.espresso:espresso-idling-resource:3.1.0'

androidTestImplementation 'org.hamcrest:hamcrest-library:1.3'
}

注释依赖是我最初尝试使用的依赖,但是无论使用“旧”还是“新”依赖,它都会失败。

这是测试班:

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

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.rule.ActivityTestRule;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.action.ViewActions.click;
import static androidx.test.espresso.action.ViewActions.typeText;
import static androidx.test.espresso.assertion.ViewAssertions.matches;
import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed;
import static androidx.test.espresso.matcher.ViewMatchers.withId;

@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {

@Rule
public ActivityTestRule<MainActivity> myRule = new ActivityTestRule<>(MainActivity.class);

@Test
public void activityLaunch(){
    onView(withId(R.id.fab)).perform(click());
    onView(withId(R.id.edit_word)).check(matches(isDisplayed()));
    onView(withId(R.id.edit_word)).perform(typeText("Hello"));
    onView(withId(R.id.button_save)).perform(click());
    onView(withId(R.id.fab)).check(matches(isDisplayed()));

}
}

以下是错误消息:

Android resource linking failed
Output: C:\Users\...\app\build\intermediates\incremental\mergeDebugAndroidTestResources\merged.dir\values-v28\values-v28.xml:7: error: resource android:attr/dialogCornerRadius not found.
C:\Users\...\app\build\intermediates\incremental\mergeDebugAndroidTestResources\merged.dir\values-v28\values-v28.xml:11: error: resource android:attr/dialogCornerRadius not found.
C:\Users\...\app\build\intermediates\incremental\mergeDebugAndroidTestResources\merged.dir\values\values.xml:1087: error: resource android:attr/fontVariationSettings not found.
C:\Users\...\app\build\intermediates\incremental\mergeDebugAndroidTestResources\merged.dir\values\values.xml:1087: error: resource android:attr/ttcIndex not found.
error: failed linking references.

Command: C:\Users\...\.gradle\caches\transforms-1\files-1.1\aapt2-3.2.1-4818971-windows.jar\47d0792f72b19a69b6e0eaf42eb2139d\aapt2-3.2.1-4818971-windows\aapt2.exe link -I\
       C:\Android\SDK\platforms\android-27\android.jar\
       --manifest\
       C:\Users\...\app\build\intermediates\merged_manifests\debugAndroidTest\processDebugAndroidTestManifest\merged\AndroidManifest.xml\
       -o\
       C:\Users\...\app\build\intermediates\processed_res\debugAndroidTest\processDebugAndroidTestResources\out\resources-debugAndroidTest.ap_\
       -R\
       @C:\Users\...\app\build\intermediates\incremental\processDebugAndroidTestResources\resources-list-for-resources-debugAndroidTest.ap_.txt\
       --auto-add-overlay\
       --java\
       C:\Users\...\app\build\generated\not_namespaced_r_class_sources\debugAndroidTest\processDebugAndroidTestResources\r\
       -0\
       apk\
       --preferred-density\
       420dpi\
       --output-text-symbols\
       C:\Users\...\app\build\intermediates\symbols\androidTest\debug\R.txt\
       --no-version-vectors
Daemon:  AAPT2 aapt2-3.2.1-4818971-windows Daemon #0

1 个答案:

答案 0 :(得分:0)

您的某些支持库的版本为28. +。尝试更改目标并将sdk版本编译为28。