首页 > 解决方案 > Robolectric does not support API level 28

问题描述

Although this question has been answered for previous versions in other threads, none of the answers seems to work for me with api 28 right now so..

All Robolectric tests worked fine when on api 27. Now when my app api target is 28 they all fail.

I have

@Config(constants = BuildConfig::class, sdk = [Build.VERSION_CODES.P])

on my test class.

Diving into Robolectrics internal SdkConfig.java it looks like they add support for api 28:

addSdk(Build.VERSION_CODES.P, "P", "4651975", "P");

But when evaluating that line of code in the debugger, Build.VERSION_CODES.P evaluates to 10000. Not sure what's going on there.

I'm running Robolectric 3.8, and also tried with the 4.0 alpha version with no luck.

What am I missing?

Edit: For now I'm just running on the latest (what I can tell) supported api version, by annotating the test class(es) with @Config(sdk = [Build.VERSION_CODES.O_MR1]). This will give you Android 8.1.0 (api 27).

标签: androidrobolectric

解决方案


要完成 Algar 的答案,您可以通过像这样注释您的测试类来暂时绕过此错误(同时 Robolectric 修复此问题):

@RunWith(RobolectricTestRunner::class)
@Config(sdk = [Build.VERSION_CODES.O_MR1])
class YourUnitTests {

...

}

它将强制 Robolectric 使用 Android API 27。


推荐阅读