###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
- Create jni folder under main folder
- Put C/C++ code into jni folder
- Write Android.mk & Application.mk file following given references
- Write interface file (in this example is: libmatrixmultiplication.i) following given reference
- 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
- 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
- Call the compiled library using code under your wanted android activity or service:
static {
System.loadLibrary("MatrixMultiplication");
}
- 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
- Matlab coder with iPhone & Android video tutorial
- Matlab coder with Android code example
- Matlab coder video tutorial (Chinese)
- Matlab coder document
- SWIG and Android
- Android Studio, gradle and NDK integration
- Compiling and Using a C++ Library on Android with Android Studio
- Wrapping C++ code with JNI
- Android NDK in Android Studio with SWIG
- Wrapping C arrays with Java arrays
- Java Native Access(JNA)
- Easier way to access native code
####Potential Problems for NDK
- How to generate .so file in Android Studio With NDK
- Can't include C++ headers like vector in Android 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.