上一篇,我已经阐述了如何创建一个简单的NDK实例: NDK 开发实例一(Android.mk环境配置下)
在上一篇的基础上,我们来添加Eigen库,然后做一个简单实例。
Eigen是一个高层次的C ++库,有效支持线性代数,矩阵和矢量运算,数值分析及其相关的算法。下面我们介绍一下
如何添加Eigen库。
1、首先在Eigen官网(http://eigen.tuxfamily.org)下载最新的zip包,解压,获取Eigen源码库:
2、把Eigen文件夹的源码添加到 项目jni目录下。因为Eigen 是C++库,所以注意C++文件的后缀名为 .cpp。
还有一些JNI的方法与C语言也存在差异。
Eigen库用例如下:
#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: " << m << std::endl; VectorXd v(2); v(0) = 4; v(1) = v(0) - 1; // std::cout << "Here is the vector v: " << 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); }
// 这是Java public native String invokeCmethod(); 对于的C++ 方法。
3、在Android.mk 的配置如下:
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)
4、在Application.mk 的配置如下:
APP_PLATFORM := android-23 APP_ABI := armeabi APP_STL := stlport_static
5、ndk-build后,编译出相应的.so库,运行,输出相应处理的 矩阵 信息。
小技巧:
在 app/build.grade 配置如下后,就不用每次都去 ndk-build,编译 .so库了
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' }