How to override the Robolectric runtime dependency repository URL?

While not a fix for using the properties directly, another way to get this to work is by overriding getJarResolver() in a RobolectricTestRunner subclass and pointing it at your artifact host:

public final class MyTestRunner extends RobolectricTestRunner {
  public MyTestRunner(Class<?> testClass) throws InitializationError {
    super(testClass);
  }

  @Override protected DependencyResolver getJarResolver() {
    return new CustomDependencyResolver();
  }

  static final class CustomDependencyResolver implements DependencyResolver {
    private final Project project = new Project();

    @Override public URL[] getLocalArtifactUrls(DependencyJar... dependencies) {
      DependenciesTask dependenciesTask = new DependenciesTask();
      RemoteRepository repository = new RemoteRepository();
      repository.setUrl("https://my-nexus.example.com/content/groups/public");
      repository.setId("my-nexus");
      dependenciesTask.addConfiguredRemoteRepository(repository);
      dependenciesTask.setProject(project);
      for (DependencyJar dependencyJar : dependencies) {
        Dependency dependency = new Dependency();
        dependency.setArtifactId(dependencyJar.getArtifactId());
        dependency.setGroupId(dependencyJar.getGroupId());
        dependency.setType(dependencyJar.getType());
        dependency.setVersion(dependencyJar.getVersion());
        if (dependencyJar.getClassifier() != null) {
          dependency.setClassifier(dependencyJar.getClassifier());
        }
        dependenciesTask.addDependency(dependency);
      }
      dependenciesTask.execute();

      @SuppressWarnings("unchecked")
      Hashtable<String, String> artifacts = project.getProperties();
      URL[] urls = new URL[dependencies.length];
      for (int i = 0; i < urls.length; i++) {
        try {
          urls[i] = Util.url(artifacts.get(key(dependencies[i])));
        } catch (MalformedURLException e) {
          throw new RuntimeException(e);
        }
      }
      return urls;
    }

    @Override public URL getLocalArtifactUrl(DependencyJar dependency) {
      URL[] urls = getLocalArtifactUrls(dependency);
      if (urls.length > 0) {
        return urls[0];
      }
      return null;
    }

    private String key(DependencyJar dependency) {
      String key =
          dependency.getGroupId() + ":" + dependency.getArtifactId() + ":" + dependency.getType();
      if (dependency.getClassifier() != null) {
        key += ":" + dependency.getClassifier();
      }
      return key;
    }
  }
}

It should be noted that this relies on two internal classes of Robolectric so care should be taken when upgrading versions.


You can set properties mavenRepositoryId and mavenRepositoryUrl of RoboSettings which are used by MavenDependencyResolver.

Example:

public class CustomRobolectricRunner extends RobolectricGradleTestRunner {

    static {
        RoboSettings.setMavenRepositoryId("my-nexus");
        RoboSettings.setMavenRepositoryUrl("https://my-nexus.example.com/content/groups/public");
    }


    ...
}