上一篇,我已經闡述瞭如何建立一個簡單的NDK實例: NDK 開發實例一(Android.mk環境配置下)html
在上一篇的基礎上,咱們來添加Eigen庫,而後作一個簡單實例。android
Eigen是一個高層次的C ++庫,有效支持線性代數,矩陣和矢量運算,數值分析及其相關的算法。下面咱們介紹一下ios
如何添加Eigen庫。算法
一、首先在Eigen官網(http://eigen.tuxfamily.org)下載最新的zip包,解壓,獲取Eigen源碼庫:apache
二、把Eigen文件夾的源碼添加到 項目jni目錄下。由於Eigen 是C++庫,因此注意C++文件的後綴名爲 .cpp。app
還有一些JNI的方法與C語言也存在差別。post
#include <jni.h> #include <string> #include <Eigen/Dense> #include <iostream> using namespace Eigen; extern "C" jstring Java_com_magicing_eigenndk_NDKUtils_invokeCmethod( JNIEnv *env, jobject /* this */) { MatrixXd m(2,2); m(0,0) = 3; m(1,0) = 2.5; m(0,1) = -1; m(1,1) = m(1,0) + m(0,1); // std::cout << "Here is the matrix m:\n" << m << std::endl; VectorXd v(2); v(0) = 4; v(1) = v(0) - 1; // std::cout << "Here is the vector v:\n" << v << std::endl; std::string hello = "Hello Eigen v(1)=" ; char out[1024]; sprintf(out,"%s%f",hello.c_str(),v(1)); return env->NewStringUTF(out); }
LOCAL_PATH:=$(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := NDKUtils LOCAL_SRC_FILES := com_magicing_eigenndk_NDKUtils.cpp LOCAL_C_INCLUDES += $(LOCAL_PATH)/Eigen include $(BUILD_SHARED_LIBRARY)
APP_PLATFORM := android-23 APP_ABI := armeabi APP_STL := stlport_static
import org.apache.tools.ant.taskdefs.condition.Os apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion '25.0.0' defaultConfig { applicationId "com.magicing.eigenndk" minSdkVersion 15 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } sourceSets{ main{ jni.srcDirs = [] // disable automatic ndk-build call, which ignore our Android.mk jniLibs.srcDir 'src/main/jni/libs' } } task ndkBuild(type: Exec) { File workingDir=file('src/main/jni') println workingDir.absolutePath commandLine getNdkBuildCmd(),'NDK_PROJECT_PATH='+workingDir.absolutePath,'APP_BUILD_SCRIPT='+workingDir.absolutePath+'/Android.mk','NDK_APPLICATION_MK='+workingDir.absolutePath+'/Application.mk' } tasks.withType(JavaCompile) { compileTask -> compileTask.dependsOn ndkBuild } } //獲取NDK目錄路徑 def getNdkDir() { if (System.env.ANDROID_NDK_ROOT != null) return System.env.ANDROID_NDK_ROOT Properties properties = new Properties() properties.load(project.rootProject.file('local.properties').newDataInputStream()) def ndkdir = properties.getProperty('ndk.dir', null) if (ndkdir == null) throw new GradleException("NDK location not found. Define location with ndk.dir in the local.properties file or with an ANDROID_NDK_ROOT environment variable.") return ndkdir } //根據不一樣系統獲取ndk-build腳本 def getNdkBuildCmd() { def ndkbuild = getNdkDir() + "/ndk-build" if (Os.isFamily(Os.FAMILY_WINDOWS)) ndkbuild += ".cmd" return ndkbuild } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.1.1' testCompile 'junit:junit:4.12' }