Skip to content

This is an example of using matlab function from android applications through android studio, following the instruction, one can integrate supported matlab function to speed up your Android app.

Notifications You must be signed in to change notification settings

ZijingMao/MatlabIntegretMulApp

Repository files navigation

Integrate Matlab Function Into Android App

###Required software and tools

Note: NDK required to be installed first in Android studio Matlab coder required to be installed first in Matlab

###Step 1. Matlab to C/C++ code

Write simple multiplication matlab code (see .\matlab_source_code)

% test function
function [ c ] = matrix_multiplication( a, b )
    c = a * b;
end

% test script
a = [1, 2, 3; 4, 5, 6];
b = [1, 2; 3, 4; 5, 6];
c = matrix_multiplication(a, b);

Convert Matlab code to C code using coder (see ...app\src\main\jni\src\MatrixMultiplication) Remember to generate C code at the last step.

See Ref. [1-4]

###Step 2. C/C++ code with JNI wrapper

  1. Create jni folder under main folder
  2. Put C/C++ code into jni folder
  3. Write Android.mk & Application.mk file following given references
  4. Write interface file (in this example is: libmatrixmultiplication.i) following given reference
  5. Execute command line to compile C/C++ to .so using NDK (this will auto generate .so file into ...app\src\main\libs\armeabi)
>$cd app\src\main
>#exec swig with interface to generate swig wrapper for C/C++
>$c:\swigwin-3.0.8\swig.exe -java -package com.matrixmultiplication -outdir ..\java\com\matrixmultiplication -o libmatrixmultiplication_wrap.c libmatrixmultiplication.i
>#link all source and header based on the configuration of Android.mk file
>#notice the include $(BUILD_SHARED_LIBRARY) should be at last to prevent errors, if error still exist, see _ref[9]_ and _Potential Problems for NDK_
>$c:\NVPACK\android-ndk-r10e\ndk-build

See Ref. [5-10]

###Step 3. Call .so lib from Android Studio

  1. Configure your build.gradle & gradle.properties following ref[6,9] will most likely avoid building errors, if there still exists please google, should be easy/common problems
  2. Call the compiled library using code under your wanted android activity or service:
    static {
        System.loadLibrary("MatrixMultiplication");
    }
  1. Use the line to call function inside .so lib in onCreate or wherever you desired
    // DON'T FORGET TO IMPORT YOUR PACKAGE
    MatrixMultiplication.matrix_multiplication(a, b, c);

Note: If you want to use more straightforward JNA library, please refer to ref[11-12]

####Reference

  1. Matlab coder with iPhone & Android video tutorial
  2. Matlab coder with Android code example
  3. Matlab coder video tutorial (Chinese)
  4. Matlab coder document
  5. SWIG and Android
  6. Android Studio, gradle and NDK integration
  7. Compiling and Using a C++ Library on Android with Android Studio
  8. Wrapping C++ code with JNI
  9. Android NDK in Android Studio with SWIG
  10. Wrapping C arrays with Java arrays
  11. Java Native Access(JNA)
  12. Easier way to access native code

####Potential Problems for NDK

Notice: Normally the problem of not able to generate .so file is because Application.mk.

If the problem is like:

error: vector: No such file or directory error: iostream: No such file or directory error: stdexcept: No such file or directory ...

The question might also be in the swig interface file (see ...\app\src\main\jni\libmatrixmultiplication.i) remove following code

%include "std_vector.i"
%include "std_string.i"
%include "exception.i"
...

if these headfiles are not required in your C/C++ code.

About

This is an example of using matlab function from android applications through android studio, following the instruction, one can integrate supported matlab function to speed up your Android app.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published