对于使用 AppCompat lib(aar 类型)的 Android Studio + Robolectric2.3 + android-maven-plugin 3.9.0-rc.2 项目,我遇到了类似的问题,导致:
java.lang.RuntimeException:嗯?找不到 StyleData{name='Theme_Custom', parent='@style/Theme_AppCompat_Light' 的父级
为了解决这个问题,我创建了自定义测试运行器:
public class MyTestRunner extends RobolectricTestRunner {
private static boolean alreadyRegisteredAbs = false;
public MyTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) {
return new MavenAndroidManifest( manifestFile, resDir, assetsDir);
}
public static class MavenAndroidManifest extends AndroidManifest {
public MavenAndroidManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory) {
super(androidManifestFile, resDirectory, assetsDirectory);
}
public MavenAndroidManifest(FsFile libraryBaseDir) {
super(libraryBaseDir);
}
@Override
protected List<FsFile> findLibraries() {
// Try unpack folder from maven.
FsFile unpack = getBaseDir().join("target/unpacked-libs");
if (unpack.exists()) {
FsFile[] libs = unpack.listFiles();
if (libs != null) {
return Arrays.asList(libs);
}
}
return Collections.emptyList();
}
@Override
protected AndroidManifest createLibraryAndroidManifest(FsFile libraryBaseDir) {
return new MavenAndroidManifest(libraryBaseDir);
}
}
}