Wednesday, 31 August 2011

Up and Running With Android Development on Windows

I made a bold statement at a team meeting the other day that you could go from a vanilla windows installation to writing a “hello world” application for android in an hour.
Providing you've a half decent internet connection, here's what you need to do.

1. Install Java

We’re going to develop our app using Java so we need to install the Java SE Development Kit 7  (choose your platform) and download it. Install it using the default options.

2. Install Eclipse

Eclipse is the development IDE we’ll use to develop our app in. (Note to all the Visual Studio users: “We’re through the looking glass here people! Just go with it. It’s fine….”)

Browse to the Eclipse download page and select “Eclipse Classic”.It doesn’t have an installer, it can be simply unzipped and ran so extract the contents of the zipped folder to your "program files" folder. It's nearly 200mb so takes a while to unzip though! Create a shortcut of the Eclipse.exe file on your desktop, then you're good to go.

3. Install the Android Software Development Kit

Download the Android SDK and install it.

NB There is currently a spooky bug in the Android SDK installer: When you install the SDK, you may come across one the wizard screens saying "Java SE Development Kit (JDK) not found".

AndroidSdkSetup_2

Providing of course that you have installed the Java SDK(!), to get round this you need to simply hit the back button, then "Next" again, and the wizard will say its now been found!

AndroidSdkSetup_3


Hit “Next” and select the default destination folder for the Android SDK then continue with the installation. When you reach the end of the wizard, ensure the Start SDK Manager check box is ticked then hit finish:

AndroidSdkSetup_5

This will then fire up the SDK Manager app, allowing you to choose the platform images you wish to download. I just went with the default platforms for the time being. It did take a while to download so please be patient:

You may get a message come up asking you if you want to restart the ABD. Hit “Yes” and you're almost done.

ABD_2

When the packages are installed (above), hit "close" then close down the Android SDK manager. We'll carry on with the configuration from within eclipse now.

4. Configure Eclipse for Android Application Development

Fire up eclipse and select a default workspace.

You know need to install the Android Development Tools(ADT) Plugin. Follow the instructions at http://developer.android.com/sdk/eclipse-adt.html to do this.

After you've restarted eclipse, you need to manually set the location of the Android SDK.

  1. Select Window > Preferences then in the left hand list, select "Android".
  2. Browse to the location of the SDK location we downloaded earlier and hit "Apply". You'll see the SDK target list populate.

NB: There is a bug in how the path to the Android SDK is resolved. If you have installed the SDK to “C:\Program Files (x86)\Android”, you need to enter “C:\Progra~2\Android\android-sdk” if you’re installing to a 64-bit machine or “C:\Progra~1\Android\android-sdk” if your installing to a 32-bit machine:

SDK_Path_Bug2

…more on this issue can be found on Stack Overflow.

5. Adding a Virtual Android Device

Tool_Bar_AVD_Button

When we're developing android applications, we'll want to test them locally before deploying them to a device. To do this, we'll need to create a virtual device that we will run our app on:

  1. Click on the AVD Button (above) on the eclipse toolbar and create a new Virtual Device, imaginatively called "TestDeviceOne".
  2. Select a target O/S of "Android 2.2 - API Level 8" and SD Card size of 1 GiB

It'll take a couple of seconds to create the device but then it should appear in the device list as so:

AVD_Manager_1

6. Creating a Hello World project

Tool_Bar_NewProject_Button
Just to get up an running, we'll start a new Android Project from the toolbar (above) and call the project HelloWorld and select the Build Target as Android 2.2. and we’ll enter a package name of com.helloworld.demo

Tool_Bar_Run_Button

Hit the "Run As" (above) button and select "Android Application" as the way to run the project.

The virtual device will now spin up and load your Hello world app. (NB The virtual device takes a while to get going, but you can keep it open once it's running while you work on your app).

There you have it. More “Hello Worlds” than a Wrox Demo…!

HelloWorld

Let me know if I’ve missed anything out or if you managed to get the process done in less than an hour!

Tuesday, 16 August 2011

Managing Scheduled Tasks With PowerShell

I’ve recently been designing a deployment script for a suite of console applications that will be deployed together. These console apps are ran as scheduled tasks and prior to updating the apps, it’s important to ensure they are not running.

Luckily, it is possible to interrogate and manage the task scheduler in powershell via COM. I’m sure there are specific modules out there but the Task Scheduler Scripting Objects met my needs in this instance:

$server = "localhost"
$ts = New-Object -ComObject Schedule.Service
$ts.Connect($server)

$ts is now an instance of the TaskService object which provides a wealth of functionality when it comes to managing tasks registered in the task scheduler.

Suppose we have 3 tasks declared in the Task Scheduler that you want to check the status of:

image

… (no prizes for guessing what they’ll be doing when they run!), and we want to ensure that none of them are running, before disabling, we can use the following.

function Get-TaskRunningState([object]$taskScheduler, [string]$taskNames){
    [bool]$result = $false
    $taskNames.split(",") | 
            foreach {
                $task = $_
                $res = $taskScheduler.GetRunningTasks(1) | 
                        Where-Object { $_.Name -eq $task }

                if($res -ne $null){
                    Write-Host $task " currently running"
                    $result = $true;
                }
            }
    $result
}

function Set-TaskEnabledState([object]$taskFolder, [string]$taskNames, [bool]$state)
{
    $taskNames.split(",") | 
        foreach {    
            $taskName = $_
            $taskFolder.GetTasks(0) |
                Where-Object { $_.Name -eq $taskName } |
                    foreach { $_.enabled = $state; }
            }
}

$taskList    = "CalculatorTask,NotepadTask,WriteTask"
$server      = "localhost"
$enableTasks = $false

try { 
    $ts = New-Object -ComObject Schedule.Service
    $ts.Connect($server)
}

catch {
  throw "Cannot connect to deployment server."
}

if ((Get-TaskRunningState -taskScheduler  $ts -taskNames $taskList) -eq $false) {
     Write-Host "Attempt to disable services.."

    try {
             $taskFolder = $ts.GetFolder("Martin");
             Set-TaskEnabledState $taskFolder $taskList $enableTasks
             Write-Host "Scheduled services enabled:" $enableTasks
    }
    catch {
               throw "One of the scheduled services could not be disabled."
        }
}
else {
      throw "One or more of the scheduled services are currently running."
}

I declared the functions we’ll use at the top. I concatenated all the names of the scheduled services together and called Get-TaskRunningState, which would return true if any of the tasks were running. Were that the case, we’d just throw a meaningful exception at this stage. If none of the tasks are running, we can call Set-TaskEnabledState passing in the tasklist and the state we wish to set them to (false in this instance). Notice the fact that I had to resolve the Folder that the tasks are in on the scheduler, and pass that in as a parameter to the function. That’s because the TaskFolder has the GetTasks() method on it which is used to find the tasks we’re interested in.

Once I’ve deployed the updated applications, I can simply call Set-TaskEnabledState, this time passing $true for the $state parameter.

More info on the TaskService API can be found on MSDN