Menu

Building mobile application for Android platform from MATLAB code with TMC Compiler

Shmuel Safonov

Building mobile application for Android platform from MATLAB code with TMC Compiler

TMC Compiler may be used for building a mobile application (e.g. for a smartphone). An example project is contained in
folder /examples/Simo_ADT of the distribution. The applcation's GUI is implemented in Java. It contains a Java class with native methods that are implemented by C code that is generated by TMC Compiler from MATLAB code.
This implementation uses Java Native Interface (JNI) for calling native code written in C/C++ from Java.

Prerequisites

  1. Install Android Development Tools (ADT) as a plugin for the Eclipse IDE from http://http://developer.android.com/sdk/installing/installing-adt.html
  2. Install Native Developement Kit (NDK) from http://developer.android.com/tools/sdk/ndk/index.html

Note: The support of ADT is announced as ending now and the developers are recommended to migrate to Android Studio. However, ADT platform had been selected due to the lack of the native code debugging support in Android studio at this moment.

  1. Open the example workspace <TMC_installation>/examples/Simo_ADT/Simo_ADT_ws/TunerDemo

Native methods: interface between Java and C

The main activity class is defined as

public class MainActivity extends Activity implements Runnable

The class supports the multi-threading: the calculation are performed in separate thread. The class declaration
contains the following definitions for the native methods:

//File src/com/tmc/tunerdemo/MainActivity.java:

public native String  runTmcTuning(float M,float R,float L, float Ts);

public native int pidgetNumFreqs();

public native int pidgetTypeCrl();

public native int pidgetNumPlants();

public native int pidgetGetPlants(double [] a);

public native int pidgetNumCrl();

public native int pidgetGetCrl(double [] a);

and the static code:

static {
    System.loadLibrary("TmcTunerDemo");
}

The first method is called for the calculations and others are called after the calculations finish
to retrieve the results. The C-code contains the native methods implementation based on Java Native Interface (JNI) :

// File : mainTunerDemoJni.c

jint Java_com_tmc_tunerdemo_MainActivity_pidgetNumFreqs(
JNIEnv *env,
jobject callingObject)
{
...
}

JNI code generation

TMC Compiler generates C-code and put it into /jni/ directory of the project. Then the shared library (libTmcTunerDemo.so) is compiled as defined in Android.mk. The code is linked with tmcruntime_minigarmv7.a library that is built using GNU C tool-change for ARM-v7 CPU.

Application debugging

The debugger configuration in Eclipse ADT defines which kind of debugging is enabled:
* Native debugging or
* Java debugging.

Build command in Eclipse is set as
ndk-build NDK_BUILD=1 APP_OPTIM=debug
and AndroidManifest.xml contains <application
android:debuggable="true" attribute.

http://www.coreservlets.com/android-tutorial/ - how to write the Android application, build GUI and organize multi-threading
https://www3.ntu.edu.sg/home/ehchua/programming/java/JavaNativeInterface.html - JNI basics
http://enos.itcollege.ee/~jpoial/oop/lugemist/JDCBook/JDCBook/jnistring.html - more information about data exchange between Java and native code


Related

Wiki: Home

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.