Disclaimer

The demo is provided only as a technology preview of COBOL code operating on an Android emulator or real device. Micro Focus cannot commit to providing support for the Android platform in the future and there are no bug fixes at the moment. The demo was created exclusively for the Micro Focus Community to use and provide feedback to help determine whether Micro Focus should support the Android platform in the future. Limitations include:

  • This demo uses a version of the JVM COBOL run-time system modified to work better on Android.
  •  The COBOL File Handler is not supplied so it is not possible to use COBOL files. You can use Android's storage APIs to save data, and the demo application uses the Settings API to store its configuration.
  • Some COBOL statements may not work correctly.
  • If you change the code, you may receive Dalvik verifier or other run-time exceptions. Though we do not provide technical support for bugs with COBOL on Android, please post on the Micro Focus Community Web site any issues you may encounter.

Please review the add pack readme for more information.

COBOL and Android

Recently we’ve demonstrated Visual COBOL running on Android devices in several forums, including the R3 launch in January 2011. It’s one of the things that seem to fire the imagination of customers and staff alike, and we’ve had lots of requests for more information.

We’ve decided to make the sources for this demo available on the Micro Focus Community web site, and this is a short article to explain how you can get the demo up and running.

Introduction

The COBOL Android demo is a proof of concept which shows COBOL code running directly on an Android emulator (part of the Android SDK) or on an actual Android device. The application does an interest calculation to provide a quote for a loan. The quote can optionally be sent back to a web page using URL query syntax – but the default web server is an Azure instance which is no longer running. You can change the URL to a web server you supply if you want to see this part of the application run. 

It consists of three Eclipse projects:

  • InterestCalculatorClient

    A Java project created with the Android Developer Toolkit for Eclipse. It consists of the UI code for the application, and because it is an Android project makes it easier to package the application up for deployment to an Android emulator or device
  • InterestCalculator

    A COBOL JVM project which has a small procedural program which can carry out an interest calculation using high-precision decimal arithmetic. This project also has a COBOL class which interfaces between the procedural COBOL code and the Java client which provides the UI.
  • QuoteCommunicator

    A COBOL JVM project with some code for sending a quote back to a web server. The actual web server referenced is an Azure instance hosting a service written in COBOL, but it is not actually running anymore – you can change the URL to a web page of your choice if you want to see this running. It also has code that will access a device’s GPS to send back location information

Android itself does not use a regular JVM like the ones supplied by Oracle or IBM, but a special JVM optimized for small memory footprints and limited resources, called Dalvik. The actual format of .class files for Dalvik is completely different to regular JVM. When you upload a Java application to Dalvik using the Android SDK, the regular Java byte code is converted to Dalvik format and packaged into an .apk file before upload.

Because the Android tools work with regular JVM files, we are able to upload COBOL to Dalvik in the same way as Java. But Dalvik is not the same as a regular JVM and currently there are some things in COBOL programs that currently do not work when translated to Dalvik; you will see runtime errors in the Android Eclipse LogCat window .

In addition, debugging COBOL in Dalvik does not work as well as it does for regular JVM. You can see the variables changing value in the variables window, but breakpoints are ignored and the execution point does not reposition correctly as you step through. Because of these sorts of issues, you should regard the Android support being demonstrated here as a Technology Preview to show you what is possible. It is not currently a supported product.

Before you start

To run the demo you need to install the following:

  • Visual COBOL for Eclipse for Windows, R4 Update 2.
  • AndroidForCommunity AddPack – downloadable from SupportLine (Visual COBOL AddPacks section).
  • The Android SDK (available from Google, here)
  • The Android Developer Tools (ADT) for Eclipse. Follow the instructions here.

Once you have all these installed, you need to install at least one Android platform in your SDK – the one you need to run the demo is for API Level 8. To do this, open the Android SDK Manager, and download the Android 2.2 (API 8) SDK Platform.  The SDK manager shows the SDK Platform for all the Android APIs released so far, but by default the only platform actually downloaded will be the latest one. Select Android 2.2 (API 8) SDK Platform and Samples, and click the Install Packages button.

You also need to create a new Android emulator (or Android Virtual Device). If you haven’t used the Android SDK before, Google’s Hello World tutorial will take you through these steps.

Your emulator should target API level 8 or later to work with the COBOL Android Demo – level 8 is the API supported by the majority of Android devices in use at the time of writing. Tablet devices running Honeycomb are at API level 11. Android API levels are always backwards compatible – level 8 is the minimum API required to run this demo so higher levels should also work, but we can’t guarantee this as we haven’t tried them all.

Preparing the Eclipse workspace

Now you can import the demo projects into Eclipse.

  1. Download the demo from SupportLine and unzip it to a directory on your computer somewhere.

You should end up with four sub directories under the CobolAndroidDemo directory:

Android
InterestCalculator

QuoteCalculator

RTE

  1. Start Eclipse, and create a new empty workspace (you can use an existing workspace, but if you have a clean workspace you might find any problems easier to diagnose).
  2. You need to specify the location of your Android SDK before you can build or run any Android projects:
    1. Click Window, Preferences to display the Preferences dialog.
    2. Select Android from the list on the left.
    3. Browse to the location where you installed your Android SDK (on Windows by default this is C:\Program Files\Android\android-sdk or C:\Program Files (x86)\Android\android-sdk on 64-bit Windows).
    4. The SDK should display a list of the target APIs you have defined for the SDK.
    5. Add a workspace classpath variable to point to your CobolAndroidDemo directory:
      1. Click Window, Preferences to display the Preferences dialog.
      2. Select Java, Build Path, Classpath Variable from the list on the left.
      3. Click the New button to display the New Variable dialog, and enter CobolAndroidDemoRoot into the Name field.
      4. Click Folder and browse to your CobolAndroidDemo directory, click OK on the Folder selection dialog and click OK on the New Variable dialog.
      5. Import the projects into your workspace:
        1.  Click File, Import and then select General, Existing Projects into workspace, Next
        2. In the Select root directory field, Browse to the location where you installed the projects and click Finish.

You now have three projects in your workspace, InterestCalculator, InterestCalculatorClient and QuoteCommunicator. If your workspace is set to build automatically (the Eclipse default) the projects will be built within a few seconds. There should be no warnings or errors in the Problems window if everything is set up correctly. The likeliest cause of problems is a failure to set up the classpath variable correctly – the imported projects need to be able to find a special version of the COBOL runtime to work with Android, and this is in the RTE directory. The classpath variable should point to the root directory of the demo and all the projects extend this variable into the RTE directory.

Running the demo

At this point you should have the projects all imported and built correctly, so you can run it on the emulator you set up in Before you start. Right-click on the InterestCalculatorClient project and click Run as, Android Application. This starts up the Android emulator and uploads the InterestCalculatorClient.apk package file. It can take up to 30 seconds for the emulator to start and for the package to be uploaded – if you display the Android console (available from the Display Selected Console icon on the right-hand side of the Eclipse console window) messages will show you the progress.

Once the application has started running it looks like this:

You can enter different values in the fields, and click Illustration to see the result.

To change the settings for the application, click the emulator’s Menu button and then click the Settings button that appears at the bottom of the application. The application now looks like this:

You can change the URL for saving quotes from the supplied default, and also select the name of a different loan advisor from the drop down list. Click Apply to save your changes and return to the application screen. These changes persist until you delete the application from the emulator or device it is installed on.

When you click Illustration, the Save Quote button is enabled, and clicking this takes you through to the Save Quote screen:

When you click Send to cloud, the application makes an HTTP request to the web page specified in the settings screen, appending a query string with all the parameters for the quote, as well as the longitude and latitude from the GPS device if one is available. This is the format of the query string:

name=customer_name&term=loan_term&rate=interest_rate&principal=capital_amount&total=final_sum&comment=comments&phone=advisors_phone&location=(latitude longitude)&customerphone=customer_phone

You can see how this string is built up in the DataSender class in the QuoteCommunicator project.

The application design

The diagram below shows the three projects and the main classes in the application. It does not show all the classes.



 

 

The UI is in the InterestCalculatorClient project on the left. Both these classes are Android activities which display the UI defined in the project’s res/layout folder. The InterestCalculator project contains the code which actually does the calculation, inside the ProceduralCalculator program (ProceduralCalculator is not a class, it is procedural COBOL which could have come from a green screen application where it would have been compiled as native code.

Making changes to the application

You can modify the supplied application using Visual COBOL, or even write your own new COBOL code for deployment. Remember though, Visual COBOL Android support is only a technology preview – it does not even have the status of a beta product at the moment.

There is no access to the COBOL file handler, so you can’t use COBOL files (you can use Android’s storage APIs to save data, and the application uses the Settings API to store its configuration). Also, not all COBOL statements will work correctly or at all – don’t be surprised if you get Dalvik verifier and other runtime exceptions when you try to run code you have changed. Please let us know about things you find that don’t work as expected, although at the moment we are not making a formal commitment to fix them.

Running the demo on a real device

You can connect an Android ‘phone or tablet to your computer via USB in order to install the demo on it. See Google’s  Using Hardware Devices for more information. On Windows computers you need to follow their instructions on installing the USB driver – which can differ slightly from one device to another. On the device itself, you need to enable USB Debugging.