Skip to content

Instantly share code, notes, and snippets.

Created November 14, 2013 08:27
What would you like to do?
AndroidTestRunner that works around the problem with different package name in manifest and R class for Robolectric
package com.myapp.whatever;
import org.junit.runners.model.InitializationError;
import org.robolectric.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.res.FsFile;
import com.myapp.R;
public class AndroidTestRunner extends RobolectricTestRunner {
public AndroidTestRunner(Class<?> testClass) throws InitializationError {
protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) {
return new CustomPackageManifest(manifestFile, resDir, assetsDir);
private static class CustomPackageManifest extends AndroidManifest {
public CustomPackageManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory) {
super(androidManifestFile, resDirectory, assetsDirectory);
public String getRClassName() throws Exception {
return getRClass().getName();
public Class<?> getRClass() {
return R.class;
public String getPackageName() {
return R.class.getPackage().getName();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment