您的位置:首页 > 移动开发 > Android开发

NDK开发入门

2017-07-14 14:48 253 查看
本文以在Android APP使用
speexdsp
中的回音消除为例来说明NDK开发流程;

编译native lib

download
speexdsp


编写cmake

此部分的流程可以参见上一篇blog《CMake交叉编译》

写好Java Class

// EchoCanceller.java
package com.icatchtek.smarthome.echo;

public class EchoCanceller {
public native int echoInit( int frameSize, int filterLength );
public native void echoCancellation( byte[] input, byte[] echo, byte[] output );
public native void echoDestroy();
}


使用
javac
javah
生成JNI header file

在以上的java source file目录下使用
javac
来生成class文件

// -d class -> 指定目录,class文件生成在`class`目录下
javac -d class EchoCanceller.java


使用
javah
来生成JNI header file

cd class
javah -classpath . com.icatchtek.smarthome.echo.EchoCanceller


执行上述操作后,在class目录下会生成
com_icatchtek_smarthome_echo_EchoCanceller.h


编写对应的JNI实现

有了header file以后,进行对应的功能实现
com_icatchtek_smarthome_echo_EchoCanceller.cpp
,对JNI编写不熟悉的可以参见《Pro Android C++ with the NDK》

添加JNI file到native lib编译

有了对应的功能实现以后,需要把
com_icatchtek_smarthome_echo_EchoCanceller.cpp
加入native lib再次编译

编写Java测试程序

进行完上述步骤以后,native & JNI的准备就已经好了;我们把编译好的android lib放入对应架构的APP lib目录下;

然后在刚才的
EchoCanceller.java
中添加进lib的load,就可以在APP中使用
EchoCanceller
测试功能了

package com.icatchtek.smarthome.echo;

public class EchoCanceller {
public native int echoInit( int frameSize, int filterLength );
public native void echoCancellation( byte[] input, byte[] echo, byte[] output );
public native void echoDestroy();

static {
System.loadLibrary( "speexdsp" );
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android java ndk