NDK 开发实例二(添加 Eigen库)

     上一篇,我已经阐述了如何创建一个简单的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:\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);
  }
复制代码
// 这是Java public native String invokeCmethod(); 对于的C++ 方法。
 
3、在Android.mk 的配置如下:
1
2
3
4
5
6
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'
}
复制代码

 

源代码用例下载链接

posted @   小小情意  阅读(1857)  评论(2编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示