Notes: Get Robolectric to work on Android Studio (RC) with Gradle

Disclaimer

This is obviously not a how-to, this is just a post dumping my notes. This will serve as personal reminder, and if it benefits others then your welcome.

Test Environment

  • Oracle JDK 1.7.0u67
  • Android Studio 1.0.0 RC4
  • Gradle 2.2.1

Setup

  1. In your root build.gradle, define the additional repo under buildscript > repositories as well as in your allprojects > repositories blocks.

    maven {
        url 'https://oss.sonatype.org/content/repositories/snapshots'
    }
    
  2. Add a classpath dependency to the robolectric-gradle-plugin under buildscript > dependencies blocks.

    classpath 'org.robolectric:robolectric-gradle-plugin:0.14.+'
    
  3. Apply the plugin (after applying Android Gradle’s plugin) in your build.gradle of your target module.

    apply plugin: 'robolectric'
    
  4. Define the test instrumentation runner in your android > defaultConfig-block.

    testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    
  5. Define the path where all our tests are located under android-block.

    sourceSets {
        androidTest {
            setRoot('src/androidTest')
        }
    }
    
  6. Define the robolectric-block in which to choose what to include and exclude.

    robolectric {
        include '**/*Test.class'
        exclude '**/espresso/**/*.class'
    }
    
  7. Define Android test dependencies under the dependencies-block.

    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    
    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }
    
    androidTestCompile('org.robolectric:robolectric:2.4') {        
        exclude module: 'classworlds'
        exclude module: 'commons-logging'
        exclude module: 'httpclient'
        exclude module: 'maven-artifact'
        exclude module: 'maven-artifact-manager'
        exclude module: 'maven-error-diagnostics'
        exclude module: 'maven-model'
        exclude module: 'maven-project'
        exclude module: 'maven-settings'
        exclude module: 'plexus-container-default'
        exclude module: 'plexus-interpolation'
        exclude module: 'plexus-utils'
        exclude module: 'wagon-file'
        exclude module: 'wagon-http-lightweight'
        exclude module: 'wagon-provider-api'        
    }
    
    androidTestCompile 'com.squareup:fest-android:1.0.+'
    
  8. Define the output folder for Android Studio.

    apply plugin: 'idea'
    
    idea {
        module {
            testOutputDir = file('build/test-classes/debug')
        }
    }
    

Triple-Boot Ubuntu, Fedora, and Vista

So here, I will share my experience when I did my first triple-boot to give you an idea on where to start.

I used my laptop, with a 80GB hard drive, partitioned it into 1 primary and 1 extended partition, dividing up the extended partition further more. I used Fedora 10, Windows Vista Home Basic, and Ubuntu 9.04 respectively.

Primary Partition #1 (ntfs, 20.00 GiB)
Extended Partition #2 (extended, 36.52 GiB)

Logical Partition #4 (ext3, 8.00 GiB) – Ubuntu
Logical Partition #5 (linux-swap, 1.00 GiB) – Ubuntu Swap
Logical Partition #6 (ext3, 8.00 GiB) – Fedora
Logical Partition #7 (linux-swap, 1.00 GiB) – Fedora Swap

So basically you have 7 partition all in all. One thing to notice is that I’ve created a swap for each linux distro, it is not necessary to do it because I later found out that both distros use both swaps making my swap a total of 2GB.

Now, I installed Vista first so I wouldn’t have a problem later on restoring my GRUB. Next is install Ubuntu, basically you do as you do it on dual boot. Lastly is Fedora, there are 2 options, use Fedora or Ubuntu’s GRUB. Each are the same, but I preferred using Ubuntu’s GRUB (I didn’t install the Fedora’s GRUB during its installation). Then you add Fedora’s boot partition in Ubuntu’s /boot/grub/menu.lst like the following below,

title Fedora Core 10, 2.6.27.5-117.fc10.i686
root (hd0,7)
kernel /boot/vmlinuz-2.6.27.5-117.fc10.i686 ro root=UUID=0a5ed460-5d07-40b1-9ca1-50ff10214c80 rhgb quiet
initrd /boot/initrd-2.6.27.5-117.fc10.i686.img

All in all, your menu.lst should look something like this,

title  Ubuntu 9.04, kernel 2.6.28-11-generic
uuid  df1599f4-12b7-4419-87de-e4c9c27799d4
kernel  /boot/vmlinuz-2.6.28-11-generic root=UUID=df1599f4-12b7-4419-87de-e4c9c27799d4 ro quiet splash
initrd  /boot/initrd.img-2.6.28-11-generic
quiet

title  Ubuntu 9.04, kernel 2.6.28-11-generic (recovery mode)
uuid  df1599f4-12b7-4419-87de-e4c9c27799d4
kernel  /boot/vmlinuz-2.6.28-11-generic root=UUID=df1599f4-12b7-4419-87de-e4c9c27799d4 ro  single
initrd  /boot/initrd.img-2.6.28-11-generic

title  Ubuntu 9.04, memtest86+
uuid  df1599f4-12b7-4419-87de-e4c9c27799d4
kernel  /boot/memtest86+.bin
quiet

### END DEBIAN AUTOMAGIC KERNELS LIST

# This is a divider, added to separate the menu items below from the Debian
# ones.
title  Other operating systems:
root


# This entry automatically added by the Debian installer for a non-linux OS
on /dev/sda1
title  Windows Vista Home Basic, 6.0:build6000
rootnoverify (hd0,0)
savedefault
makeactive
chainloader +1

title Fedora Core 10, 2.6.27.5-117.fc10.i686
root (hd0,7)
kernel /boot/vmlinuz-2.6.27.5-117.fc10.i686 ro root=UUID=0a5ed460-5d07-40b1-9ca1-50ff10214c80 rhgb quiet
initrd /boot/initrd-2.6.27.5-117.fc10.i686.img

Now, you should have a successful triple-boot! Also, now you can restore your GRUB using each of their Live CDs.

This method can be used with any combination you like and number of OSes, but just limit to your hard drive capacity. Having multiple OSes is cool, but to much is just plain dumb. =)

Applications Menu Missing

Today, I was experimenting with my Ubuntu, when I noticed that my Applications menu in my panel had no contents, only Places and System. I did a little search and found a solution in the Ubuntu forums. To restore it back, issue the following commands in the terminal.

rm /home/<your username>/.config/menus/applications.menu

This will remove your current configuration, and reset a new config. After that, your Applications menu should be restored back to default.

Installing OpenOffice 3.1 in Ubuntu/Debian

OpenOffice 3.1 has been released with some cool features that we so long waited. The way to install it, is pretty much the same when you installed OpenOffice 3.0. So to start, I have Ubuntu 9.04 as my OS, other Debian-based OSs and Ubuntu derivatives are applicable too.

  1. Uninstall your previous version.

    sudo apt-get remove --purge openoffice.org-*
    rm -rf /home/[yourUsername]/.openoffice.org
    
  2. Of course, download OpenOffice 3.1 first (in my case OOo_3.1.0_LinuxIntel_install_en-US_deb.tar.gz).

  3. Next, launch the terminal and extract the package.

    tar -xvzf OOo_3.1.0_LinuxIntel_install_en-US_deb.tar.gz
    
  4. Now, enter to its directory by issuing the following commands.

    cd OOO310_m11_native_packed-4_en-US.9399/DEBS/
    
  1. Install all the packages inside.

    sudo dpkg -i *.deb
    
  2. Next, install the shortcuts.

    cd desktop-integration
    sudo dpkg -i *.deb
    
  1. Now you should have OpenOffice ready in your menu.

These are the steps I made for me to complete my installation without conflict. Of course there are other methods like from the repository, but yesterday when I installed mine, the update was still not uploaded. Also, you may notice that the splash screen is still on OpenOffice 3.0, its normal, maybe they just forgot to change it.

Get Dropbox to work on Jaunty Jackalope

I did a fresh install yesterday, and I got a problem on installing the latest Dropbox. The error was like this,

The program dropbox received an X Window System error.
This probably reflects a bug in the program.
The error was BadIDChoice (invalid resource ID chosen for this connection).
(Details: serial 734 error_code 14 request_code 53 minor_code 0)
(Note to programmers: normally, X errors are reported asynchronously;
that is, you will receive the error a while after causing it.
To debug your program, run it with the --sync command line
option to change this behavior. You can then get a meaningful
backtrace from your debugger if you break on the gdk_x_error() function.)

To get it working, I did the following

  1. Install Dropbox’s nautilus and daemon package

  2. Download latest build of Dropbox (as of April 28, 2009), here

  3. Issued the following command on the terminal

    cd ~        
    rm -r .dropbox-dist/        
    tar -xzf dropbox-lnx.x86-0.6.510.tar.gz
    
  4. Restart nautilus (I did a logout)

and voila! Dropbox is now working!

Credit goes to Adam Michael Roach, I modified it to fit the current versions and releases.