Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric 2.x + Maven on Jenkins failed with APKLIB dependencies

I am having an issue using Robolectric 2.x (I am actually using 2.1) with an Android Maven project using APKLIBs.

It seams that Robolectric 2.x expect the Android libraries to be referenced in the project.properties (This file is automatically filled by Intellij or Eclipse).

It works fine when using Maven on developing environment, however If I want build my Android Maven project on a CI server (Jenkins) my build is failing with:

java.lang.RuntimeException: huh? can't find parent for StyleData{name='Theme_Abs_cs', parent='@style/Theme_Sherlock_Light'}

Apparently Robolectric cannot find the Android library dependencies.

Does anyone has a configuration working with Android APKLIB + Jenkins + Maven + Robolectric 2.x?

like image 600
Niqo Avatar asked Jun 04 '13 00:06

Niqo


1 Answers

Here's how we handle this at Square... android-maven-plugin unpacks your APK dependencies into target/unpack, then we extend RobolectricTestRunner to pull them in. A little clunky but the best we've come up with.

public class SquareTestRunner extends RobolectricTestRunner {
  private static boolean alreadyRegisteredAbs = false;

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

  @Override protected AndroidManifest createAppManifest(File baseDir) {
    return new MavenAndroidManifest(Fs.newFile(new File(".")));
  }

  public static class MavenAndroidManifest extends AndroidManifest {
    public MavenAndroidManifest(FsFile baseDir) {
      super(baseDir);
    }

    @Override protected List<FsFile> findLibraries() {
      // Try unpack folder from maven.
      FsFile unpack = getBaseDir().join("target/unpack/apklibs");
      if (unpack.exists()) {
        FsFile[] libs = unpack.listFiles();
        if (libs != null) {
          return asList(libs);
        }
      }
      return emptyList();
    }

    @Override protected AndroidManifest createLibraryAndroidManifest(FsFile libraryBaseDir) {
      return new MavenAndroidManifest(libraryBaseDir);
    }
  }
}
like image 172
Xian Avatar answered Nov 15 '22 03:11

Xian