Does Robolectric support API level?

拜拜、爱过 提交于 2019-11-30 07:47:45

Update: The annotation is now @Config(sdk = 18) (or @Config(sdk = Build.VERSION_CODES.JELLY_BEAN_MR2)) and the properties file mentioned in link is now robolectric.properties.

Original Answer: You can use the @Config annotation to have Robolectric emulate an SDK version. You can put this :

import org.robolectric.annotation.Config;

@Config(emulateSdk = 18) // now @Config(sdk = 18) as of Robolectric 3.0
@RunWith(RobolectricTestRunner.class)
public class SomeTest ...

This is also possible using a file as mentioned here

Not sure what it means for your KitKat specific tests but at least the others should work.

In case people like me, still visiting the link for the similar error,

@Config(emulateSdk = ) is not working now. Its changed to sdk--
@Config(constants = BuildConfig.class, sdk=21)

For me, I was getting error with target version 22,

java.lang.UnsupportedOperationException: Robolectric does not support API level 22

and so I emulated it to 21.

James Muranga

According to SdkConfig.java, Roboelectric only supports the following versions / API levels:

SUPPORTED_APIS.put(Build.VERSION_CODES.JELLY_BEAN, new SdkVersion("4.1.2_r1", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.JELLY_BEAN_MR1, new SdkVersion("4.2.2_r1.2", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.JELLY_BEAN_MR2, new SdkVersion("4.3_r2", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.KITKAT, new SdkVersion("4.4_r1", "0"));
SUPPORTED_APIS.put(Build.VERSION_CODES.LOLLIPOP, new SdkVersion("5.0.0_r2", "0"));

Are you sure you have tried those?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!