Wednesday, 17 December 2014

Bring an App to front using command line

An easy and simple solution to bring the App to front screen:

adb shell 'am start -n your.package.name/.YourActivityName'

Source
Incise.org - Android development on the command line

Tuesday, 16 December 2014

Configure Jenkins for Android - HockeyApp automatic upload

One of the benefits with Jenkins is with some extra plugins, it could automatise actions such as upload the app to test. This happens with the HockeyApp plugin, which allow the user to upload the compiled file to HockeyApp.

The steps to do it are:
1. Install HockeyApp plugin for Jenkins
1.1 On the main page of Jenkins, click on "Manage Jenkins" -> "Manage Plugins"
1.2 Click on the tab "Available"
1.3 Check the checkbox of "HockeyApp Plugin"
1.4 Click on "Download now and install after restart"
1.5 On the new screen, check "Restart Jenkins when installation is complete and no jobs are running"

2. Create a new app in HockeyApp
2.1 Log in or register yourself in HockeyApp
2.2 On the dashboard, click on "New App". You could fill the data manually or upload an old version of your app.
The new app will have an App ID. Remember where is because Jenkins need it.
2.3 On the top right, click on "Account settings"
2.4 On the bottom left, click on "API Tokens"
2.5 On the "Create API Token" block, select the new app, the right (I have chosen Full access) and enter a name.
A new API Token will be generated at the bottom. Copy it.

3. Config Jenkins
3.1 On Jenkins, go to the configuration page of your app
3.2 On the block "Post-build Actions", click on "Add post-build action" and select "Upload to HockeyApp".
3.3 A new configuration screen will appear. Fill the API Token with the token copied on the step 2.5.
3.4 On the field "Upload Method", select "Upload Version" and paste the App ID created on the step 2.2.
Other options could be set, but with App Token and App ID, Jenkins can upload the app to HockeyApp correctly.

Monday, 15 December 2014

Configure Jenkins for Android - Lint

Lint is a great tool created by Google to analyse the code for Android. It comes with the SDK of Android and it is useful to have good quality code.

Jenkins integrates Lint plugin which could be used to show result of Lint. To run Lint on Jenkins follow the steps:

1. Install Lint plugin on Jenkins
1.1 On the main page of Jenkins, go to Manage Jenkins -> MAnage Plugins
1.2 Go to "Available" tab
1.3 Look for a plugin called "Android Lint Plugin". Check it and click on "Download now and install after restart"
1.4 On the new screen, check "Restart Jenkins when installation is complete and no jobs are running"

2. Configure Jenkins to run Lint
2.1 Go to the configuration of the job on Jenkins
2.2 On the Build block, click on "Add build step" and select execute shell
2.3 Paste the follow command on the dialog

# Run lint
$ANDROID_HOME/tools/lint --xml lint-results.xml .

Make sure that the variable $ANDROID_HOME is defined.

This command will run Lint in the workspace directory and put the result on a xml file called lint-result.xml

3. Configure Jenkins to show Lint result
3.1 In the same page of configuration of the job, look for the block called "Post-build Actions"
3.2 Click on "Add post-build action" and select "Publish Android Lint result"
3.3 Fill the filed called "Lint files" with

lint-results.xml


Configure Jenkins for Android - Sonar

Sonar is a Stand alone server which could be used to analyse the code. Jenkins has specific plugin for Sonar which could trigger sonar to do the code analysis for each build.

1. Download and install Sonar
The first thing to do is download and install Sonar. In its wiki it has a good tutorial about how to download and run it. Just go to the follow page and install Sonar, Sonar runner and try to run it with the sample project:

2. Install plugins for Jenkins
2.1 From the main page of Jenkins, go to "Manage Jenkins" -> "Manage Plugins" -> Available
2.2 Look for a Plugin called "Sonar Plugin" and check the checkbox
2.3 At the bottom of the screen, click on "Download and install after restart"
2.4 On the new screen, check "Restart Jenkins when installation is complete and no jobs are running"

3. Configure Jenkins
Before run the project, Jenkins must be configured to know where is Sonar and the Sonar runner.
3.1 From the main page of Jenkins, go to "Manage Jenkins" -> "Configure system"
3.2 Look for Sonar block, fill the name. It could be as simple as "Sonar"
3.3 In the same block, click on "Advanced"
3.4 Fill the field "Server URL" with the url of the Sonar, it is usually:


3.5 In the same page, look for Sonar Runner block and click on "Sonar Runner Installations"
3.6 Set the name for the Sonar Runner. It could be "Sonar runner"
3.7 In the field "SONAR_RUNNER_HOME", set the path to Sonar runner, which has been downloaded and tested in the first step.
It is also possible install a new version of Sonar Runner by check the checkbox "Install automatically" and set the version of Sonar Runner. The new Sonar Runner will be installed in the SONAR_RUNNER_HOME directory.

4. Configure the job
4.1 In the configuration of the project, in the "Add build step", select "Invoke Standalone Sonar Analysis"
4.2 Let the field "Task to run" blank
4.3 Config JDK = (Inherit From Job)
4.4 There are two ways to config the project properties:
    - By creating a file which contains all the properties
    - By setting the properties directly in the configuration of Jenkins

4.4.1 To save the properties in a file, you must add it to the repository and config the field "Path to project properties". Leave the field "Project properties" unfilled.
4.4.2 To set the properties directly, leave the filed "Path to project properties" unfilled, and use the field "Project properties" to set the properties.
Here is some clue about how to fill the fields and what properties they could have:


Thursday, 11 December 2014

Configure Jenkins for Android - FindBugs

FindBugs is a static code analysis tool which could find potential bugs. It could be installed on Jenkins to run each time there is a change in the code.

1. Download and install the findBugs
1.1 Go to the follow web page to download the last version of FindBugs


1.2 Unzip it and put it on your /usr/lib folder. Call it as findbugs
1.3 Copy the findbugs-ant.jar from the /usr/lib/findbugs/lib to the lib directory of ant. In Mac OS X it ant could be located at /opt/local/share/java/apache-ant

2. Configure Jenkins
2. 1 Install the plugin called ¨FindBugs plugins¨ in Jenkins
2.2 Go to the configuration page of the project
2.3 On the Build section, add the target checkstyle for ant. It could be just after debug and before install target
2.4 On the Post-build Actions section, add a new action called "Publish FindBugs analysis results". The file could be find in the Test/findbugs.xml, where Test is the name of the test project.

3. Modify the Test project
3.1 On the ant.properties file of the test project, add the follow line:

findbugs.home=/usr/lib/findbugs

3.2 If you want to exclude the R folder, create a file called findbugs-exclude.xml in the root directory of the test project and paste the follow code:

<?xml version="1.0" encoding="UTF-8"?>
<FindBugsFilter>
    <Match>
        <Class name="~.*\.R\$.*"/>
        <Bug code="Nm"/>
    </Match>
</FindBugsFilter>

3.3 Add the follow line in the build.xml of the test project, at the end of the file and just before the last line, which is </project>

    <taskdef name="findbugs" classname="edu.umd.cs.findbugs.anttask.FindBugsTask"/>
    <target name="findbugs">
        <mkdir dir="reports" />
        <findbugs home="${findbugs.home}" output="xml" outputFile="findbugs.xml" excludeFilter="findbugs-exclude.xml">
            <auxClasspath path="${project.target.android.jar}"/>
            <class location="${tested.project.dir}/${out.dir}" />
        </findbugs>
    </target>

Add the value for tested.project.dir in the ant.properties file if you have not done it before. It is value is the directory where the source project is.

Source
Jenkins - Building an Android app and test project

Configure Jenkins for Android - CheckStyles

Google has a very nice coding style guide published in the web page. It is called "Code Style Guidelines for Contributors" and it could be found here.

Jenkins could be configured to do automatic coding style check on each run. Here is a short guide about how to do it.

1. Download and get the jar
1.1 Go to the web page of Check style in SourceForge:


1.2 Download the jar folder and remember where is.

2. Configure the Jenkins for CheckStyle plugin
2.1 Opens Jenkins
2.2 Go to Manage Jenkins -> Manage plugins
2.3 From the Available tab, look for a plugin called "CheckStyle Plugin". Download and install it. Make sure that another plugin called "Static Analysis Utilities" is also installed.

3. Configure the project
3.1 From the test project, add the follow line in the ant.properties

checkstyle.jar=/usr/lib/checkstyle/checkstyle-6.1.1-all.jar

In my case the plugin is stored in the /usr/lib/checkstyle folder. Change it to the folder where your jar file is.

3.2 Download the google_check.xml from the follow url and place it in the root folder of the test project.


3.3 Add the follow line at the end of the build.xml in the test project, just before the line which ends with </project>

    <taskdef classpath="${checkstyle.jar}" resource="checkstyletask.properties" />
    <target name="checkstyle">
        <checkstyle config="google_checks.xml">
           <formatter type="xml" tofile="checkstyle-result.xml"/>
           <fileset dir="${tested.project.dir}/src" includes="**/*.java"/>
       </checkstyle>
    </target>

The code above creates a new target called checkstyle, which when runs, it gets the file called "google_checks.xml", which should be in the same folder as the build.xml is, and check all the java files in the source folder of the project to be test. Make sure you have set correctly the variable for "test.project.dir" in the ant.properties file.

4. Config the job
4.1 On Jenkins, go to the configuration of the project
4.2. On the Build section, add the target "checkstyle" in the Invoke Ant part. The mine looks like this:

uninstall clean emma debug checkstyle install test

*If you are not using emma, just remove the target emma.

4.3 On the Post-build Actions section, add a new action called "Publish CheckStyle analysis results"
4.4 Enter the path for the checkstyle-result.xml. It should be something like:

TestProject/checkstyle-result.xml

Where "TestProject" is the name of the testing project, not the source project.

Finally, to check the result, on the left side of the project, along with "Build", "Configure" and other options, there should be a new option called "Checkstyle Warnings", where you can see all the warnings about the coding style.

Happy coding!

Source
Jenkins - Building an Android app and test project
Github - CheckStyle

Tuesday, 9 December 2014

Android test projects - little tweak to run it on Jenkins

For big tests, instead have the test in the same project as the original one, it is better to have a separated project. It will make the code more clean and easy to read.

To do it so, just do the follow on eclipse:
1. Click on File -> new project -> Android Test project
2. Enter the test project name and click on next
3. Select the existent project which the tests should run and click on next
4. Select the Build target and click on Finish

But this is not good enough to run in the Jenkins. Two more steps should be done.
1. Create the ant files
1.1 Go to the sdk directly of Android and in the directory tool, run the command android as the follow one to create the proper files for the test

./android update test-project -m <path-to-project-to-be-tested> -p <path-to-test-project>

Replace the <path-to-project-to-be-tested> and <path-to-test-project> with your own path. It could be like this

./android update test-project -m /User/jiahaoliuliu/projects/coolProject -p /User/jiahaoliuliu/projects/coolProjectTest

This command will create the follow files in the test project directory:
- ant.properties
- build.xml
- local.properties

2. Update the relative path
2.1 Go to the directory of the test project
2.2 Open the ant.properties file
2.3 Check the path in there is relative and not absolute. The relative path starts with one dot(.) or two dots(..). The absolute path starts with slash(/). If it is the absolute path, change it to the relative path.

For more information