I'm a huge fan of living in IDE-land as much as possible, so this approach minimizes command line work and manual project configuration. This tutorial will walk you through using IntelliJ to generate the project and maven integration to make installation of Robolectric simpler.
After creating an Android project with the wizard, you move the source files into a mvn-like structure. Then you configure module settings in IntelliJ. To install Robolectric, you will add a pom file (maven dependency file) into the project. Finally, you will write your first test and run it inside the IDE.
Note: There are other projects and methods out there, but this is what I found to be the most effective way to get up and running. If you'd prefer an all in one solution, check out this project.
- You are starting a new project.
- You are familiar with the IntelliJ and the Android wizard (CE edition is fine).
- You have installed the Android SDK and configured IntelliJ to use it.
- You have maven installed.
Step 1: Create Android project using IntelliJ wizardUse the IntelliJ Android wizard to create a Hello World project.
Step 2: Set up directories for mavenUse IntelliJ or the command line to move files into the following structure:
After you are done, your project should look something like this (minus the test files):
Note: IntelliJ might not like it when you add subdirectories to the folder marked as source. If you are having trouble setting up the directory structure, open the screen shown in the next step and remove the old source folder before adding new subfolders.
Step 3: Configure IntelliJ source and test foldersUpdate module settings in IntelliJ to recognize the source and test directories (below: blue folder is source and test folder is green).
Step 4: Install RobolectricUse this sample pom.xml to get you started. Change the groupId, artifactId, version, and name tags to suit your project. The file should live in the root of the project.
And some of my environment set up (zsh):
# Shell Settings (one long line, broken for view online) export ANDROID_HOME="/usr/bin/android-sdk-macosx" export PATH="/usr/bin/android-sdk-macosx/platform-tools:/usr/bin: /usr/bin/android-sdk-macosx/tools: /usr/local/apache-maven-2.1.2/bin:$PATH"After updating the pom and environment variables, check the project in IntelliJ to make sure the "Maven Projects" view looks something like this (you may need to refresh):
If you see errors, you may just need to wait for it to finish indexing and importing your libraries. If you have git configured, once you see this dialog, you should be moments away from running your first test.
Step 5: Create testI use the IDE to generate a test. On a Mac, the key combination is Apple+Shift+T.
Step 6: Write test & runRight click on the test file name to run the test or use keyboard shortcut. TODO - resolve - If you are seeing issues similar to this, rebuild your project a few times to convince IntelliJ to generate the R.java file (which links to all your applications resources) and/or run "mvn install" from the command line.
If you are still having issues, update your Android facet here: