首页 > 解决方案 > Android:为什么 Espresso 不能在 compileSdkVersion 27 上运行?

问题描述

我开始测试我的应用程序并遇到了一些错误,但设法修复它们并成功运行测试。

问题是:为什么这个 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

标签: androidtestingandroid-espresso

解决方案


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


推荐阅读