1、build.gradle中添加assets
设置完成后要运行File->Sync project with gradle files。
sourceSets {...androidTest {assets.srcDirs = ['src/androidTest/assets']}
}
2、读取时注意context
一定是InstrumentationRegistry.getInstrumentation().getContext()。
网上有很多种说法,有的用了Deprecated的类或方法,也有用InstrumentationRegistry.getInstrumentation().getTargetContext()。用getTargetContext则获取的是主应用的context,读取的assets也是主应用的,而不是androidTest/assets中的。
Context context = InstrumentationRegistry.getInstrumentation().getContext();
AssetsWrapper asset = new AssetsWrapper(context.getAssets());
没有使用Robolectric,一是担心场景不能完全覆盖,还有就是gradle下载它太慢,半途放弃了。