# Eigen in android
### First setup NDK and javah for android studio
1. after open Android Studio, choosing 'Configure' -> 'Preferences' -> 'Tools' -> 'External Tools'
2. click the add button
* javah:
* Name, Descriptions type anything you want
* Tool Settings:
Program: choose the path of your javah
Parameters: -v -jni -d ```$ModuleFileDir$\src\main\jni $FileClass$```
Working directory: ```$SourcepathEntry$```
* NDK
* Name, Descriptions type anything you want
* Tool Settings:
Program: choose the path of your ndk
Parameters: ```NDK_PROJECT_PATH=$ModuleFileDir$/build/intermediates/ndk ```
```NDK_LIBS_OUT=$ModuleFileDir$/src/main/jniLibs ```
```NDK_APPLICATION_MK=$ModuleFileDir$/src/main/jni/Application.mk ```
```APP_BUILD_SCRIPT=$ModuleFileDir$/src/main/jni/Android.mk ```
```V=1```
Workint directory: ```$SourcepathEntry$```
* Done!
### create project
1. create jni folder under app/src/main
2. download eigen http://eigen.tuxfamily.org/index.php?title=Main_Page#Download
3. then copy Eigen Folder to jni folder
4. create a java class(here I call Eigenlib)
```
package com.example.eugenechou.matrixcal;
public class Eigenlib {
static {
System.loadLibrary("eigenlib"); // the .so name we will name later
}
// the function to be implement
public static native double[] mul(double[] matrix, int row, int col);
}
```
5. use javah(click Tools->External Tools->External Tools->javah) to generate .h file (packageName_Eigenlib.h)
```
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_example_eugenechou_matrixcal_Eigenlib */
#ifndef _Included_com_example_eugenechou_matrixcal_Eigenlib
#define _Included_com_example_eugenechou_matrixcal_Eigenlib
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: com_example_eugenechou_matrixcal_Eigenlib
* Method: mul
* Signature: ([DII)[D
*/
JNIEXPORT jdoubleArray JNICALL Java_com_example_eugenechou_matrixcal_Eigenlib_mul
(JNIEnv *, jclass, jdoubleArray, jint, jint);
#ifdef __cplusplus
}
#endif
#endif
```
6. create .cpp file under jni(here I name test.cpp)
```
#include <iostream>
#include <math.h>
#include <jni.h>
#include <exception>
#include <Eigen/Dense>
using namespace Eigen;
extern "C"{
//declare constant here
double a[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
double b[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
jdoubleArray
Java_com_example_eugenechou_matrixcal_Eigenlib_mul
(JNIEnv *env, jclass obj, jdoubleArray matrix, jint row, jint col){
jsize length = env->GetArrayLength(matrix);
if(row != col) return NULL;
if(length != row * col) return NULL;
MatrixXd Mat(row, col), const1(row, col), const2(row, col);
jdouble *array = env->GetDoubleArrayElements(matrix, 0);
// convert the array to matrix
int k=0;
for(int i=0 ; i<row ; i++)
for(int j=0 ; j<col ; j++){
Mat(i, j) = array[k];
const1(i, j) = a[k];
const2(i, j) = b[k];
k++;
}
// multiplication
MatrixXd result = const1 * const2 * Mar;
//convert matrix back to array
k=0;
for(int i=0 ; i<row ; i++)
for(int j=0 ; j<col ; j++)
array[k++] = result(i, j);
int len = (int) row * col;
jdoubleArray ret = env->NewDoubleArray(len);
env->SetDoubleArrayRegion(ret, 0, len, array);
env->ReleaseDoubleArrayElements(matrix, array, 0);
return ret;
}
}
```
7. create Android.mk under jni
```
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := eigenlib
LOCAL_SRC_FILES := eigenlib.cpp
include $(BUILD_SHARED_LIBRARY)
```
8. create Application.mk under jni
```
APP_ABI := all
APP_STL := c++_shared
APP_CPPFLAGS += -fexceptions
```
9. use ndk(click Tools->External Tools->External Tools->NDK) to build .so file
10. calling in java
```
package com.example.eugenechou.matrixcal;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = (TextView)findViewById(R.id.TextView1);
StringBuffer sb = new StringBuffer();
Eigenlib eigenlib = new Eigenlib();
double[] matrix = {2, 2, 2, 2, 2, 2, 2, 2, 2};
int row = 3, col = 3;
sb.append("matrix: \n");
int len = matrix.length;
for(int i=0 ; i<len ; i++){
sb.append(matrix[i]);
if(i != len - 1) sb.append(" ");
if((i+1) % row == 0) sb.append("\n");
}
sb.append("\n");
sb.append("\n");
sb.append("Result: \n");
double[] f = eigenlib.mul(matrix, row, col);
if(f == null) sb.append("Result error!");
else{
len = f.length;
for(int i=0 ; i<len ; i++){
sb.append(f[i]);
if(i != len-1) sb.append(" ");
if((i+1) % row == 0) sb.append("\n");
}
}
textView.setText(new String(sb));
}
}
```
###### tags: `note`