Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
0
Student Exercises
EXempty
Introduction
IBM Worklight Studio provides a complete environment for developing mobile
applications in an Eclipse-based integrated development environment (IDE).
It includes coding tools, JavaScript libraries, and an embedded Worklight
Server to facilitate the development of mobile applications that are targeted
to run on multiple platforms, including Android, iOS, BlackBerry, and
Windows Phone.
In the first part of this exercise, you install IBM Worklight Studio from an IBM
Worklight V6.2 Developer Edition package. This product edition is free to
download from the IBM Mobile development website at:
https://www.ibm.com/developerworks/mobile/worklight.html
You configure it for Android development, a platform that is openly supported
on both Windows and Mac OS. Even if you are using a Mac OS computer
and want to develop for the iOS platform only, this exercise demonstrates the
capability of IBM Worklight to support cross-platform development.
Finally, you create a simple application and test it by using the IBM Worklight
Studio browser preview tools and a device emulator.
Optionally, if you have a physical mobile device and the prerequisite
certificate or driver software, you can test the application on your mobile
device.
1-1
Student Exercises
Requirements
This exercise requires that you already have the following software that is
installed on your computer:
Course lab files, which are in C:\LabFiles
A supported JDK:
- IBM Runtime Environment, Java Technology Edition 6.0
- Oracle Java SDK/JRE/JDK 7.0
- Sun Java SDK/JRE/JDK 6.0/1.6 Update 24
Eclipse IDE for Java EE Developers Juno 4.2.2 (SR2)
Firefox v13.0.1 or later if you are using a Windows computer
Xcode 4 or later if you are using a Mac OS computer and want to develop
for the iOS platform
The appropriate provisioning certificates if you intend to test on a physical
device on iOS
You also must have an Internet connection to download the software that is
installed in the exercise, such as the Android Software Development Kit
(SDK).
The specific details on how the course lab files and required software should
be installed are provided in the courses Lab Setup Guide.
1-2
V9.0
Student Exercises
EXempty
Exercise instructions
This exercise is divided into the following parts:
1. Installing IBM Worklight Studio
2. Setting up an Android development environment
3. Developing a simple application
The instructions can be followed on a Windows or Mac OS development computer if it meets the
requirements that are identified in the Requirements section. Depending on whether you are
developing for an Android or iOS platform, some steps might be environment-specific or optional. If
so, they are indicated as such.
1-3
Student Exercises
__ 1.
__ a.
If you are using the Windows virtual machine that is built for this course, double-click the
Eclipse icon on the desktop. Otherwise, start Eclipse by the method that is appropriate
to your computer. The Workspace Launcher window opens and asks you to select a
workspace.
__ b.
__ c.
__ d.
In the Welcome view, click the Workbench icon at the upper right to close it and open
the Java EE perspective in the workbench.
__ 2.
Install the Worklight Studio plug-in from the local archive file.
__ a.
1-4
From the menu bar, click Help > Install New Software.
V9.0
Student Exercises
EXempty
__ b.
On the Available Software page, click Add to add the location of the archive file to the
workbenchs list of known software sites.
__ c.
__ d.
1-5
Student Exercises
__ e.
On the Available Software page, select the IBM Worklight Studio Development Tools
check box. This action selects all of the archive contents:
IBM Dojo Mobile Tools
IBM JQuery Mobile Tools
IBM Worklight Studio
1-6
__ f.
Click Next.
__ g.
The Install Details page opens to allow you to review the items to be installed. Click
Next.
V9.0
Student Exercises
EXempty
__ h.
On the Review Licenses page, select I accept the terms of the license agreements
and click Finish to begin the software installation.
__ i.
When the installation completes, the Software Updates dialog box prompts you to restart
Eclipse to apply the changes. Click Yes to restart Eclipse.
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn
__ 3.
In the Java EE perspective, notice that the toolbar now shows the Create a Worklight
Artifact icon.
1-7
Student Exercises
You successfully installed IBM Worklight Studio. Next, you configure it to enable development
for the Android platform.
1-8
V9.0
Student Exercises
EXempty
__ 2.
On the Get the Android SDK page, click VIEW ALL DOWNLOADS AND SIZES to see all of
the installer packages available by OS platform. The arrangement of the information on the
screen might be different when you do the download.
1-9
Student Exercises
__ 3.
Continue with step 4 if you are using a Windows computer or with step 5 if you are using a
Mac OS computer.
STOP
STOP
Stop
The following instructions are for Windows. Go to step 5 for Mac OS.
__ 4.
For a Windows computer, download and run the Android SDK starter package installer:
__ a.
__ b.
Select the check box to accept the license agreement and click Download.
__ c.
If you get a Security Warning dialog box, click Run. When the download finishes, the
Android SDK Tools Setup wizard starts.
__ d.
On the Welcome to the Android SDK Tools Setup Wizard page, click Next.
__ e.
The wizard checks that a valid Java SDK is installed and displays its location in the Java
SE Development Kit page.
__ f.
Click Next.
__ g.
On the Choose Users page, accept the default selection of Install just for me and click
Next.
__ h.
On the Choose Install Location page, change the entry in the Destination Folder field to:
C:\Program Files\Android\android-sdk
__ i.
Click Next.
__ j.
V9.0
Student Exercises
EXempty
__ k.
__ l.
On the Completing the Android SDK Tools Setup Wizard page, make sure that Start
SDK Manager is selected.
1-11
Student Exercises
__ m. Click Finish. The Android SDK Manager window opens to allow you to select the SDK
packages that you want to install, including tools, specific SDK versions, and other
extras.
__ n.
STOP
STOP
Proceed to the next subsection, Installing an Android SDK platform and the SDK
platform tools.
Stop
The following instructions are for Mac OS. Go to step 6 if you are using Windows.
__ 5.
For a Mac OS computer, download the Core SDK Tools compressed package.
__ a.
__ b.
Use Finder to open the Downloads folder and move the android-sdk-macosx folder
out of it and into the /Users/<yourUserName> folder (the root folder for your user
name). Make sure to remember this new location, as you must specify it when
configuring the ADT tool in the next section.
__ c.
__ d.
In the Are you sure you want to open it? confirmation dialog box, click Open.
The Android SDK Manager window opens to allow you to select the SDK packages that you
want to install, including tools, specific SDK versions, and other extras. Make sure that you
only select the required packages, otherwise the download takes time.
__ a.
Select the Android SDK Platform-tools check box and the check box for the latest
version of Android SDK Build-tools (under Tools).
V9.0
Student Exercises
EXempty
__ b.
Expand the Android 4.4.2 (API 19) check box and select the following components:
- SDK Platform
- ARM EABI v7a System Image
- Google APIs (ARM System Image)
Note
You are encouraged to use the latest Android SDK API level, which is Android 4.4.2 (API 19). In
some cases, an application might require a different API level. You can install as many API levels
as needed for your project.
__ c.
For Windows only, make sure that Google USB Driver is selected under Extras.
__ d.
In total, six packages are selected for a Windows installation, or five for Mac OS. Click
Install n packages (where n is either 6 or 5).
__ 2.
In the Choose Packages to Install dialog box, select Accept License and click Install.
The package installation starts, and the Android SDK Manager Log window opens to show
progress messages.
__ 3.
When the installation completes, the message Done loading packages is displayed. In the
Android SDK Manager Log window, click Close.
__ 4.
Verify that the packages that you selected have the status as Installed in the Android SDK
Manager window.
1-13
Student Exercises
__ 5.
If your C:\LabWork\WorklightWS workspace is not already opened, start Eclipse and open
it.
__ 2.
From the menu bar, click Help > Install New Software.
__ 3.
__ 4.
__ a.
__ b.
__ c.
Click OK. Wait a few minutes for the repository to be accessed over the Internet.
V9.0
Student Exercises
EXempty
__ 5.
On the Available Software page, select the Developer Tools check box and click Next.
__ 6.
The Install Details page opens to allow you to review the items to be installed. Click Next.
__ 7.
On the Review Licenses page, click I accept the terms of the license agreements and
click Finish to begin the plug-in installation.
__ 8.
__ 9.
When the installation completes, the Software Updates dialog box prompts you to restart
Eclipse to apply the changes. Click Yes to restart Eclipse.
1-15
Student Exercises
__ 13. Click the Command Groups Availability tab. Select the Android SDK and AVD Manager
and Android Wizards check boxes.
When you click the Window menu, you see the Android commands.
__ 14. Open the Android SDK Manager and click Window > Android SDK Manager. It shows the
same packages that you installed in the previous section with a status of Installed.
__ 15. Close the Android SDK Manager window.
V9.0
Student Exercises
EXempty
From the menu bar, click Window > Android Virtual Device Manager.
__ 2.
__ 3.
__ a.
__ b.
__ c.
__ d.
__ e.
Click OK.
1-17
Student Exercises
__ 4.
In the Android Virtual Device Manager window, confirm that the virtual device that you
created is labeled with a green check mark, which indicates that it is a valid AVD.
__ 5.
__ a.
__ b.
In the Launch Options dialog box, change the emulator screen size so that it does not
occupy the entire screen.
i.
V9.0
Student Exercises
The Android Emulator window opens and shows the launch progress.
EXempty
__ c.
Wait a few minutes for the virtual device to start. It can take several minutes, depending
on the speed of your computer.
Note
The virtual device emulator window can hide behind the Eclipse workbench. Monitor the Windows
taskbar to see whether it is open. Also, you can get an alert dialog box or warning messages in the
Android Console if a class is deprecated. You can ignore these warnings.
1-19
Student Exercises
__ d.
Wait until the locked screen is displayed, and then click and drag the lock icon to unlock
the screen.
__ e.
Click Home.
__ f.
In the emulator home screen, click the circle icon to see the applications screen.
V9.0
Student Exercises
EXempty
__ g.
In the apps screen, click OK. The screen shows all available applications.
__ h.
Familiarize yourself with the controls in the Android virtual device emulator and then
close the window when you are finished.
__ 6.
Close the Android Virtual Device Manager window so that you can return to the Eclipse
workbench.
You successfully set up an Android development environment, and you are now ready to develop
your first application in Worklight Studio.
1-21
Student Exercises
If your C:\LabWork\WorklightWS workspace is not already opened, start Eclipse and open
it.
__ 2.
From the menu bar, click File > New > Worklight Project (alternatively, you can click the
Create a Worklight Artifact icon in the main toolbar).
__ 3.
__ a.
V9.0
Student Exercises
EXempty
__ b.
Make sure that Hybrid Application is selected in the Project Templates section.
__ c.
Click Next.
1-23
Student Exercises
__ 4.
__ a.
This page also allows you to configure a JavaScript framework to add to your project.
__ b.
Click Finish. The wizard creates a new folder structure for the project and application in
the workspace. It also opens the index.html file in a split view.
__ c.
If you see a dialog box about Rich Page Editor browser compatibility, click OK.
__ 5.
In the Open Associated Perspective? dialog box, click No to stay in the Java EE
perspective.
V9.0
Student Exercises
EXempty
__ 6.
__ a.
In the Project Explorer view, review the folder structure that was created for
HelloWorklightProject:
adapters: This folder contains the source code for all adapters that belong to the
project. It is empty because you did not create any adapters for the project.
apps: This folder contains the source code for all applications that belong to the project.
You see a subfolder for the HelloWorklight application that you created by using the
wizard in the previous step.
bin: This folder contains the artifacts that result from building adapters, applications,
and server-side configuration and libraries.
server: This folder contains configuration information for the server.
- conf: IBM Worklight Server configuration files, such as worklight.properties
and authenticationConfig.xml
- lib: Precompiled libraries that must be deployed to the IBM Worklight Server
__ b.
Now, review the content that was generated for the application.
1-25
Student Exercises
V9.0
Student Exercises
EXempty
1-27
Student Exercises
Information
Each environment on which the application can run must be declared with a dedicated XML
element (for example, <android> or <iphone>). An environment-specific element is inserted
automatically as new environments are added to the project. This action is not shown in the image
because there are no environments declared yet.
- The main HTML file also references the JavaScript files in the js folder,
initOptions.js, main.js, and messages.js.
V9.0
Student Exercises
EXempty
__ 7.
Modify the application to display a formatted Hello Worklight! message with a colored
background by using HTML5 and CSS.
__ a.
In the editor Source view for index.html, replace the placeholder text in the body
<!--application UI goes here-->
Hello Worklight
With the following HTML code:
<div id="messageContent">
<p>Hello Worklight!</p>
</div>
Hint
You can copy and paste from the snippet file from:
C:\LabFiles\Resources\Exercise01\index.HTML.txt
__ b.
Save your changes to index.html and verify that there are no errors. Click the Save
icon, or select File > Save.
1-29
Student Exercises
__ c.
Edit the main CSS file for the application to add a background style sheet and some
formatting for the Hello Worklight! message.
- In the Project Explorer view, open the HelloWorklight > css folder and double-click
main.css to open the file in an editor.
- Add this CSS style selector:
#messageContent {
background-color: aqua;
text-align: center;
padding: 12px;
font-family: sans-serif;
font-size: xx-large;
}
Hint
This code is also available to copy and paste from the snippet file that is in:
C:\LabFiles\Resources\Exercise01\main.CSS.txt
__ d.
Save your changes to main.css and verify that there are no errors.
__ e.
Refresh the Design view of the HTML page to show the effect of your changes:
- Click the editor tab for index.html.
- In the view toolbar, click the Refresh page icon to refresh the preview for the page.
It now shows the Hello Worklight! message in a large font and with an aqua
background.
V9.0
Student Exercises
EXempty
__ 8.
Next, build and deploy the HelloWorklight application to the embedded Worklight Server.
__ a.
In the Project Explorer view, right-click HelloWorklight in the apps folder and click Run
As > Build All Environments.
1-31
Student Exercises
__ b.
In the Project Explorer view, right-click HelloWorklight in the apps folder and click Run
As > Run on Worklight Development Server.
V9.0
Student Exercises
EXempty
In the Project Explorer view, right-click HelloWorklightProject and click Open Worklight
Console.
1-33
Student Exercises
__ 2.
The Worklight Console is displayed within Worklight Studio. Click Preview as common
resources.
__ 3.
You can also access the Worklight Console from a web browser, as described in the
following step.
V9.0
Student Exercises
EXempty
__ 4.
The Catalog page of the Worklight Console lists all deployed applications, and you can use
it to view and test applications.
__ 5.
Click the Preview as Common Resources link. A new browser tab or window opens, loads
the HelloWorklight application, and displays a page with the Hello Worklight! message in
the body.
1-35
Student Exercises
environment to the HelloWorklight application and examine the resulting artifacts that are added to
its folder structure.
__ 1.
In the Project Explorer view, right-click the HelloWorklight application and click New >
Worklight Environment. The New Worklight Environment window opens.
V9.0
Student Exercises
EXempty
__ 2.
In the New Worklight Environment window, select iPhone and Android phones and
tablets.
__ 3.
Click Finish. After a few minutes, the Worklight Console messages pane displays the
message:
Application 'HelloWorklight' with environments [android, iphone] build
finished.
1-37
Student Exercises
The following new folders are added to the HelloWorklight application folder structure:
- An environment folder that is named android
- An environment folder that is named iphone
- An Android project folder that is named
HelloWorklightProjectHelloWorklightAndroid
__ 4.
__ a.
In the Project Explorer view, expand the android folder under the HelloWorklight
application folder.
__ b.
V9.0
Student Exercises
contain web and native resources specific to an Android environment, while the
common folder holds resources shared by all of the application target environments.
EXempty
- native folder: This folder contains all of the application code and resources that are
organized in the structure of an Eclipse Android project so that it can be run in an
Android emulator or device. Worklight Studio automatically creates a separate
Android project that maps to this native folder. The native folder contains
automatically generated Android application code and includes several files and
subfolders.
__ 5.
Review the Android project that was created for the HelloWorklight application. In the
Project Explorer view, expand the HelloWorklightProjectHelloWorklightAndroid
project folder.
Note
Most of the content is the same as the content of the native folder in the application android
folder. As mentioned previously, to simplify development and testing, Worklight Studio creates a
separate Android project for the application and maps it to the android/native folder. This project
can then be used directly to start the Android emulator to test the application.
1-39
Student Exercises
__ 6.
Examine the iphone environment folder (you can skip this step if you are developing for the
Android platform only).
__ a.
In the Project Explorer view, expand the iphone folder under the HelloWorklight
application folder.
__ b.
Now that you added an Android and iOS environment to your application, you can preview it in the
Worklight Mobile Browser Simulator.
V9.0
Student Exercises
EXempty
multiple devices on one page and simulate the Apache Cordova APIs to access native device
functions such as the camera and the accelerometer.
__ 1.
__ a.
In the Project Explorer view, right-click HelloWorklight in the apps folder and click Run
As > Build All Environments. Wait until a message indicates that the build process is
finished:
Application 'HelloWorklight' with all environments build finished.
In the Project Explorer view, right-click HelloWorklight in the apps folder and click Run
As > Run on Worklight Development Server. Wait until a message indicates that the
deployment is successful:
Application 'HelloWorklight' deployed successfully with all environments.
__ 2.
In the Project Explorer view, right-click HelloWorklightProject and click Open Worklight
Console. The Worklight Console now displays information for the iPhone and Android
environments under the HelloWorklight application.
1-41
Student Exercises
__ 3.
A new browser tab or page opens to show the application in the Mobile Browser Simulator.
The application renders on an Android device simulator, and you see the Hello Worklight!
message with an aqua background.
Note
If you click the iPhone link instead, the Mobile Browser Simulator opens in a new tab or page and
renders the application on an Apple iPhone device simulator.
V9.0
Student Exercises
EXempty
__ 4.
Switch to a different Android device: Click Add Device, expand Android, and select HTC
One S (or the device platform of your choice).
The Mobile Browser Simulator displays the two devices side by side so that you can
compare them.
1-43
Student Exercises
__ 5.
Change the device orientation. Click Rotate at the top of the device simulator view. The
simulator rotates the device 90 degrees and renders the Hello Worklight! message.
__ 6.
V9.0
Student Exercises
EXempty
__ 7.
Experiment with the other features of the Mobile Browser Simulator by selecting the
functions that are displayed on the left side of the page. For example, expand Geolocation
to see the settings for that feature.
__ 8.
When you are finished, close the Mobile Browser Simulator window.
1-45
Student Exercises
IBM Worklight Studio integrates with the Android ADT tool and allows you to start an application in
an Android Virtual Device emulator from its generated Android project.
__ 1.
As the application starts, you can view progress messages in the Console pane by clicking
the menu to Display Selected Console and selecting Android.
It can take several minutes for the emulator to load the application.
__ 2.
If you see the lock screen, click and drag the lock icon to unlock it.
__ 3.
V9.0
Student Exercises
EXempty
__ 4.
In the home screen, click the applications icon. The applications screen is displayed. An
icon for the HelloWorklight application is added, allowing it to be started like a native
application.
__ 5.
Click the HelloWorklight icon. The application starts and displays the expected message.
__ 6.
__ 7.
In the Eclipse workbench, the Auto Monitor Logcat dialog box is displayed. Click OK to
accept the default monitoring option.
This action opens the LogCat view in your workbench. LogCat is the Android device output
console where internal application log messages and information are written. It is useful for
debugging applications.
1-47
Student Exercises
__ a.
If you are running Worklight Studio in Eclipse on a Mac OS computer, you can open your
application directly in Xcode from within the workbench. In the Project Explorer view,
right-click the apps > HelloWorklight > iphone folder and click Run As > Xcode
Project.
V9.0
Student Exercises
EXempty
__ b.
If you are running Worklight Studio on a non-Mac OS computer, you must transfer the
application Xcode project compressed file to the Mac OS computer first, and then open
it in Xcode.
Transfer the HelloWorklight-1.0-iphone.zip file that is found in the
HelloWorklightProject > apps > HelloWorklight > iphone > package folder to the
Mac OS computer.
__ 3.
__ a.
In the top toolbar, click the menu that is labeled iPad 6.0 Simulator.
1-49
Student Exercises
__ b.
__ 4.
Click Run.
The simulator window opens and runs the HelloWorklight application, which displays the
Hello Worklight! message.
__ 5.
Click the iPhone simulators home button to see the iPhone springboard screen.
V9.0
Student Exercises
An icon is displayed for the HelloWorklight application that allows it to be started like a
native application.
EXempty
__ 6.
Click the HelloWorklight icon. The application starts again and displays the expected
message.
__ 7.
In Xcode, look at the Debug area in the Console view at the bottom of the window (you
might need to expand it to read it clearly). It contains messages that are logged during the
execution of your application that can be useful when debugging it.
__ 8.
When you are finished exploring it, close the iOS Simulator.
1-51
Student Exercises
__ 1.
__ 2.
If you are developing on Windows, set up your system to detect your device by installing the
appropriate USB driver. You must install a USB driver for adb. For an installation guide and
links to OEM drivers, see the OEM USB Drivers document at
http://developer.android.com/tools/extras/oem-usb.html
Note
__ 3.
__ 4.
__ a.
__ b.
V9.0
Student Exercises
- Select the device on which you want to install and run the application.
EXempty
Click OK.
Before you can use an iOS device for testing, you must provision it for development. This process
involves:
Obtaining a developer certificate that allows you to sign applications.
Obtaining a provisioning profile that identifies your developer certificate, your device,
and the applications you can run on the device.
If you did these prerequisite steps, you can use the following instructions to test your application on
the device.
__ 1.
__ 2.
__ 3.
Click Run. After your code compiles successfully, Xcode uploads and runs your application
on the device.
End of exercise
1-53
Student Exercises