Wednesday, April 24, 2013

Writing an Android GUI using C++

Inline image 2

Introduction

For some reason, we might want to write Android applications using native code. In this case, the NDK will be used. But using only NDK is not enough. Because Android only exports Java interface to programmers, so applications can not call Android classes directly. How do we solve this problem? Using CLE and the Wrapandroid project, programmers can call Android classes through interfaces of CLE more easily.

The article is an introduction to write Android GUI applications using CLE and Wrapandroid. CLE is a middleware for programming using multiple languages, which supports Java, Python, C/C++, Lua, etc., and can be extended to support other languages. Objects, for example, instances of classes, will be maintained in the kernel. And then, CLE provides a common interface to multiple languages. We can call an object's function, get or set object's attributes, capture object's events, etc.

Wrapandroid wraps android classes with CLE objects, which enables programmers to use android classes in their applications. In normal case, the steps is illustrated below:

  1. Create an object of an Android class using MallocObjectL of the CLE interface.
  2. Call object's functions using ScriptCall method.
  3. Override object's function using CreateOvlFunction method
  4. Hook object's event using RegEventFunction method
Step 1: Prepare environment

  • CLE may be installed from the network by the application automatically, you only need to include starcore_android_r6.jar in the project. The file is in starcore_devfiles_r6.zip, which can be download from http://code.google.com/p/cle-for-android.
  • Wrapandroid has lib files: wrapandroid.jar, which can be download from here.
Step 2: Begin programming 

We will be using Eclipse and NDK to develop the application. How to install these, please refer to other related articles. Android version should be above 2.2.

  • Open Eclipse, create a new Android project named "introduction".
  • CLE may be installed from the network when the application is started; in this case, the following permissions should be added:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

  • Copy the Java libraries starcore_android_r6.jar and wrapandroid.jar to the project directory and add them into the project:

Read more: Codeproject
QR: Inline image 1