Skip to content

Instantly share code, notes, and snippets.

@dcow
Last active August 29, 2015 14:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dcow/62f7c5257cf4725248f0 to your computer and use it in GitHub Desktop.
Save dcow/62f7c5257cf4725248f0 to your computer and use it in GitHub Desktop.
Notes forking Robolectric and the associated Gradle plugin for Android projects

Robolectric is Fun™

Steps for building robolectric and the gradle plugin locally

Robolectric

Setup

Robolectric is a pure maven build. You'll need maven.

$ brew install maven

Robolectric currently works with sdk 18. Make sure you have downloaded sdk 18 (with google apis) using the android tool. You'll need the support library version 19.0.1 too.

$ android update sdk -u --all --filter addon-google_apis-google-18,extra-anroid-support 

Then, follow the steps in the robolectric readme to install the needed dependencies into your local maven repository.

OSX

For robolectric to build, I needed to export JAVA_HOME pointing to the result of the OSX java_home command.

$ export JAVA_HOME=$(/usr/libexec/java_home)

Clone

Grab a copy of the Robolectric source. After you have the source, you'll probably want to checkout the latest release tag and then branch so you've got a clean workspace.

Own

To distinguish your copy from the real Robolectric, edit the groupId in the top level pom.xml.

<groupId>com.example</groupId>

You'll then need to edit the pom.xml files in all the module projects to point at your altered groupId.

<parent>
    <groupId>com.example</groupId>
    <artifactId>robolectric-parent</artifactId>
    ...
</parent>

Leave the version unchanged for now. The module projects have some dependencies on others. Edit the groupId in the dependencies section of the pom.xml file too. But instead of hard-coding the groupId, we can just use the dynamic maven property. For example:

<dependencies>
    <dependency>
        <groupId>${parent.groupId}</groupId>
        <artifactId>robolectric-annotations</artifactId>
    </dependency>
...

You'll need to edit dependencies in all the modules.

Publish

Build

Once the groupId is in shape, building is simple:

$ mvn package

Install

Maven makes it super easy to publish locally:

$ mvn install

(You can simply run mvn install as it depends on the package target.)

This deposites the artifacts from the last step into your local maven repository (usually ~/.m2/repository).

Version

If you need to update your version, use something like:

$ mvn versions:set -DnewVersion=Your.Desired.Version[-SNAPSHOT]

(-SNAPSHOT is optional.)

Robolectric Gradle Plugin

Setup

Android Home

For now, make sure ANDROID_HOME is defined in your environment:

$ export ANDROID_HOME="/path/to/your/sdk"

Note: You cannot use local.properties for this build because the Android plugin is a compile time dependency of the plugin project. There may be a way to fix this but I haven't looked into it yet.

Clone

Grab the robolectric-gradle-plugin.

Own

You may want to change the groupId so that your version doesn't shadow the released plugin on maven central. Edit the build.gradle script:

group = 'com.example'

Thanks gradle!

Publish

Build

$ ./gradlew build

Publish to maven local

$ ./gradlew install

This generates a pom file and deposits the artifacts into ~/.m2/repository.

Using the Artifacts

Point your build script at your local maven:

repositories {
    mavenLocal()
    // First wins.  Others repos.  Order matters.
}

Don't forget to add mavenLocal() to the buildscript repositories for your local installation of the android plugin.

Take a look at my example project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment