要在Android设备上运行仪器测试,可以使用以下解决方法:
dependencies {
    // ...
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    // ...
}
ExampleInstrumentedTest的类:import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.platform.app.InstrumentationRegistry;
import org.junit.Test;
import org.junit.runner.RunWith;
import static org.junit.Assert.*;
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
    @Test
    public void useAppContext() {
        // 获取应用程序上下文
        Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
        // 检查应用程序上下文是否正确
        assertEquals("com.example.app", appContext.getPackageName());
    }
}
在Android Studio中,使用设备连接到计算机并确保设备处于调试模式下。
在Android Studio的"Build Variants"面板中,选择正确的测试变体。例如,选择"androidTest"。
在Android Studio的工具栏中,点击运行按钮或使用快捷键运行仪器测试。
这样就可以在连接的Android设备上运行仪器测试了。