Thursday, 26 February 2015

Monkey Talk integration with Android Studio

Monkey talk is an excellent tool to run feature tests, which simulates the interaction of the user with the application.

Unfortunately it does not has good integration with Android Studio, which reduces the among of users. In this tutorial I am going to show how to do it on more manual way, with a simple project.

1. Add the AspectJ plugin
1.1 Go to the main build.gradle file and add the follow one, inside the dependencies, like this.

classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.5'


1.2 In the build.gradle file of the module "app" add the follow line at the top, like this:

apply plugin: 'android-aspectj'


2. Add Monkey talk library
2.1 Download the latest version of MonkeyTalk Agent from their web page. It is contained inside the IDEs.
2.2 Inside of the module app, create a new folder called monkey-libs and paste the Monkey Talk jar library inside.
2.3 Open the build.gradle file of the app and paste the follow lines.
2.3.1 Paste the follow code inside of the buildTypes:

        monkeytalk.initWith(buildTypes.debug)
        monkeytalk {
            applicationIdSuffix ".monkey"
        }

2.3.2 Paste the follow code inside of the dependencies:

monkeytalkCompile fileTree(dir: 'monkey-libs', include: ['*.jar'])

Here is the example:

3. Include the permissions
Just in case you have not added them, add the required permissions to the Manifest file.
3.1 Open the Android Manifest file.
3.2 Copy the follow line into the manifest file. This will adds the Get task and internet permissions.

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_TASKS" />


4. Run the app
4.1 To run the app, open the Terminal or the console and type the follow:

./gradlew clean installMonkeytalk

This command will compile the app and install it into the existence device connected.

Note1: The method above does not works with the version 1.1.0 of the android gradle tool, but the version 1.0.0.

Note2: I have tested the method above with simple projects but with big projects actually it fails. So, good luck!

Source code:

Source:

Monday, 23 February 2015

Android Robolectric - Solving multidex problem

One of the biggest advantages I see with Android Studio, is it is very easy to deal with the problem when there are more than 65K methods in the apk, usually because too many libraries.

But this causes problems with Robolectric, which results in the follow error:

java.lang.RuntimeException: java.lang.RuntimeException: Multi dex installation failed (/Users/jiahaoliuliu/..../ (Is a directory)).

I have detected this both on Robolectric 2.4 and 3.0. To fix this, do the follow workaround:

1. Create a new Java class which extends the class Application, if you have not done it yet. It could be called MyApplication.

2. Go to the Android manifest, and add the follow line just below application tag as attribute:

<application
    android:name=".MyApplication"
    ... >
    ...
</application>


3. Open the new class called MyApplication and override the method attachBaseContext as follow:

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        try {
            MultiDex.install(this);
        } catch (RuntimeException multiDexException) {
            // Work around Robolectric causing multi dex installation to fail, see
            // https://code.google.com/p/android/issues/detail?id=82007.
            boolean isUnderUnitTest;

            try {
                Class<?> robolectric = Class.forName("org.robolectric.Robolectric");
                isUnderUnitTest = (robolectric != null);
            } catch (ClassNotFoundException e) {
                isUnderUnitTest = false;
            }

            if (!isUnderUnitTest) {
                // Re-throw if this does not seem to be triggered by Robolectric.
                throw multiDexException;
            }
        }
    }

Source:

Thursday, 5 February 2015

Programming as art

Signing GitHub with the keychain from Mac OS X

Mac OS X comes with a feature called keychain, which allow the user to save the password into it and then, authenticate automatically.

This also works with any projects on GitHub, but it need a bit of hand work.

1. Go to your repository folder
2. Open a folder called .git
3. Open the file called config
4. Modify the protocol used for origin to ssh and add the git user to it.

e.g. this is the original code for a simple project on GitHub

[remote "origin"]
        url = https://github.com/jiahaoliuliu/MockitoTest.git
        fetch = +refs/heads/*:refs/remotes/origin/*

And this is the final result
[remote "origin"]
        url = ssh://git@github.com/jiahaoliuliu/MockitoTest.git
        fetch = +refs/heads/*:refs/remotes/origin/*

Tuesday, 27 January 2015

Running Dia on Mac OS X Yosemite

Dia is an excellence tool to draw UML diagrams. Originally it was built for Linux. Later, it is also exported to Windows and Mac OS X.

It could be downloaded from here:
http://dia-installer.de/download/index.html

For some strange reason, it cannot be run normally on Mac OS X Yosemite. To run it, you should run the follow command in the terminal:


DISPLAY=:0 /Applications/Dia.app/Contents/Resources/bin/dia

The another way to run it is set the command DISPLAY=:0 in the running file. Here are the steps:
1. Go to the Application directory
2. Right click on Dia and select "Show package contents"
3. Go to Contents -> Resources -> bin
4. Open dia file with an text editor
5. Remove the block under # Check for X11 and just leave the line which says:

export Display=:0

It will be something like this:

#export PATH="/usr/texbin:/opt/local/bin:/sw/bin/:/Library/Frameworks/Python.framework/Versions/Current/bin:/usr/local/bin:$CWD:$PATH"

# Check for X11
export DISPLAY=:0

startx=`which startx`
if [[ "" == $startx ]]; then
 if [[ ! -e /opt/X11/bin/startx ]]; then
   osascript -e 'tell app "System Events" to display dialog "X11 (XQuartz) is not installed or not running. Would you like to visit xquartz.macosforge.org now in order to download and install XQuartz?" with icon 0'
   if [[ $? -eq 0 ]]; then
     open http://xquartz.macosforge.org
     exit
   fi
  fi
fi

Source:
Technical Blog - Dia Diagram Mac OSX Yosemite Fix

Tuesday, 13 January 2015

Ant - Running a task in a infinite loop

Ant is a task runner which by specifying different targets, it can run it and show the possible results.
Here is a trick to make it run a task in a infinite loop.

1. Download the antelope jar
1.1 Go to the home page of Antelope and download the antelope tasks jar. At the time of written this post the last version is 3.5.1
http://antelope.tigris.org/servlets/ProjectDocumentList?folderID=0&expandFolder=0&folderID=11184

1.2 Copy the jar file into the Ant library. For Mac OS X Yosemite it has the follow path:


/opt/local/share/java/apache-ant/lib

2. Config the Apache Ant build.xml
2.1 Open the build.xml of Apache ant and add the follow sentence on header, in the same line of project sentence:

xmlns:antelope="antlib:ise.antelope.tasks"

2.2 From the task you want to repeat, enclose it with the follow sentences:

<antelope:repeat count="-1">
    <!-- Tasks go here -->
</antelope:repeat> 

Note that optionally you could save the jar file download in the step 1 to another directory. In that case you should execute the ant command with -lib option. For instance:

ant mySuperCoolTask -lib PATH_TO_ANTELOP_LIBRARY

In case you want to just loop a finite number of times, just replace -1 of the step 2.2 with the number of loops you want to run.

The repeat command also has several options. Check the official manual for more information:

Source

Thursday, 8 January 2015

Running Monkey Talk on Android Project with Ant

In this post I explained the steps to do to integer the Monkey Talk with the Android project. Now it is time to explain the steps to make it run with Ant.

1. Download the AspectJ library
1.1 Go to the AspectJ download web page and download the latest stable jar. At the time of written the version is 1.8.4.


1.2 Unzip the Jar and put it on a folder. In Unix based system you just have to type the follow command:

jar xf aspectj-1.8.4.jar

Remember the folder where it is.

2. Generate Apache Ant files.
Create the Build.xml file and ant.properties file with android tool, if you have not done it yet. You can find more reference here:

3. Get the custom_rules.xml
3.1 Download the Monkey Talk package from here if you have not done it:
3.2 Unzip the file.
3.3 Copy the custom_rules.xml file in the samples/android folder to the same folder where Build.xml file were generated before.

4. Invoke Ant
From the console or terminal, type the follow:

ant clean debug -Daspectj.dir=PATH_TO_ASPECTJ_FOLDER

Where PATH_TO_ASPECTJ_FOLDER is the path to the aspectJ folder downloaded on the first step.

Source: