Harbour and Android

Although I will tell here about programming on the Harbour for Android, this material may be interesting for those who write in other languages. Solutions that are considered here are universal and can be used in other programming systems, if they can be built from sources to the so - library under Linux and have a C API, and, of course, can be used by C/C++ developers.

The purpose of this article is to investigate the possibility to write programs for Android with the language of your choice, in this case, with Harbour.

First, a little about what Android is from a developer's perspective.

This is an operating system from Google for mobile devices - smartphones and tablets.

It is based on the Linux kernel compiled from Linux sources for mobile devices.

The next layer is the Java virtual machine, which is called Dalvik - it is optimized for mobile devices. This is a byte codes interpreter, which has the structure, similar to other analogical interpreters, including our Harbour VM.

At the same structural level are system libraries, including the standard C library, OpenGL ES - the 2D and 3D graphics engine, SQLite.

Next, the application framework, it is a very features rich set of APIs for developers.

And, finally, the very custom applications that we write and use.

The native programming language for Android is Java, it is a Java code, which is executed by a Dalvik virtual machine. But Java allows you to connect modules written in C and C++ - like Harbour, it has an appropriate API, it is called JNI - Java Native Interface. The C modules are called native in Java terminology. In fact, a program for Android can be written entirely in C; in this case, the components of the graphical interface need to be implemented independently to draw them "manually". Besides, it is the way, which game developers use often for not rewriting existing engines in Java.

Development tools for Android

All the main tools for developing applications for Android are free and available for free download from the official resources.

First of all, it's Java Development Kit - JDK. You need to download it anyway, even if you will write pure native applications. The JDK includes not only the Java compiler, but some other tools, necessary to create the final product - apk - package, ready for download on the Android device. Download it from the Oracle website.

Next - Android SDK Tools (Software Develpment Kit) in fact, the set of tools for developing for Android. You can download the full set, including IDE - Android Studio, or just the SDK Tools. For our purposes, Android Studio not necessary - we will write programs in Harbour, and to build apk with the help of command line tools, at least, until we make our IDE for this. After installation of the Android SDK Tools you need to run the SDK Manager and install some components - Platform-tools, Build tools, a desired version of SDK platform, for example, the last (although I have installed 4.4.2 ), the emulator, which is a very useful thing for debugging, Support library, you may need also the Google USB Driver - if you will be installing your app to the device via USB.

And finally, Android NDK - Native Development Kit, the tools for developing native applications. We will need it, because we will write not in Java, or, not only in Java.

So, the points again:
  1. Download JDK, unpack ( in Windows by default - C:\Program Files\Java\jdk1.7.0_71if it is JDK7 ).
  2. Download the Android SDK Tools, unzip it wherever you like, for example, c:\android\sdk, run the SDK Manager and install Platform-tools, Build tools, emulator, ...
  3. Download Android NDK, unzip it wherever you like, for example, in c:\android\android-ndk-r10d

Which way to go further?

(We will look at ways of creating Harbour applications for Android)

I must say that, in any case, we will use the JNI - a special APIfor connecting C/C++ modules to Java programs. JNI allows you to call C functions from Java and vice versa, Java methods from C. Fortunately, we have sources of the Harbour and we can to create a libharbour.so dynamic library, which can be connected to Android application. This library contains the Harbour virtual machine executing compiled Harbour code and Run-time library of the Harbour with all the familiar features. Therefore, all that can be done using native C code, can be done from Harbour program, it will also be "native" in the terminology of Java/Android and can be connected via JNI.

In many cases native modules are used to implement certain functions only. They are called from Java programs, produce some calculations and return the result. This is done in cases when the programmer expects to receive a significant gain in performance or if the corresponding module on C he already has and he doesn't want to rewrite it in Java. But this is not our case. With this approach, you need to write every time a certain amount of a Java code, but we want to be able to write programs, including the user interface, only in a language of our choice, in this case - in Harbour.

There are at least 3 ways of achieving our objectives

1) "native" interface. All writing in Harbour. The interface in this case it is necessary to "paint by hands" with, for example, OpenGL - i.e., you need to create appropriate library as a wrapper around OpenGL. Most likely, this is the approach implemented in the frameworks based on QT ( not sure at 100%, because I'm not closely familiar with these products, but I think that it is so ). Another similar option is to write a console application, and to redirect the console i/o, as it is done, for example, in the Terminal for Android - there is an Open Source Android terminal project.

2) the Web interface. Using of WebView to build the interface. There is such Android GUI element - WebView - a built-in browser. It is possible to load a web page into it by specifying the url, or a string with html content, which can be taken from the previously prepared file or be generated dynamically. This string can contain css and javascript that allows you to use all the wealth and variety of modern web interface. And another very important point: it is possible to "build a bridge" between javascript code loaded in a WebView and Java-application code, and, therefore, "native" code - to call from javascript the functions and methods implemented in our application and to get the result of their execution. I.e., we can use the web interface as the interface of the application, handle events from the input elements, etc.. This approach is used in many frameworks, for example, in PhoneGap and rfoBasic. The implementation of this alternative in Harbour is presented here: see h4aFirstApp.

3) HDroidGUI. I gave to this item the name of my framework, which implements this approach. This way uses natural ( don't confuse with "native"! ) components of the Android GUI. They are created dynamically by means of Java code on the basis of "descriptions", created in Harbour program. The framework includes a set of Java classes which are able to create interface components on the basis of the "descriptions" of a certain format and a set of Harbour classes, who create these "descriptions". Harbour programmer creates an interface using familiar syntax, similar to that, used in HwGUI and other GUI libraries. Harbour classes of our framework create a string, describing this interface and send it to Java-methods of the framework, which creates the GUI components of Android. Read more about HDroidGUI here.

As you can see, I have chosen the third approach, although paid attention to the second. First, I generally prefer to use the native interface on each platform. So, HwGUIfor Windows uses WinAPI, and for Linux - GTK. Native Android interface carefully designed, has rich possibilities, so I think it is the better option. The approach with the web interface, with all its attractiveness, adds the javascript, css and, maybe, some javascript framework like jquery to the list of languages that you need to use when developing an application, which complicates the programming process. As to the first option, you need to write a library that implements GUI, not too much inferior to the native, and it is a very time consuming task. Using of QT is not an option for me - it is too "heavy" for my taste and the implementation of even a small application will result in a multi-megabyte package.

I would like to reiterate that while I am talking mainly about the use of the Harbour, in its place can be the code written in another language, the same C, for example.

In conclusion, I will say that there is another possible solution to our problem - the implementation of a possibility to write in Harbour for Android. This method does not use "native" code and JNI and is implemented in such a known commercial product, as Basic4android. It uses a special compiler, which converts the source code in some language ( Harbour, Basic,... ), into the Java code. That is the way, too...