How do you integrate Robolectric, Maven, ActionBarSherlock (and preferably + IntelliJ) into an Android project?

时间:2023-01-17 13:10:42

I've been banging my head against a wall for some time trying to get maven, robolectric, actionbar sherlock, and intellij, to all play nicely. I've given up on building with intellij for now and am focusing on maven.

我一直在撞墙一段时间试图让maven,robolectric,actionbar sherlock和intellij,所有人都玩得很好。我现在已经放弃了与intellij一起建设并且专注于maven。

I have an app with an action bar when I skip the tests in the maven build (which tells me I'm good to go on the ActionBar integration front). The IntelliJ build fails on ManifestParsingTest.java complaining that it doesn't know where junit is (among other issues).

当我跳过maven构建中的测试时,我有一个带有操作栏的应用程序(这告诉我,我很高兴继续使用ActionBar集成前端)。 IntelliQ构建在ManifestParsingTest.java上失败,抱怨它不知道junit在哪里(以及其他问题)。

Robolectric integration

Robolectric集成

I already had Robolectric running successfully in the project. I copied the files from Jake Wharton's gist into src/test/java/com/blah/blah/support/ (except ShadowSherlockFragmentActivity.java which lives in src/test/java/com/blah/blah/support/shadows/) and put this line in the constructor for my custom test runner (lives in support folder):

我已经在项目中成功运行了Robolectric。我将Jake Wharton的要点中的文件复制到src / test / java / com / blah / blah / support /(除了生成src / test / java / com / blah / blah / support / shadows /的ShadowSherlockFragmentActivity.java)并放入我的自定义测试运行器的构造函数中的这一行(位于支持文件夹中):

ActionBarSherlock.registerImplementation(ActionBarSherlockRobolectric.class);

mvn errors

mvn错误

I'm running into these build errors (IntelliJ also has red squigglies for the same stuff):

我遇到了这些构建错误(IntelliJ也有相同的红色波浪形):

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.0:testCompile (default-testCompile) on project Blah: Compilation failure: Compilation failure:
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/SherlockResourceLoader.java:[29,9] cannot find symbol
[ERROR] symbol  : constructor ResourceLoader(int,java.lang.Class,java.util.List<java.io.File>,java.io.File)
[ERROR] location: class com.xtremelabs.robolectric.res.ResourceLoader
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/SherlockResourceLoader.java:[45,5] method does not override or implement a method from a supertype
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[39,37] resourceLoaderForRootAndDirectory has private access in com.xtremelabs.robolectric.RobolectricTestRunner
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[47,30] cannot find symbol
[ERROR] symbol  : method getResourcePath()
[ERROR] location: class com.xtremelabs.robolectric.RobolectricConfig
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[48,9] resourceLoaderForRootAndDirectory has private access in com.xtremelabs.robolectric.RobolectricTestRunner
[ERROR] /Users/clatislaw/Documents/Code/Android/projectName/src/test/java/com/blah/blah/support/ActionBarSherlockTestRunner.java:[38,3] method does not override or implement a method from a supertype

pom.xml

的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project
    xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.blah.blah</groupId>
    <artifactId>projectName</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>apk</packaging>
    <name>Project Name App</name>

    <dependencies>
        <dependency>
            <groupId>com.google.android</groupId>
            <artifactId>android</artifactId>
            <version>4.0.1.2</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>com.pivotallabs</groupId>
            <artifactId>robolectric</artifactId>
            <version>1.1</version>
            <scope>test</scope>
        </dependency>

        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <version>4.8.2</version>
             <scope>test</scope>
        </dependency>

        <dependency>
             <groupId>com.google.android</groupId>
             <artifactId>support-v4</artifactId>
             <version>r6</version>
        </dependency>

        <dependency>
            <groupId>com.actionbarsherlock</groupId>
            <artifactId>library</artifactId>
            <version>4.1.0</version>
            <type>apklib</type>
        </dependency>
    <build>
        <finalName>${project.artifactId}</finalName>

        <plugins>
            <plugin>
                <!-- See http://code.google.com/p/maven-android-plugin/ -->
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.2.0</version>
                <configuration>
                    <sdk>
                        <platform>15</platform>
                    </sdk>
                </configuration>
                <extensions>true</extensions>
            </plugin>
        </plugins>
    </build>
</project>

Is there some project config I'm missing?

是否有一些项目配置我不见了?

2 个解决方案

#1


10  

At Square, to support this use case we use a modified version of Robolectric. We are working with Pivotal to get us off of using a fork and the necessary changes upstream into the main repository. The changes required are hooks into the resource loading system. These methods are currently private which is the cause of the compilation failures that you are seeing.

在Square,为了支持这个用例,我们使用了Robolectric的修改版本。我们正在与Pivotal合作,让我们不再使用fork和上游必要的更改到主存储库中。所需的更改是挂钩到资源加载系统。这些方法目前是私有的,这是您看到的编译失败的原因。

Pivotal doesn't exactly make releases in any semblance of a frequent manner so if the changes do make it upstream you'll likely have to use the SNAPSHOT version for a while.

Pivotal并没有以任何常见的方式完全发布版本,因此如果更改确实使其成为上游版本,则可能需要使用SNAPSHOT版本一段时间。

#2


2  

I could make it work.

我可以让它发挥作用。

Try this:

尝试这个:

  • move robolectric and junit dependencies to the end.

    将robolectric和junit依赖项移到最后。

  • indicate src and test path in the build tag, after finalname and before plugins.

    在finalname之后和插件之前指示构建标记中的src和测试路径。

  • rebuild and run test from maven toolbar.

    从maven工具栏重建并运行测试。

    <dependencies>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>4.0.1.2</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>support-v4</artifactId>
        <version>r6</version>
    </dependency>
    
    <dependency>
        <groupId>org.roboguice</groupId>
        <artifactId>roboguice</artifactId>
        <version>2.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.octo.android.robospice</groupId>
        <artifactId>robospice-spring-android</artifactId>
        <version>1.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.9</version>
    </dependency>
    
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.2</version>
    </dependency>
    
    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>actionbarsherlock</artifactId>
        <version>4.2.0</version>
        <type>apklib</type>
    </dependency>
    
    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>actionbarsherlock</artifactId>
        <version>4.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>1.1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    
    </dependencies>
    
    <build>
    <finalName>${project.artifactId}</finalName>
    
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>test</testSourceDirectory>
    
    <plugins>
        <plugin>
            <!-- See http://code.google.com/p/maven-android-plugin/ -->
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <sdk>
                    <platform>15</platform>
                </sdk>
            </configuration>
            <extensions>true</extensions>
        </plugin>
    </plugins>
    </build>
    

#1


10  

At Square, to support this use case we use a modified version of Robolectric. We are working with Pivotal to get us off of using a fork and the necessary changes upstream into the main repository. The changes required are hooks into the resource loading system. These methods are currently private which is the cause of the compilation failures that you are seeing.

在Square,为了支持这个用例,我们使用了Robolectric的修改版本。我们正在与Pivotal合作,让我们不再使用fork和上游必要的更改到主存储库中。所需的更改是挂钩到资源加载系统。这些方法目前是私有的,这是您看到的编译失败的原因。

Pivotal doesn't exactly make releases in any semblance of a frequent manner so if the changes do make it upstream you'll likely have to use the SNAPSHOT version for a while.

Pivotal并没有以任何常见的方式完全发布版本,因此如果更改确实使其成为上游版本,则可能需要使用SNAPSHOT版本一段时间。

#2


2  

I could make it work.

我可以让它发挥作用。

Try this:

尝试这个:

  • move robolectric and junit dependencies to the end.

    将robolectric和junit依赖项移到最后。

  • indicate src and test path in the build tag, after finalname and before plugins.

    在finalname之后和插件之前指示构建标记中的src和测试路径。

  • rebuild and run test from maven toolbar.

    从maven工具栏重建并运行测试。

    <dependencies>
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>android</artifactId>
        <version>4.0.1.2</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>com.google.android</groupId>
        <artifactId>support-v4</artifactId>
        <version>r6</version>
    </dependency>
    
    <dependency>
        <groupId>org.roboguice</groupId>
        <artifactId>roboguice</artifactId>
        <version>2.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.octo.android.robospice</groupId>
        <artifactId>robospice-spring-android</artifactId>
        <version>1.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.9</version>
    </dependency>
    
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.2.2</version>
    </dependency>
    
    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>actionbarsherlock</artifactId>
        <version>4.2.0</version>
        <type>apklib</type>
    </dependency>
    
    <dependency>
        <groupId>com.actionbarsherlock</groupId>
        <artifactId>actionbarsherlock</artifactId>
        <version>4.2.0</version>
    </dependency>
    
    <dependency>
        <groupId>com.pivotallabs</groupId>
        <artifactId>robolectric</artifactId>
        <version>1.1</version>
        <scope>test</scope>
    </dependency>
    
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8.2</version>
        <scope>test</scope>
    </dependency>
    
    </dependencies>
    
    <build>
    <finalName>${project.artifactId}</finalName>
    
    <sourceDirectory>src</sourceDirectory>
    <testSourceDirectory>test</testSourceDirectory>
    
    <plugins>
        <plugin>
            <!-- See http://code.google.com/p/maven-android-plugin/ -->
            <groupId>com.jayway.maven.plugins.android.generation2</groupId>
            <artifactId>android-maven-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <sdk>
                    <platform>15</platform>
                </sdk>
            </configuration>
            <extensions>true</extensions>
        </plugin>
    </plugins>
    </build>