gpt4 book ai didi

gradle - 在Robolectric中使用多个res文件夹

转载 作者:行者123 更新时间:2023-12-03 03:37:58 27 4
gpt4 key购买 nike

我当前的Gradle配置有多个(合并)res文件夹:

sourceSets {
androidTest {
setRoot('src/test')
}
main {
res.srcDirs =
[
'src/main/res/features/registration',
'src/main/res/features/login',
'src/main/res'
]
}
}

但是Robolectric允许我使用AndroidManifest配置单个目录:
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18;

public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}

@Override
protected AndroidManifest getAppManifest(Config config) {
String manifestProperty = "../app/src/main/AndroidManifest.xml";
String resProperty = "../app/src/main/res";

return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty)) {
@Override
public int getTargetSdkVersion() {
return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
}
};
}
}

这样测试就失败了。是否可以配置robolectric以反射(reflect)我的gradle文件?

最佳答案

类似于Luca的另一种解决方案:

public class MyTestRunner extends RobolectricTestRunner {
...
@Override
protected AndroidManifest getAppManifest(Config config) {
String appRoot = "./src/main/";
String manifestPath = appRoot + "AndroidManifest.xml";
String resDir = appRoot + "res";
String assetsDir = appRoot + "assets";

return new AndroidManifest(Fs.fileFromPath(manifestPath), Fs.fileFromPath(resDir), Fs.fileFromPath(assetsDir)) {
@Override
public List<ResourcePath> getIncludedResourcePaths() {
List<ResourcePath> paths = super.getIncludedResourcePaths();
paths.add(new ResourcePath(getRClass(), getPackageName(), Fs.fileFromPath("../app/src/main/res/features/registration"), getAssetsDirectory()));
paths.add(new ResourcePath(getRClass(), getPackageName(), Fs.fileFromPath("../app/src/main/res/features/login"), getAssetsDirectory()));
return paths;
}
};
}
}

别忘了用 @RunWith(MyTestRunner.class)注释测试

关于gradle - 在Robolectric中使用多个res文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27618660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com